dppfxf909679 2016-08-19 02:38
浏览 38
已采纳

将MySQLi查询结果存储在PHP变量中

All i want to do is to store the name of the guest that is on the A1 seat in a PHP variable named "resulted".

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'test';
$con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
if ($con->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


$resulted = mysqli_query("SELECT name FROM guests WHERE seat='A1');

echo $resulted;
?>

I know this is totally wrong but I don't know how i shoud do it....

  • 写回答

2条回答 默认 最新

  • dongxu8533486 2016-08-19 03:08
    关注

    There is some issue in your code. I have fetched associative array amd print gust name.

        $dbhost = 'localhost';
        $dbuser = 'root';
        $dbpass = 'root';
        $dbname = 'test';
        $con=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
        if ($con->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
    
    
        $resulted = mysqli_query($conn, "SELECT name FROM guest WHERE seat='A1'");
        if(mysqli_num_rows($resulted) > 0) {
         $row = mysqli_fetch_assoc($resulted);
          print_r($row['name']);
        }
    
    
        // Free result set
        mysqli_free_result($resulted);
    
        mysqli_close($con);
    
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件