Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli_query, mysqli_fetch_array and while loop

Tags:

php

mysql

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>';
like image 441
Omar Avatar asked Oct 24 '12 16:10

Omar


People also ask

What is mysqli_query () used for?

Definition and Usage. The query() / mysqli_query() function performs a query against a database.

What does Mysqli_fetch_array mean?

Definition and Usage The fetch_array() / mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both. Note: Fieldnames returned from this function are case-sensitive.

What is the purpose of mysqli_query () in web development?

The mysqli_query function is used to execute SQL queries. The function can be used to execute the following query types; Insert. Select.

What is the difference between Fetch_assoc and Fetch_array?

fetch_array returns value with indexing. But Fetch_assoc just returns the the value. here array location 0 contains 11 also this location name is 'id'. means just returns the value.


2 Answers

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)) {
...
like image 73
FlorinelChis Avatar answered Sep 28 '22 02:09

FlorinelChis


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

$db = mysql_select_db('portal');

$sqlCommand = "SELECT * FROM property"; 
like image 22
Abhishek Saha Avatar answered Sep 28 '22 00:09

Abhishek Saha