-2

I want to create a table with PHP, but it won't work... Here is my code:

<!DOCTYPE html>
<html>
<head>
<title>Table with PHP</title>
</head>
<body>

<?php
$dblink=mysqli_connect('IP','username','password');
if (mysqli_connect_errno())
{
    echo "Error: Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_select_db($dblink,'database');

$abfrage='$dblink,"SELECT * FROM database"';
$ergebnis='mysqli_query($abfrage)';
?>

<table width="1000" cellpadding="0" cellspacing="0" border="0">
<tr>
<th>var1</th>
<th>var2</th>
<th>var3</th>
</tr>

The error is somewhere here

<?php

while($row='mysqli_fetch_object($ergebnis)'){
echo '<tr>'."\r\n";

echo '<td>'.$row->var1.'</td>'."\r\n";
echo '<td>'.$row->var2.'</td>'."\r\n";
echo '<td>'.$row->var3.'</td>'."\r\n";

echo '<tr>'."\r\n";
}

?> 

</body>
</html>

The errors are:

Notice: Trying to get property 'var1' of non-object in (Path) on line xx

Notice: Trying to get property 'var2' of non-object in (Path) on line xx

Notice: Trying to get property 'var3' of non-object in (Path) on line xx

Community
  • 1
  • 1
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – James May 19 '18 at 12:08
  • do you have a table named `database` ? – Goms May 19 '18 at 12:09
  • 5
    `$row` is a *string*, it doesn't have any properties. You are randomly enclosing code in quotes, which makes it a string. You're doing this in several places. – David May 19 '18 at 12:10
  • if you are create table using query then where is you create table query. like $query = CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... ); ??? – Priyanka Maurya May 19 '18 at 12:22

1 Answers1

2

$ergebnis='mysqli_query($abfrage)';
and $row='mysqli_fetch_object($ergebnis)' should not be quoted, since now you're not querying the database, but just declaring strings.

It should be $ergebnis=mysqli_query($dblink, $abfrage); and $row=mysqli_fetch_object($ergebnis). Also $abfrage='$dblink,"SELECT * FROM database"' should be $abfrage="SELECT * FROM table_name" where table_name is the name of the table you want to query.

An example on how to query a database using php can be found here.

MichaelK
  • 352
  • 4
  • 11
  • Than the errors are: **Warning:** mysqli_query() expects at least 2 parameters, 1 given in "Path" on line xx **Warning:** mysqli_fetch_object() expects parameter 1 to be mysqli_result, null given in "Path" on line xx – Harald Foster May 21 '18 at 12:39
  • Im sorry, I missed a few more problems with your code. I've edited my answer. – MichaelK May 22 '18 at 19:32
  • Hi Michael, thank you for your answer. Most of the errors are fixed now but one is still there. It's the line with the $row variable. I post the error here: **Warning:** mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given in "Path" on line xx – Harald Foster May 23 '18 at 11:18
  • This is because mysqli_query($abfrage) is failing (and returning false because of that); in other words there is an error in your SQL statement. Unfortunately I can't tell what exactly is wrong since I don't know the schema of your database. PHP manual: http://php.net/manual/en/mysqli.query.php#refsect1-mysqli.query-returnvalues – MichaelK May 23 '18 at 12:38