mysqli_query,mysqli_fetch_array和while循环

时间:2022-02-06 20:14:06

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.

我是PHP的新手,我正在尝试使用PHP构建一个网站。我有localhost用于测试结果,我已经在网站上安装了phpmyadmin。

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.

我现在要做的是从数据库“portal”列出我的表“property”的内容,并用结果填充表格。

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

我正在使用mysqli_query,mysqli_fetch_array和while循环。我收到以下错误:

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

警告:mysqli_fetch_array()要求参数1为mysqli_result,布尔值在第15行的C:\ xampp \ htdocs \ falcon \ portal \ forms \ edit listing.php中给出

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>';

6 个解决方案

#1


2  

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

用此替换您的查询。确保之前已添加此行。

$db = mysql_select_db('portal');

$sqlCommand = "SELECT * FROM property"; 

#2


4  

Your query is wrong, so after

你的查询错了,所以之后

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

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

$ query是false。这就是为什么,你得到错误。

The correct SQL Query is:

正确的SQL查询是:

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

您应该检查$ query是否存在

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

#3


2  

It should be

它应该是

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

Or simply use

或者只是使用

$sqlCommand = "SELECT * FROM property";

#4


1  

Your SQL statement

你的SQL语句

SELECT * property FROM portal

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

是不正确的SQL,因此查询不会被执行。尝试删除单词属性以获得一些结果。

#5


1  

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");

#6


1  

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

问题是SQL语句中出现语法错误,导致mysqli_query()返回false。

SELECT * property FROM portal is not valid SQL.

SELECT *属性FROM门户是无效的SQL。

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

您应该始终检查以确保mysqli_query返回一个有效结果,如下所示:

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

// use result here.....

#1


2  

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

用此替换您的查询。确保之前已添加此行。

$db = mysql_select_db('portal');

$sqlCommand = "SELECT * FROM property"; 

#2


4  

Your query is wrong, so after

你的查询错了,所以之后

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

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

$ query是false。这就是为什么,你得到错误。

The correct SQL Query is:

正确的SQL查询是:

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

您应该检查$ query是否存在

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

#3


2  

It should be

它应该是

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

Or simply use

或者只是使用

$sqlCommand = "SELECT * FROM property";

#4


1  

Your SQL statement

你的SQL语句

SELECT * property FROM portal

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

是不正确的SQL,因此查询不会被执行。尝试删除单词属性以获得一些结果。

#5


1  

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");

#6


1  

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

问题是SQL语句中出现语法错误,导致mysqli_query()返回false。

SELECT * property FROM portal is not valid SQL.

SELECT *属性FROM门户是无效的SQL。

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

您应该始终检查以确保mysqli_query返回一个有效结果,如下所示:

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

// use result here.....