【问题标题】:Querying a database within a PHP function [duplicate]在 PHP 函数中查询数据库 [重复]
【发布时间】:2013-04-10 21:47:35
【问题描述】:

我正在尝试使用 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);

我在这里做错了什么?

【问题讨论】:

  • 检查错误 $result = mysqli_query($dbc, $query) or die('Could not connect: ' . mysqli_error()); 但我想说这是您将函数参数插入 SQL 查询的方式
  • 如果您尝试在函数中将$call 更改为0,然后在不带参数的情况下运行该函数,会发生什么情况?
  • @bamwebdesign 这无关紧要。这是连接的范围问题。

标签: php mysql


【解决方案1】:

您不能在函数中使用$dbc,因为它是一个global 变量。

你可以使用任何一个

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

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

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

并将其称为

loadArtists($dbc, $call1);

【讨论】:

  • -1 Using global is poor practice. 连接应该传递到函数。
  • @FreshPrinceOfSO -1 不是“错误答案”吗?这个答案不是“错误的”。所以也许重新考虑只是评论它是不好的做法,不要-1你觉得不合适?你为什么不只是回答你所说的,让我们说为什么这样使用它更好,而不是global
  • @Allendar 这个答案是错误,因为它声明 OP “必须使用” global
  • 太棒了!感谢两者。
  • @FreshPrinceOfSO 也许我们并不都把答案的上下文理解得如此直接:P 够公平的。
【解决方案2】:

正如我在我的一个 cmets 中提到的,使用 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);

【讨论】:

    【解决方案3】:

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

    1. 如果您移动到与开发环境不同的主机或环境,您可能需要编辑多个页面。
    2. 如果您在 root 之外声明它,您可以限制从 FTP 帐户访问数据库密码。

    我喜欢为连接使用一个函数,因此如果连接关闭,您可以随意重新打开它(减少服务器开销)。此外,您不必将其设置为函数内的全局变量 (not a good idea because of several reasons)。

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

    /../safe/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); 
    

    【讨论】:

      【解决方案4】:

      问题是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;
            }
        }
        

      【讨论】:

        【解决方案5】:

        对我来说,这似乎是一个范围界定问题。您在函数中引用的 $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);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-18
          • 2014-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多