简体   繁体   English

试图获取非对象PHP的属性

[英]Trying to get property of non-object PHP

The code worked properly till yesterday... Then wamp crashed... I reinstalled wamp and its throwing the error 该代码正常工作到昨天...然后wamp崩溃了...我重新安装了wamp并抛出错误

Trying to get property of non-object 试图获取非对象的属性

$con=mysqli_connect('localhost' ,'root' ,'' ,'images');

$result="SELECT * from images";
$stmt=$con->query($result);
if ($stmt->num_rows>0)
{
    while ($row=mysqli_fetch_array($stmt))
    {
        echo '<img height="300" width="300" src="data:image;base64,'.$row[2].' "> ';
    } 

First check weather your $stmt have something inside before accessing its childs. 首先检查您的$ stmt是否有东西在进入它的孩子之前。

var_dump($stmt); var_dump($ stmt);

The two objects in the you code you are trying to use a property is $stmt->num_rows . 您尝试使用属性的代码中的两个对象是$stmt->num_rows

It seems something wrong in the db. 在数据库中似乎出了点问题。
Could you check the connection and the db? 您可以检查连接和数据库吗?
Maybe when you reinstall you clean the db. 也许当您重新安装时清理数据库。

According to your code the error seem correct because you get output in form of object just use 根据您的代码,错误似乎是正确的,因为您仅以对象的形式获取输出

$con=mysqli_connect('localhost' ,'root' ,'' ,'images');

$result="SELECT * from images";
$stmt=$con->query($result);
if ($stmt->num_rows>0)
{
    while ($row=mysqli_fetch_array->($stmt))
    {
        echo '<img height="300" width="300" src="data:image;base64,'.$row[2].' "> ';
    } 

I would expect that when you reinstalled you did not re-create/restore the images database. 我希望重新安装时不会重新创建/还原images数据库。 But adding the error checking that should have been there in the first place should identify the problem. 但是首先添加应该已经在其中的错误检查应该可以识别问题。

Also you should stick to Proceedural or Object oriented mysqli and not mix the 2 as you have done. 另外,您应该坚持Proceedural或面向对象的mysqli,不要像已经完成的那样混合使用2。

$con = new mysqli_connect('localhost' ,'root' ,'' ,'images');
if ($con->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

$sql = "SELECT * from images";

// $con->query returns a result object
$result = $con->query($result);
if ( ! $result ) {
   die("Error: %s.\n", $stmt->error);
}

if ($result->num_rows > 0)
{
    while ($row = $result->fetch_assoc())
    {
        echo '<img height="300" width="300" src="data:image;base64,'.$row[2].' "> ';
    } 

This will now at least show you any errors that happen during the processing of the database. 现在,这至少将向您显示在处理数据库期间发生的任何错误。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM