简体   繁体   English

不能在php Mysql中使用mysqli_result类型的对象作为数组

[英]Cannot use object of type mysqli_result as array in php Mysql

I want to select the last row for my database and echo the columns titled s1 and s2 to the body of the web page, the following is my code.我想为我的数据库选择最后一行,并将标题为 s1 和 s2 的列回显到网页正文中,以下是我的代码。 This gives an error.这给出了一个错误。

<html>
<body>
<?php

$servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "mydb2";

            $conn = new mysqli($servername, $username, $password, $dbname);
            if ($conn->connect_error) {die("Connection failed: " . $conn->connect_error);
            } 
$sql = "SELECT s1, s2 from reading ORDER BY id DESC LIMIT 1";
$row = array();
$row = mysqli_query($conn,$sql);
echo " cup 1". $row["s1"]. "CUP 2". $row["s2"];
 ?>



</body>
</html>

You have to use mysqli_fetch_assoc (that will loop through your resultset) as follows:您必须使用 mysqli_fetch_assoc(它将遍历您的结果集),如下所示:

if ($result = mysqli_query($conn, $sql)) {

    while ($row = mysqli_fetch_assoc($result)) {
        echo " cup 1". $row["s1"]. "CUP 2". $row["s2"];
    }

    /* free result set */
    mysqli_free_result($result);
}

Without loop:无循环:

if ($result = mysqli_query($conn, $sql)) {

    $row = mysqli_fetch_assoc($result);
    if($row)
    {
       echo " cup 1". $row["s1"]. "CUP 2". $row["s2"];
    }

    /* free result set */
    mysqli_free_result($result);
}

Your solution您的解决方案

<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb2";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("MySQL Connection Error"); // Try not to output SQL error messages on the front-end, look into error_reporting()
}

$sqlQuery = "SELECT s1, s2 from reading ORDER BY id DESC LIMIT 1";
$results = mysqli_fetch_assoc(mysqli_query($conn, $sqlQuery));
echo " cup 1". $results["s1"]. "CUP 2". $results["s2"];
?>
</body>
</html>

In addition to that, I suggest not doing your SQL operations in both procedural style and object-oriented style, as this can lead to many complications in the future.除此之外,我建议不要同时以过程风格和面向对象风格进行 SQL 操作,因为这可能会导致未来的许多复杂情况。

Use mysqli_fetch_assoc for fetching the result to array使用mysqli_fetch_assoc获取结果到数组

$sql = "SELECT s1, s2 from reading ORDER BY id DESC LIMIT 1";
if ($result = mysqli_query($conn, $sql)) {
    $row = mysqli_fetch_assoc($result);
    echo " cup 1". $row["s1"]. "CUP 2". $row["s2"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mysqli错误:无法将mysqli_result类型的对象用作数组 - mysqli error : Cannot use object of type mysqli_result as array Mysqli依靠php“不能将mysqli_result类型的对象用作数组” - Mysqli count on php “Cannot use object of type mysqli_result as array” 无法将mysqli_result类型的对象用作数组错误 - Cannot use object of type mysqli_result as array error 致命错误:无法将mysqli_result类型的对象用作数组 - Fatal error: Cannot use object of type mysqli_result as array sql + php-致命错误:无法将mysqli_result类型的对象用作数组,为什么 - sql + php - Fatal error: Cannot use object of type mysqli_result as array why PHP:未捕获的错误无法将类型为mysqli_result的对象用作数组 - PHP: Uncaught Error Cannot use object of type mysqli_result as array 使用mysqli-&gt; fetch_assoc()-无法将mysqli_result类型的对象用作数组 - Using mysqli->fetch_assoc() - Cannot use object of type mysqli_result as array 不能将mysqli_result类型的对象用作数组是什么意思? - What does Cannot use object of type mysqli_result as array mean? 致命错误:无法在第97行上将mysqli_result类型的对象用作数组。 - Fatal error: Cannot use object of type mysqli_result as array in…on line 97 致命错误:不能将mysqli_result类型的对象用作C:\\ xampp \\中的数组 - Fatal error: Cannot use object of type mysqli_result as array in C:\xampp\
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM