Jump to content

Notice: Trying to get property of non-object on Localhost


neonio

Recommended Posts

Hi, i just joined the forum.

 

i'm having trouble with a php script, i hope you can help me, cause i've searched through google a lot and found nothing.

 

this is the script:

 

<?php
  
  $db = new mysqli('localhost', 'user', 'pass', 'database');
  if (mysqli_connect_errno()) 
    {
    echo 'Fail';
    exit;
    }
  $query = "SELECT * FROM 'test'";
  $result = $db->query($query);
  $num_results = $result->num_rows;
  echo "Number of entries: ".$num_results."<br />";
  echo "<table border = '1'> \n";
  for ($i=0; $i<$num_results; $i++)
    {
    $row=$result->fetch_assoc();
    echo "<tr> \n";
    echo "<td>".$row["column1"]." / ".$row["column2"]."</td> \n";
    echo "<td>".$row["column3"]."</td>\n";
    echo "<td>".$row["column4"]."</td>\n";
    echo "</tr> \n";
    }
  echo "</table> \n";
?> 

 

it gives the following error: Notice: Trying to get property of non-object in C:\apache\htdocs\showbd.php on line 11 which is $num_results = $result->num_rows;

 

i'm using PHP 5.3.2, Apache 2.2.15, Mysql 5.1.48 and windows xp sp3, also i've enabled php extensions mysql and mysqli.

 

i really don't know what could be wrong.

 

 

I don't actually use mysqli, but the error message says $result is not an object. Therefore, $db->query() failed. That generally means there is a syntax error in your SQL statement.

 

$query = "SELECT * FROM 'test'";

the table name does not be surrounded by single-quotes.

 

$query = "SELECT * FROM test";

should be correct.

 

If you not "quotes" around a table (or column) name, use back-ticks:

$query = "SELECT * FROM `test`";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.