在PHP函数中查询数据库

时间:2013-04-17 23:06:14

标签: php mysql

我正在尝试使用PHP从数据库加载一些数据,但由于某种原因,当我将它放在函数中时它不起作用。如果我在没有函数的情况下尝试代码,它可以正常工作:

//$dbc connection
$call1 = 0;
$output = '';
$query = "select * from artists order by lname limit $call1, 15";
$result = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($result)){
    $output .= "<ul>";
    $output .= "<li>" . $row['name'] . "</li>";
    $output .= "</ul>";
}

但是,当我将代码更改为函数内部时,我没有从数据库中获取任何内容(或者至少它不会返回任何内容):

//$dbc connection
$call1 = 0;
$output = '';
function loadArtists($call){
    $query = "select * from artists order by lname limit $call, 15";
    $result = mysqli_query($dbc, $query);
    while($row = mysqli_fetch_array($result)){
        $output .= "<ul>";
        $output .= "<li>" . $row['name'] . "</li>";
        $output .= "</ul>";
    }
}
loadArtists($call1);

我在这里做错了什么?

5 个答案:

答案 0 :(得分:5)

您无法在函数中使用$dbc,因为它是global变量。

您可以使用

function loadArtists($call){
    global $dbc;
    ...
}

使$dbc知道loadArtists()或将其作为第二个参数传递

function loadArtists($dbc, $call){
...
}

并将其命名为

loadArtists($dbc, $call1);

答案 1 :(得分:3)

正如我在其中一条评论中提到的,使用global来修复连接范围is poor practice正确传递连接的方式如下:

$dbc = mysqli_connect("localhost", "my_user", "my_password", "world");

$call1 = 0;
$output = '';
function loadArtists($call, $dbc){
    $query = "select * from artists order by lname limit $call, 15";
    $result = mysqli_query($dbc, $query);
    while($row = mysqli_fetch_array($result)){
        $output .= "<ul>";
        $output .= "<li>" . $row['name'] . "</li>";
        $output .= "</ul>";
    }
}
loadArtists($call1, $dbc);

答案 2 :(得分:2)

每次要进行数据库连接时,在与您执行的代码相同的页面上声明用户名和密码是不好的做法,因为:

  1. 如果您移动到与开发环境不同的主机或不同的环境,则可能有多个页面需要编辑。
  2. 如果您在root之外声明它,则可以限制从FTP帐户访问数据库密码。
  3. 我喜欢使用连接函数,所以如果连接关闭,你可以随意重新打开它(减少服务器开销)。此外,您不必将其设置为函数内的全局变量(not a good idea because of several reasons)。

    因此,出于这些原因,此连接应位于根目录之外(下方)。

    <强> /../安全/ connection.php

    function openSQL() {
      $conn = mysqli('localhost', 'my_user', 'my_password', 'my_db');
      return $conn;
    }
    

    <强>的functions.php

    require_once($_SERVER['DOCUMENT_ROOT'].'/../safe/connection.php');
    function loadArtists($call){
       $dbc = openSQL();
       $query = "select * from artists order by lname limit $call, 15";
       $result = mysqli_query($dbc, $query);
       while($row = mysqli_fetch_array($result)){
          $output .= "<ul>";
          $output .= "<li>" . $row['name'] . "</li>";
          $output .= "</ul>";
       }
       mysqli_close($dbc);
       return $output;
    }
    
    $myOutput = loadArtists(4); 
    

答案 3 :(得分:2)

问题是variable scope。该变量在你的函数中不存在

有三种方法可以解决这个问题:

  1. 将其设为全局,这意味着可以从函数中读取外部变量。 (请注意,使用全局变量通常被视为安全问题。)

    global $dbc;
    
  2. 您可以将该变量作为参数传递给函数

    function loadArtists($connection, $call) { ... }
    
  3. 你可以创建一个类,该类变量现在可以在类函数中使用:

    class Artists {
        public $dbc;
        public function __construct() {
            $this->dbc = open_the_db_connection(); //etc?
        }
        public function loadArtists($call) {
            $query = "select * from artists order by lname limit $call, 15";
            $result = mysqli_query($this->dbc, $query);
            while($row = mysqli_fetch_array($result)){
                $output .= "<ul>";
                $output .= "<li>" . $row['name'] . "</li>";
                $output .= "</ul>";
            }
            return $output;
        }
    }
    

答案 4 :(得分:1)

这对我来说似乎是一个范围问题。您在函数中引用的$ output与您在函数外定义的$ output不同。

您应该将功能更改为以下内容:

function loadArtists($call){
$output = "";
$query = "select * from artists order by lname limit $call, 15";
$result = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($result)){
    $output .= "<ul>";
    $output .= "<li>" . $row['name'] . "</li>";
    $output .= "</ul>";
}

return $output;

}

$ output = loadArtists($ call1);