获取mysqli获取数组错误

时间:2013-07-06 12:07:38

标签: php mysqli

这是我得到的错误。请帮助代码。 警告:mysqli_fetch_array()要求参数1为mysqli_result,第28行的C:\ xampp \ htdocs \ check \ test.php中给出布尔值

警告:mysqli_fetch_array()要求参数1为mysqli_result,第33行的C:\ xampp \ htdocs \ check \ test.php中给出布尔值

   <?php

    $hostname='localhost';
    $user='root';
    $password='';

    $connect=mysqli_connect($hostname,$user,$password,'a_railway');

    if(!$connect)
     {
       die('Could not connect');
      }

    $rs="central";
    $rd="central";
    $s="Kurla";
    $d="Dadar";

    $dist=call_dist($rs,$rd,$s,$d,$connect);
    echo $dist;

    function call_dist($rs,$rd,$s,$d,$connect)
    {
         $src_dist="SELECT Distance FROM $rs WHERE Station=$s ";
         $dest_dist="SELECT Distance FROM $rs WHERE Station=$d ";   

         $res1=mysqli_query($connect,$src_dist);
         $src_res = mysqli_fetch_array($res1);
         $s_dist=$src_res['Distance'];

         $res2=mysqli_query($connect,$dest_dist);
         $dest_res = mysqli_fetch_array($res2);
         $d_dist=$dest_res['Distance']; 

         $dist=abs($s_dist-$d_dist);
         return $dist;
    }
    ?>

2 个答案:

答案 0 :(得分:0)

变化:

$src_dist="SELECT Distance FROM $rs WHERE Station=$s ";
$dest_dist="SELECT Distance FROM $rs WHERE Station=$d ";   

要:

$src_dist = "SELECT Distance FROM $rs WHERE Station  = '" . mysqli_real_escape_string ($connect, $s) . "'";
$dest_dist = "SELECT Distance FROM $rs WHERE Station = '" . mysqli_real_escape_string ($connect, $d) . "'";   

您必须引用字符串值。

答案 1 :(得分:-1)

试试这个。由于查询失败,因此给出了布尔值。它应该工作,但我不确定你的变量包含什么。

function call_dist($rs,$rd,$s,$d,$connect)
        {
             $src_dist="SELECT `Distance` FROM `$rs` WHERE `Station` = '$s' ";
             $dest_dist="SELECT `Distance` FROM `$rs` WHERE `Station` = '$d' ";   

             $res1=mysqli_query($connect,$src_dist);
             $src_res = mysqli_fetch_array($res1);
             $s_dist=$src_res['Distance'];

             $res2=mysqli_query($connect,$dest_dist);
             $dest_res = mysqli_fetch_array($res2);
             $d_dist=$dest_res['Distance']; 

             $dist=abs($s_dist-$d_dist);
             return $dist;
        }