gpt4 book ai didi

php - SQL 语法错误/无效查询 - WAMP、phpMyAdmin

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:12 25 4
gpt4 key购买 nike

我在 PHP 中使用 SQL 查询时遇到错误。我尝试了多个查询,也厌倦了使用 phpMyAdmin 生成的 PHP 代码。谁能帮我解决这个问题?

Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''sample_table' LIMIT 0, 30' at line 1

这是 PHP 代码:

//Connect to server
$connect = mysql_connect("localhost", "root", "");
if (!$connect) {
die('Not connected : ' . mysql_error());
}

//Connect to DB
$db_selected = mysql_select_db("testing", $connect);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}

$sql = "SELECT `Name` FROM `sample_table` LIMIT 0, 30 ";

//Query DB table
$sqlQuery = mysql_query($sql, $connect);
if (!$sqlQuery) {
die('Invalid query: ' . mysql_error());
}

//fetch the results / convert the results into an array
while($rows = mysql_fetch_array($sqlQuery, MYSQL_NUM)) //this is happening
{
$name = $rows['name'];
$age = $rows['age'];
$ID = $rows['ID'];

echo "$name<br/>$age<br/>$ID<br/>";
}

mysql_close($connect);

这是我创建的 phpMyAdmin 表的屏幕截图。该表中有数条记录。

phpMyAdmin Screenshot

最佳答案

改变这个:

$sql = "SELECT Name FROM 'sample_table' LIMIT 0, 30 ";

进入这个:

$sql = "SELECT `Name` FROM `sample_table` LIMIT 0, 30 ";

请注意,我已将 ' 更改为 `

顺便说一下,echo 语法应该是这样的:

 echo $name."<br/>".$age."<br/>".$ID."<br/>";

关于php - SQL 语法错误/无效查询 - WAMP、phpMyAdmin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11194414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com