mysql prepare语句-选择 [英] Mysql prepare statement - Select

查看:151
本文介绍了mysql prepare语句-选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁可以给我建议?为什么查询无法为我提供期望值?谢谢.

Who can give me an advice? why the query can't to provide me an expected value? Thanks.

 $mysqli = new mysqli($GLOBALS["mysql_host"], $GLOBALS["mysql_user"],          $GLOBALS["mysql_passwd"], $GLOBALS["mysql_database"]);
 $stmt = $mysqli->prepare("SELECT one FROM table ORDER BY date DESC LIMIT 1");
 $last = $stmt->bind_result($one);
 $stmt->execute();
 $stmt->close();
 $mysqli->close();

 Echo $last; //it should be "abc"

推荐答案

我认为您必须先execute,然后在mysql_stmt -objects上调用fetch. 因为您可能会得到多个结果(行).

I think you have to execute and then call fetch on mysql_stmt-objects. Because you may get multiple results (rows).

使用fetch,您将前进结果光标.

With fetch you will advance your result-Cursor.

文档: mysqli mysqli-stmt

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) {
    $stmt->execute();

    /* bind variables to prepared statement */
    $stmt->bind_result($col1, $col2);

    /* fetch values */
    while ($stmt->fetch()) {
        printf("%s %s\n", $col1, $col2);
    }

    /* close statement */
    $stmt->close();
}
/* close connection */
$mysqli->close();

?>

这篇关于mysql prepare语句-选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆