Omar
Omar

Reputation: 31732

mysqli_query, mysqli_fetch_array and while loop

I am new to PHP and I am trying to build a website using PHP. I have localhost for testing the result and I have phpmyadmin already installed on the website.

What i am trying to do now, is to list the contents of my table "property" from database "portal" and fill a table with the results.

I am using mysqli_query, mysqli_fetch_array and while loop. I'm getting the following error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\falcon\portal\forms\edit listing.php on line 15

session_start();
require_once "connect_to_mysql.php"; // where i store username and password to access    my db.

$sqlCommand = "SELECT * property FROM portal"; // dbname: portal - table: propery
$query = mysqli_query($myConnection, $sqlCommand);

$Displayproperty = '';
while ($row = mysqli_fetch_array($query))
$id = $row["pid"];
$title = $row["ptitle"];
$area = $row["parea"];
$city = $row["pcity"];
$Displayproperty .= '<table width="500" border="0" cellspacing="0" cellpadding="1">
<tr>
<td>' . $id . '</td>
<td>' . $title . '</td>
<td>' . $area . '</td>
<td>' . $city . '</td>
<td><a href="forms.php?pid=' . $id . '">Upload images</a><br /></td>
</tr>
</table>';

Upvotes: 6

Views: 60545

Answers (6)

Joe Day
Joe Day

Reputation: 7264

The issue is a syntax error in your SQL statement, which is causing mysqli_query() to return false.

SELECT * property FROM portal is not valid SQL.

You should always check to make sure mysqli_query returns a valid result with a construct like:

$result = mysqli_query($myConnection, $sqlCommand);
if(! $result) {
    die("SQL Error: " . mysqli_error($myConnection));
}

// use result here.....

Upvotes: 1

Shubhanshu Mishra
Shubhanshu Mishra

Reputation: 6700

You need to first connect to DB portal using:

$myConnection = new mysqli("localhost", "user", "password", "database");

Then run:

$mysqli->query("SELECT * FROM property"); // This will run the query on portal database.

If you want to simply query property table of portal you can use:

$mysqli->query("SELECT * FROM portal.property");

or

mysqli_query("SELECT * FROM portal.property");

Upvotes: 1

Abhishek Saha
Abhishek Saha

Reputation: 2564

Replace your query with this. Make sure you have added this line before.

$db = mysql_select_db('portal');

$sqlCommand = "SELECT * FROM property"; 

Upvotes: 2

FlorinelChis
FlorinelChis

Reputation: 1478

Your query is wrong, so after

$query = mysqli_query($myConnection, $sqlCommand);

$query is false. That's why, you get the error.

The correct SQL Query is:

SELECT * FROM portal.property

If you need to specify the database name. Also, before doing:

while ($row = mysqli_fetch_array($query))

You should check that $query exists

if(!empty($query) {
while ($row = mysqli_fetch_array($query)) {
...

Upvotes: 4

Mr. Alien
Mr. Alien

Reputation: 157334

It should be

$sqlCommand = "SELECT * FROM portal.property"; /* Database_Name.Table_Name */

Or simply use

$sqlCommand = "SELECT * FROM property";

Upvotes: 2

Bjoern
Bjoern

Reputation: 16304

Your SQL statement

SELECT * property FROM portal

is not correct sql, therefore the query doesn't get executed. Try removing the word property to get some results.

Upvotes: 1

Related Questions