警告:mysql_fetch_array()要求参数1为resource,boolean给出

时间:2017-03-19 12:25:10

标签: php sql

我有这个问题,我正在使用简短的PHP代码来获取JQuery自动完成的建议。我尝试了很多东西,但我无法找到解决方案:

//connect to your database
<?php require('Connections/con_1.php');?>
<?php

//retrieve the search term that autocomplete sends
$term = trim(strip_tags($_REQUEST['term']));

//save search queries to csv file
$file = fopen("searchqueries.csv","a");
fputcsv($file,explode(',',$term));
fclose($file);

//query the database for entries containing the term
$qstring ="USE database_2";"SELECT DISTINCT Pracodawca as value FROM Pensje WHERE Pracodawca LIKE '%".$term."%'";
$result = mysql_query($qstring) or die($result.mysql_error());

//loop through the retrieved values        
while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
    {

    //build an array
    $row['value']=htmlspecialchars(stripslashes($row['value']));
    $row_set[] = $row['value'];
    $row['value']=
    $row_set[] = array('value' => ($row['value']));
    }

//format the array into json data
echo json_encode($row_set, JSON_UNESCAPED_UNICODE);
?>

我收到错误“警告:mysql_fetch_array()期望参数1是资源,布尔值在”中给出,结果是 NULL

我做错了什么?

1 个答案:

答案 0 :(得分:0)

请不要使用mysql_ *函数。

更改以下行:

$qstring ="USE database_2";"SELECT DISTINCT Pracodawca as value FROM Pensje WHERE Pracodawca LIKE '%".$term."%'";

$qstring = "SELECT DISTINCT Pracodawca as value FROM Pensje WHERE Pracodawca LIKE '%".$term."%'";

由于在连接数据库时已选择数据库,因此在查询USE dbname时无需传递php

再试一次。