MySQL prepared语句返回NULL值

时间:2015-03-11 12:01:07

标签: php mysql

我已经创建了一个MYSQL预处理语句来查询我的数据库但是我没有得到我想要的结果。 var_dump返回NULL值。这是我的代码

更新完整代码和错误

function dynamicsearch() {
    $link = mysqli_connect ( '192.168.2.113', 'root', '', 'solstats' );

    if (isset ( $_POST ['callerID'] )) {
        $number = $_POST ['callerID'];
        $qry = "SELECT * FROM call_outcome WHERE callerid LIKE '?' LIMIT 10";
        $stmt = mysqli_prepare ( $link, $qry );
        $stmt->bind_param('s', $number);
        $stmt->execute ();
        var_dump($stmt);
        $res = $stmt->fetch ();
        $row = mysqli_fetch_assoc ( $res );
        return $res;

    }
}


var_dump ( $res );
$IDdata = dynamicsearch ();
?>
<!DOCTYPE html>
<html>
<body>
    <h2>DB Query Results</h2>
    <table border="2">
        <tr>
            <th>id</th>
            <th>callerid</th>
            <th>calldate</th>
            <th>ivron</th>
            <th>bopon</th>
            <th>type</th>
            <th>uniqueid</th>
            <th>queue_name</th>
        </tr>
        <tr>        
<?php
while ( $result = mysqli_fetch_array ( $IDdata ) ) { // Fecth array used to fetch each array of the queried result and populate it to the table.
    echo "<tr>";
    echo "<td>" . $result ['id'] . "</td>";
    echo "<td>" . $result ['callerid'] . "</td>";
    echo "<td>" . $result ['calldate'] . "</td>";
    echo "<td>" . $result ['ivron'] . "</td>";
    echo "<td>" . $result ['bopon'] . "</td>";
    echo "<td>" . $result ['type'] . "</td>";
    echo "<td>" . $result ['uniqueid'] . "</td>";
    echo "<td>" . $result ['queue_name'] . "</td>";
    echo "</tr>";
}

?>
</tr>
    </table>

</body>
</html>

不知道我哪里出错。在执行语句后我又做了var_dump并返回了以下错误object(mysqli_stmt)#2 (0) { }。这个错误意味着什么,我该如何解决?感谢。

Error

3 个答案:

答案 0 :(得分:0)

你没有传递它的价值。

   $number = $_POST ['callerID'];
        $qry = "SELECT * FROM call_outcome WHERE callerid LIKE ? LIMIT 10";
        $stmt = mysqli_prepare ( $link, $qry );
        $stmt->bind_param('i', $number); 
        $stmt->execute ();
        $res = $stmt->fetch ();

我还没有和mysqli合作,可能想仔细检查一下。

http://php.net/manual/en/mysqli-stmt.bind-param.php

答案 1 :(得分:0)

请查看此更新代码:

        function dynamicsearch() {
    $link = mysqli_connect ( '192.168.2.113', 'root', '', 'solstats' );

    if (isset ( $_POST ['callerID'] )) {
        $number = $_POST ['callerID'];
        $qry = "SELECT * FROM call_outcome WHERE callerid LIKE '%?%' LIMIT 10";
        $stmt = mysqli_prepare ( $link, $qry );
        mysqli_stmt_bind_param($stmt, "s", $number); //for integer use i , for string use s
        mysqli_stmt_execute($stmt);
        $res= mysqli_stmt_fetch($stmt);       
        return $res;

    }
}

答案 2 :(得分:0)

<?php
function dynamicsearch() {
    $link = mysqli_connect('192.168.2.113', 'root', '', 'solstats');
    $result = array();
    if (isset($_POST ['callerID'])) {
        $number = $_POST ['callerID'];
        $qry = "SELECT * FROM call_outcome WHERE callerid LIKE ? LIMIT 10";
        $stmt = mysqli_prepare($link, $qry);

        mysqli_stmt_bind_param($stmt, 's', $number);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($stmt, $id, $callerid, $calldate, $ivron, $bopon, $type, $uniqueid, $queue_name);
        while (mysqli_stmt_fetch($stmt)) {
            $result[] = ['id' => $id, 'callerid' => $callerid, 'calldate' => $calldate, 'ivron' => $ivron,
                'bopon' => $bopon, 'type' => $type, 'uniqueid' => $uniqueid, 'queue_name' => $queue_name];
        }
    }
    return $result;
}
$IDdata = dynamicsearch();
?>
<!DOCTYPE html>
<html>
    <body>
        <h2>DB Query Results</h2>
        <table border="2">
            <tr>
                <th>id</th>
                <th>callerid</th>
                <th>calldate</th>
                <th>ivron</th>
                <th>bopon</th>
                <th>type</th>
                <th>uniqueid</th>
                <th>queue_name</th>
            </tr>
            <tr>        
                <?php
                foreach ($IDdata as $result) { // Fecth array used to fetch each array of the queried result and populate it to the table.
                    echo "<tr>";
                    echo "<td>" . $result ['id'] . "</td>";
                    echo "<td>" . $result ['callerid'] . "</td>";
                    echo "<td>" . $result ['calldate'] . "</td>";
                    echo "<td>" . $result ['ivron'] . "</td>";
                    echo "<td>" . $result ['bopon'] . "</td>";
                    echo "<td>" . $result ['type'] . "</td>";
                    echo "<td>" . $result ['uniqueid'] . "</td>";
                    echo "<td>" . $result ['queue_name'] . "</td>";
                    echo "</tr>";
                }
                ?>
            </tr>
        </table>
    </body>
</html>