Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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中的mysql查询不返回多行?_Php_Mysql_Sql_Json - Fatal编程技术网

为什么php中的mysql查询不返回多行?

为什么php中的mysql查询不返回多行?,php,mysql,sql,json,Php,Mysql,Sql,Json,我试图以PHP中的JSON格式返回用户的名字和姓氏列表 我的PHP看起来像: $_Query = ' SELECT Fname,Lname FROM users WHERE number = "'.$_REQUEST['number'].'"

我试图以PHP中的JSON格式返回用户的名字和姓氏列表

我的PHP看起来像:

    $_Query = '
                    SELECT
                    Fname,Lname
                    FROM
                    users
                    WHERE
                    number = "'.$_REQUEST['number'].'"
                                                    ;';

    $SQLResult = mysql_query($_Query) or die(mysql_error());

    $_UserData = array();

    if(mysql_num_rows($SQLResult) <> 0){

            while($SQLRow = mysql_fetch_array($SQLResult)){
                    $_UserData['Fname'] = $SQLRow['Fname'];
                    $_UserData['Lname'] = $SQLRow['Lname'];
            }

    }
echo json_encode($_UserData);
$_UserData = array();

if(mysql_num_rows($SQLResult) <> 0){

        while($SQLRow = mysql_fetch_array($SQLResult)){
                // Either...
                // Create a new entry in $_UserData with same structure and contents as $SQLRow
                $_UserData[] = $SQLRow;
                // OR more specific - say if the structures aren't identical
                // $DataRow=array();
                // $DataRow['Fname'] = $SQLRow['Fname'];
                // $DataRow['Lname'] = $SQLRow['Lname'];
                // $_UserData[] = $DataRow;
        }

}
当然。但是,脚本返回具有正确条件的最后一行

{"Fname":"Steve","Lname":"LastName"}
MySQl服务器通过相同的查询返回它应该返回的内容

+--------------+-------------+
| Fname        | Lname       |
+--------------+-------------+
| First_name1  | Last_name1  |
| First_name2  | Last_name2  |
+--------------+-------------+
为什么会发生这种情况?我应该如何着手解决它?谢谢

$_UserData['Fname'] = $SQLRow['Fname'];
$_UserData['Lname'] = $SQLRow['Lname'];

您正在重复设置_UserData的相同两个元素,而不是向数组中添加新值。

您的循环将覆盖$\u UserData数组中的值。 您应该将每一行追加到数组中

此外,请注意,您应该转义您的输入

您的代码应该如下所示:

$_Query = '
                SELECT
                Fname,Lname
                FROM
                users
                WHERE
                number = "'.(int)$_REQUEST['number'].'"
                                                ;';

$SQLResult = mysql_query($_Query) or die(mysql_error());

$_UserData = array();

if(mysql_num_rows($SQLResult) <> 0){

        while($SQLRow = mysql_fetch_array($SQLResult)){
                $_UserData[] = $SQLRow;
        }

}
echo json_encode($_UserData);
试试这个:

while($SQLRow = mysql_fetch_array($SQLResult)){
    $_UserData[] = $SQLRow;
}
在您的示例中,实际上是为每一新行覆盖$\u UserData数组,从而覆盖丢失的行

更新: 这将使JSON字符串看起来更符合您的需要

while($SQLRow = mysql_fetch_assoc($SQLResult)){
    $_UserData['Fname'][] = $SQLRow['Fname'];
    $_UserData['Lname'][] = $SQLRow['Lname'];
}
试试这个:

$\u UserData=数组

if(mysql_num_rows($SQLResult) <> 0){
        $i = 0;
        while($SQLRow = mysql_fetch_array($SQLResult)){
                $_UserData[$i]['Fname'] = $SQLRow['Fname'];
                $_UserData[$i]['Lname'] = $SQLRow['Lname'];
                $i++;
        }

我相信当你意识到你只分配了$\u UserData数组中的一个元素时,你会大吃一惊的

应该是这样的:

    $_Query = '
                    SELECT
                    Fname,Lname
                    FROM
                    users
                    WHERE
                    number = "'.$_REQUEST['number'].'"
                                                    ;';

    $SQLResult = mysql_query($_Query) or die(mysql_error());

    $_UserData = array();

    if(mysql_num_rows($SQLResult) <> 0){

            while($SQLRow = mysql_fetch_array($SQLResult)){
                    $_UserData['Fname'] = $SQLRow['Fname'];
                    $_UserData['Lname'] = $SQLRow['Lname'];
            }

    }
echo json_encode($_UserData);
$_UserData = array();

if(mysql_num_rows($SQLResult) <> 0){

        while($SQLRow = mysql_fetch_array($SQLResult)){
                // Either...
                // Create a new entry in $_UserData with same structure and contents as $SQLRow
                $_UserData[] = $SQLRow;
                // OR more specific - say if the structures aren't identical
                // $DataRow=array();
                // $DataRow['Fname'] = $SQLRow['Fname'];
                // $DataRow['Lname'] = $SQLRow['Lname'];
                // $_UserData[] = $DataRow;
        }

}

干杯

因为您的循环会覆盖以前的值,所以您总是并且只会获取最后一条记录。mysql\u查询容易受到SQL注入攻击。不要在生产代码中使用它,通常没有理由使用它。改用mysqli。你会让自己对SQL注入敞开大门。请了解如何使用参数化查询(最好是PDO模块)来保护您的web应用程序。有一些例子可以让你开始。@Andy@Alex谢谢你,我来看看。尽管如此,为什么htmlspecialchars不处理这个问题呢?htmlspecialcharacters与SQL完全无关。我尝试了这个@Sagi也建议过的方法,我得到了[{0:First_name1,Fname:First_name1,1:Last_name1,Lname:Last_name1},{0:First_name2,Fname:First_name2,1:Last_name2}]您可以使用mysql\u fetch\u assoc而不是mysql\u fetch\u array:while$SQLRow=mysql\u fetch\u assoc$SQLResult{…这应该可以去除json字符串中的编号项。我尝试了第二种方法DataRow=array,它可以工作,但是,有没有办法返回数组中的所有名字和姓氏,所以{Fname:[F-name1,F-name2],…等等?