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

时间:2016-08-19 02:38:49

标签: php mysqli

我想要做的就是将名为&#34的PHP变量存放在A1座位上的guest虚拟机的名称;结果为#34;。

<?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;
?>

我知道这是完全错的,但我不知道我该怎么做......

2 个答案:

答案 0 :(得分:0)

$('input[type=checkbox]').on('change', function() { var $this = $(this); // option #1 //$this.closest('tr').find('select.you-are-my-select').prop('disabled', $this.is(':checked')); // option #2, if the select always placed (for example) in third column $this.closest('tr').find('> td:nth-child(3) select').prop('disabled', $this.is(':checked')); }); 是一个mysqli资源。您需要从此资源中获取一行,如下所示:

$resulted

有关详细信息,请访问http://www.w3schools.com/php/func_mysqli_fetch_assoc.asp

答案 1 :(得分:-1)

您的代码中存在一些问题。我已经获取了关联数组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);

    ?>