douwen9540 2016-05-14 04:58
浏览 27
已采纳

不知道如何从mysql中回显出Selected数据

$stmt = $mysqli->prepare("SELECT `nameData` FROM `accountsDone` WHERE `nameToSearch` = ?");
$stmt->bind_param("s", $query);
$stmt->execute();
$stmt->store_result();
if ($stmt->affected_rows > 0) {
    echo "Exists";
}

Instead of echoing out Exists, I want it to echo out nameData. How can I go about doing that?

  • 写回答

3条回答 默认 最新

  • douqiu1604 2016-05-14 05:13
    关注

    First of all, if you want only one row then append LIMIT 1 to your SELECT query, like this:

    $stmt = $mysqli->prepare("SELECT `nameData` FROM `accountsDone` WHERE `nameToSearch` = ? LIMIT 1");
    

    So there are two approaches to display nameData:

    Method(1):

    First bind the variable $nameData to the prepared statement, and then fetch the result into this bound variable.

    $stmt = $mysqli->prepare("SELECT `nameData` FROM `accountsDone` WHERE `nameToSearch` = ? LIMIT 1");
    $stmt->bind_param("s", $query);
    $stmt->execute();
    $stmt->store_result();
    if($stmt->num_rows){
        $stmt->bind_result($nameData);
        $stmt->fetch();
        echo $nameData;
    }else{
        echo "No result found";
    }
    

    Method(2):

    First use get_result() method to get the result set from the prepared statement, and then use fetch_array to fetch the result row from the result set.

    $stmt = $mysqli->prepare("SELECT `nameData` FROM `accountsDone` WHERE `nameToSearch` = ? LIMIT 1");
    $stmt->bind_param("s", $query);
    $stmt->execute();
    $result = $stmt->get_result();
    if($result->num_rows){
        $row = $result->fetch_array()
        echo $row['nameData'];
    }else{
        echo "No result found";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题