Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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 - Fatal编程技术网

PHP-使用MySQL检索数据

PHP-使用MySQL检索数据,php,mysql,Php,Mysql,从数据库检索数据时遇到问题。所以我有一个页面有一个搜索框。用户键入项目编号,并通过使用表单传输该编号而重定向到单个项目页面。然后,脚本选择该数字并仅显示该数字等于结果数的结果问题:如果该数字仅由数字组成,则一切正常。如果我们添加字母,则会出现“PHP致命错误:对非对象调用成员函数execute()。在数据库中,我将此设置为文本,而不是数字 以下是单个项目页面上的代码: 因此,首先它从顶部URL(part.php?partnumber=WB20K10023+)获取该数字: 然后运行此函数: fun

从数据库检索数据时遇到问题。所以我有一个页面有一个搜索框。用户键入项目编号,并通过使用表单传输该编号而重定向到单个项目页面。然后,脚本选择该数字并仅显示该数字等于结果数的结果问题:如果该数字仅由数字组成,则一切正常。如果我们添加字母,则会出现“PHP致命错误:对非对象调用成员函数execute()。在数据库中,我将此设置为文本,而不是数字

以下是单个项目页面上的代码:

因此,首先它从顶部URL(part.php?partnumber=WB20K10023+)获取该数字:

然后运行此函数:

function fetchAllItems($item_number) {      
        global $mysqli,$db_table_prefix; 
        $stmt = $mysqli->prepare("SELECT            
            *
            FROM ".$db_table_prefix."items
            WHERE
            item_number = $item_number
            LIMIT 1
            ");
        $stmt->execute(); // <- This line shows in an error_log
        $stmt->bind_result($id);
        while ($stmt->fetch()){
            $row[] = array('id' => $id);
        }
        $stmt->close();
        return ($row);      
    }
函数fetchAllItems($item_number){
全局$mysqli,$db_table_前缀;
$stmt=$mysqli->prepare(“选择
*
来自“$db\u表\u前缀”项
哪里
项目编号=$项目编号
限制1
");
$stmt->execute();//绑定_结果($id);
而($stmt->fetch()){
$row[]=数组('id'=>$id);
}
$stmt->close();
返回($行);
}

如果MySQL表中的partNumber字段是string/text/varchar数据类型,那么在查询字符串中,应该写入
partNumber='$partNumber'
,而不是写入
partNumber=$partNumber

注意,没有orderby的限制几乎没有意义。除此之外,您只是缺少一对倒逗号
partNumber='$partNumber'
。字符串需要用引号括起来。由于您使用的是
mysqli
prepare()
,因此确实应该使用占位符
partNumber=?
$stmt->bind_param('s',$partNumber)。谢谢,已经解决了。参考。-代码未正确使用参数化查询,
$identifier
是内插的,不代表占位符。@Art添加引号会导致代码错误。。确保正确绑定。虽然这可能“修复”问题,但这是导致SQL注入的糟糕方法。OP应该使用占位符,特别是考虑到代码已经使用了MySQLi。
function fetchAllItems($item_number) {      
        global $mysqli,$db_table_prefix; 
        $stmt = $mysqli->prepare("SELECT            
            *
            FROM ".$db_table_prefix."items
            WHERE
            item_number = $item_number
            LIMIT 1
            ");
        $stmt->execute(); // <- This line shows in an error_log
        $stmt->bind_result($id);
        while ($stmt->fetch()){
            $row[] = array('id' => $id);
        }
        $stmt->close();
        return ($row);      
    }