php mysqli_query、mysqli_fetch_array 和 while 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13053660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:43:03  来源:igfitidea点击:

mysqli_query, mysqli_fetch_array and while loop

phpmysql

提问by Omar

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 构建网站。我有用于测试结果的本地主机,并且我已经在网站上安装了 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.

我现在要做的是从数据库“门户”中列出我的表“属性”的内容,并用结果填充表。

I am using mysqli_query, mysqli_fetch_arrayand 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,布尔值在 C:\xampp\htdocs\falcon\portal\forms\edit listing.php 第 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>';

采纳答案by Abhishek Saha

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

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

$db = mysql_select_db('portal');

$sqlCommand = "SELECT * FROM property"; 

回答by FlorinelChis

Your query is wrong, so after

你的查询是错误的,所以之后

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

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

$query 是假的。这就是为什么,你会得到错误。

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)) {
...

回答by Mr. Alien

It should be

它应该是

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

Or simply use

或者干脆使用

$sqlCommand = "SELECT * FROM property";

回答by Joe Day

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 portalis not valid SQL.

SELECT * property FROM portal不是有效的 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.....

回答by Bjoern

Your SQL statement

你的 SQL 语句

SELECT * property FROM portal

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

是不正确的 sql,因此不会执行查询。尝试删除这个词 property以获得一些结果。

回答by Shubhanshu Mishra

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