mysqli_fetch_array()期望参数1为mysqli_result,第37行给出布尔值

时间:2015-06-23 11:16:53

标签: php sql mysqli

/ *  * 修长的人  *连接数据库  * @ return bool(连接)  * / function db_connect(){

$connection = ysqli_connect("localhost","slenderman","slenderman");
if(!$connection){
    return false;
}   
if (!mysqli_select_db($connection, "joblicious")) {
    return false;
}
return $connection;

}

function find_jobs(){         db_connect();

$select = "SELECT jobs.id,
                  jobs.location,
                  jobs.title,
                  jobs.company,
                  jobs.description,
                  jobs.url";
$from = "FROM jobs.id";  
$where ="WHERE jobs.id >0";

$query = $select.$from.$where;
$result = mysqli_query(db_connect(),$query);

 while ($row = mysqli_fetch_array($result)){
    echo $row['id'];
 }

}

find_jobs();

4 个答案:

答案 0 :(得分:1)

您错过了查询部分之间的空白。 改变

$query = $select.$from.$where;

$query = $select." ".$from." ".$where;

此外,您应该在每个sql语句后检查错误。

答案 1 :(得分:0)

您收到此错误是因为您的查询返回了错误结果。

  • 另一个问题是你写的
  

$ from =“FROM jobs.id”;

我认为您的表名是作业,因此请将其替换为作业并使用以下方法检查结果:

  

如果(!空($结果)){

     

}

答案 2 :(得分:0)

<?php 

function db_connection(){

    $connection = mysqli_connect("localhost","slenderman","slenderman");
    if(!$connection){
     return false;
    }
    if(!mysqli_select_db($connection,"joblicious")){
     return false;
    }
      return $connection;
    }


function find_jobs(){
         db_connection();


 $select =  ("SELECT jobs.id,
                    jobs.location,
                    jobs.title,
                    jobs.company,
                    jobs.description,
                    jobs.url ");
 $from = ("FROM jobs");
 $where = ("WHERE jobs.id >0");

 $query = $select." ".$from." ".$where;



    $result = mysqli_query(db_connection(),$query); 
    while($row = mysqli_fetch_array($result)){
    echo $row["title"];
   }
}
find_jobs();

?>

答案 3 :(得分:0)

不鼓励您编写SQL查询的方式。尝试整体编写查询:

$query = "SELECT jobs.id, jobs.location, jobs.title, jobs.company, jobs.description, jobs.url FROM jobs.id WHERE jobs.id >0";

我确定由于select语句的语法不正确而导致此错误(很可能是由于缺少空格)。

请查看 PHP syntax manual,以避免将来出现此类错误。