PHP Mysql IN子句不起作用

时间:2014-09-28 15:17:35

标签: php mysql sql

这是我的代码:

public function fetchAllPhotoRatings($table_name,$arrayOfPics)
{
    $mysqli = $this->returnDatabaseConnection();
    $picsArrayToString = "";


    foreach ($arrayOfPics as $value) 
    {
        $picsArrayToString .= '"'.$value.'",';
    }
    $picsArrayToString = rtrim($picsArrayToString, ",");

    // Running the below in PHPMyAdmin works
    //echo "SELECT number_of_ratings,voted_on_image,rated_by from $table_name where voted_on_Image in($picsArrayToString)";exit;


    if ($stmt = $mysqli->prepare ( "SELECT number_of_ratings, voted_on_image, rated_by from $table_name where voted_on_Image in(?)" ))
    {
        $stmt->bind_param ( "s", $picsArrayToString);


        $stmt->execute ();
        $stmt->bind_result ($number_of_ratings,$voted_on_image,$rated_by);

        echo $rated_by; // Not binding, this value is false.
        $array = array();
        $i = 0;
        while ($stmt->fetch())
        { echo "here";exit; // Trying to see if it enters the while() loop
            if (isset ($voted_on_image))
            {
                $array[$i]['number_of_ratings']         = $number_of_ratings;
                $array[$i]['voted_on_image']            = $voted_on_image;
                $array[$i]['rated_by']                  = $rated_by;
            }
            $i++;
        }
        $stmt->close ();
    }
    $mysqli->close ();


    if (empty($array)) // This shouldnt fire, but it does :(
    {
        die("Invalid data");
    }
    else
    {
        return $array;
    }

}

如果我直接在PHPMyAdmin中运行SQL它运行正常,但是当我运行此脚本时它不会进入while()循环/不从DB中获取任何内容。
我认为它与mySql的IN子句有关...或者?

从上面生成的SQL:

  

SELECT number_of_ratings,voted_on_image,rated_by from   Gallery_ratings中的voted_on_Image   在(" cd4ab876c9ce3e854d95e53b07e97ecb.jpg"" a1b37032887c916ded69f1f023845ef6.jpg&#34)

2 个答案:

答案 0 :(得分:2)

MySQLI没有数组(或枚举..)类型,只有i整数,s字符串,b二进制/ blob,d表示双

您的查询应准备如下:

$array       = array('one', 'two', 'three');

$query       = "SELECT .... IN ("
             . implode(', ', array_fill(0, sizeof($array), '?'))
             . ")";

$stmt->bind_param(str_repeat('s', sizeof($array)), $array);

输出就像:

SELECT .... IN (?, ?, ?)

bind_param然后看起来像:

->bind_param('sss', $array);

如果我对这个假设错了...... 因为你说输出看起来确实正确,我还有一些想法:

try {
    $stmt = $mysqli->prepare("SELECT number_of_ratings, voted_on_image, rated_by from $table_name where voted_on_Image in(?)");
} catch (Exception $e) {
    // Error exception
    echo $e->getMessage();
}

if ($stmt !== false) {
    // go on..

    while (true) {
        try {
            $fetchStatus = $stmt->fetch();
        } catch (Exception $e) {
            // Error exception
            echo $e->getMessage();
            break;
        }

        if ($fetchStatus === false) {
            echo $mysqli->error;
            break;
        }
        elseif ($fetchStatus === null) {
            // no more results
            break;
        }
        else {
            // all fine
        }

        // ...
    }

} else {
    echo $mysqli->error;
}

答案 1 :(得分:0)

参数化查询不会与IN条款一起播放,因为?会被'boundValue'替换为引号!

你可以做的是:

$numberOfPics=count($arrayOfPics);
$placeHolders = implode(',',array_fill(0,  $numberOfPics , '?'));
$query = "SELECT number_of_ratings, voted_on_image, rated_by from $table_name 
     where voted_on_Image in($placeHolders)"

$params=implode('',array_fill(0,$numberOfPics,'s'));


$stmt->bind_param($params, $arrayOfPics);