PHP / MYSQL 选择使用 mysqli_query

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20875113/
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 03:38:06  来源:igfitidea点击:

PHP / MYSQL select using mysqli_query

phphtmlmysqlmysqli

提问by user3146788

I am struggling with my script and since switching to mysqli it will no longer work.I have checked the PHP manual but just cannot see what I am doing wrong, surely a beginners mistake.

我正在努力处理我的脚本,自从切换到 mysqli 后,它将不再起作用。我检查了 PHP 手册,但看不到我做错了什么,这肯定是初学者的错误。

Here is my code:

这是我的代码:

<?php

//Connect to database
include ('connection.php');

// Retrieve all the data from the table
$result = mysqli_query("SELECT * FROM gear") or die(mysqli_error()); 

echo "<table border='1'>";
echo "<tr> <th>Manufacturer</th> <th>Model</th> <th>Description</th> </tr>";
// keeps getting the next row until there are no more to get
while($gear = mysqli_fetch_array( $result )) {

// Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $gear['manu'];
    echo "</td><td>"; 
    echo $gear['model'];
    echo "</td><td>"; 
    echo $gear['desc'];
    echo "</td></tr>"; 

} 
echo "</table>";

?>

I am wondering if it is because of the fact that I am using another script to connect but it is complaining about my mysqli_query so I am getting this error:

我想知道是否是因为我使用另一个脚本进行连接但它抱怨我的 mysqli_query 所以我收到这个错误:

[Wed Jan 01 21:14:54 2014] [error] [client ::1] PHP Warning: mysqli_error() expects exactly 1 parameter, 0 given in /var/www/eml/includes/query_gear.php on line 7

[Wed Jan 01 21:14:54 2014] [error] [client ::1] PHP 警告:mysqli_error() 需要 1 个参数,0 在第 7 行的 /var/www/eml/includes/query_gear.php 中给出

Any advice or suggestions would be appreciated.

任何意见或建议将不胜感激。

回答by John Conde

You are missing your resource identifier obtained with mysqli_connect()which is required with mysqli_* and not with mysql_*. Assuming you called yours $link:

您缺少mysqli_connect()使用 mysqli_* 而不是 mysql_* 所需的资源标识符。假设你打电话给你的$link

$result = mysqli_query($link, "SELECT * FROM gear") or die(mysqli_error($link));