Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysqli_查询数据库选择错误_Php_Mysql_Database_Select_Mysqli - Fatal编程技术网

Php mysqli_查询数据库选择错误

Php mysqli_查询数据库选择错误,php,mysql,database,select,mysqli,Php,Mysql,Database,Select,Mysqli,我试图从数据库中选择文本,但只选择由某些用户名发布的文本。基本上,我需要有人看看这个PHP和MySQL代码,告诉我他们看到了什么问题。我希望我已经提供了足够的信息。另外,我得到了以下错误:警告:mysqli_fetch_数组期望参数1是mysqli_结果,字符串在。。。谢谢代码如下: $followed = mysqli_query($con,"SELECT followed FROM follows WHERE follower = '$username'"); while($row = m

我试图从数据库中选择文本,但只选择由某些用户名发布的文本。基本上,我需要有人看看这个PHP和MySQL代码,告诉我他们看到了什么问题。我希望我已经提供了足够的信息。另外,我得到了以下错误:警告:mysqli_fetch_数组期望参数1是mysqli_结果,字符串在。。。谢谢代码如下:

$followed = mysqli_query($con,"SELECT followed FROM follows WHERE follower = '$username'");

while($row = mysqli_fetch_array($followed)){

    echo $row['followed']."<br>";
    $followed = $row['followed'];

    $random = mysqli_query($con,"SELECT text FROM post WHERE user = '$followed'");

    while($row = mysqli_fetch_array($random)){
        echo "<ul><li id = 'stream-post'>";
        echo $row['text'];
        echo "</li></ul>";
        $user = $row['user'];
    }
}

作为提示:第二个循环将结果分配给已经保存第一个查询中的行的行。使用不同的变量名:

    ...
    while($row = mysqli_fetch_array($followed)){

    echo $row['followed']."<br>";
    $followed = $row['followed'];

    $random = mysqli_query($con,"SELECT text FROM post WHERE user = '$followed'");

    while($subrow = mysqli_fetch_array($random)){
        echo "<ul><li id = 'stream-post'>";
        echo $subrow['text'];
        ....

代码中的问题是$followed是保存SQL结果的变量。在第一次获取之后,它将被一个字符串值覆盖。下次通过循环时,$followed不再是对查询返回的结果集的引用

还尝试从数组$行检索键“user”,但该键在数组中不存在

此外,您的代码容易受到SQL注入的攻击,并且无法检查查询返回是否成功。我们希望看到带有绑定占位符的准备好的语句,但至少,您应该在不安全的值上调用mysqli_real_escape_string函数,并在SQL文本中包含函数的返回

下面是一个我更喜欢遵循的模式示例

# set the SQL text to a variable
$sql = "SELECT followed FROM follows WHERE follower = '" 
     . mysqli_real_escape_string($con, $username) . "' ORDER BY 1"; 
# for debugging
#echo "SQL=" . $sql; 

# execute the query
$sth = mysqli_query($con, $sql);

# check if query was successful, and handle somehow if not
if (!$sth) {
    die mysqli_error($con);
}

while ($row = mysqli_fetch_array($sth)) {
    $followed = $row['followed'];
    echo htmlspecialchars($followed) ."<br>";

    # set SQL text 
    $sql2 = "SELECT text FROM post WHERE user = '"
          . mysqli_real_escape_string($con, $followed) . "' ORDER BY 1";
    # for debugging
    #echo "SQL=" . $sql2;

    # execute the query
    $sth2 = mysqli_query($con, $sql2);

    # check if query execution was successful, handle if not
    if (!$sth2) {
       die mysqli_error($con);
    }

    while ($row2 = mysqli_fetch_array($sth2)) {
        $text = $row2['text'];

        echo "<ul><li id = 'stream-post'>" . htmlspecialchars($text) . "</li></ul>";
    }
}

谷歌的错误消息,让我们知道你认为它可能是什么。请使用。它不一定是错误的重复使用$行。OP已经抓取了fetch返回到数组中的列值。一个更大的问题是,当数组键“user”不在数组中时,尝试检索该数组键。此处发布的示例代码受SQL注入的约束,就像操作代码一样。@spencer7593关于两行的:当代码保持原样时,您可能是对的。如果它将被改变/发展,这些事情就是夜班调试的原因…真正的问题是$1的重用。第一次通过循环时,会被覆盖。第二次获取是尝试从字符串而不是结果集获取。我更喜欢在resultset中使用变量名,如$sth语句句柄或$stmt。回到我早期的Perl DBI时代。感谢Axel,重复的$follow就是问题所在。
# set the SQL text to a variable
$sql = "SELECT followed FROM follows WHERE follower = '" 
     . mysqli_real_escape_string($con, $username) . "' ORDER BY 1"; 
# for debugging
#echo "SQL=" . $sql; 

# execute the query
$sth = mysqli_query($con, $sql);

# check if query was successful, and handle somehow if not
if (!$sth) {
    die mysqli_error($con);
}

while ($row = mysqli_fetch_array($sth)) {
    $followed = $row['followed'];
    echo htmlspecialchars($followed) ."<br>";

    # set SQL text 
    $sql2 = "SELECT text FROM post WHERE user = '"
          . mysqli_real_escape_string($con, $followed) . "' ORDER BY 1";
    # for debugging
    #echo "SQL=" . $sql2;

    # execute the query
    $sth2 = mysqli_query($con, $sql2);

    # check if query execution was successful, handle if not
    if (!$sth2) {
       die mysqli_error($con);
    }

    while ($row2 = mysqli_fetch_array($sth2)) {
        $text = $row2['text'];

        echo "<ul><li id = 'stream-post'>" . htmlspecialchars($text) . "</li></ul>";
    }
}