gpt4 book ai didi

php - MYSQL查询每次都出错

转载 作者:行者123 更新时间:2023-11-29 23:14:34 24 4
gpt4 key购买 nike

我尝试使用下拉列表从我上传的数据库中提取神奇宝贝信息,但不断收到以下错误:

No Pokemon was requestedDatabase access failed: 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 'WHERE pokemon_name = SELECT * FROM pokemon_info' at line 1

我当前有一个包含标题的数据库:

id |神奇宝贝名称 |高度|重量 |动图

我实在不明白为什么......我的处理代码如下;

// CODE TO QUERY DATABASE TO GO HERE

// Capture form data, if anything was submitted

if (isset($_POST['pokemon_submit'])) {
$pokemon_submit = clean_string($db_server, $_POST['submit']);

// create the SQL query

$query = "SELECT * FROM pokemon_info where pokemon_info=$pokemon_submit";

// query the database

mysqli_select_db($db_server, $db_database);

$result = mysqli_query($db_server, $query);

if (!$result) die("Database access failed: " . mysqli_error($db_server));

// if there are any rows, print out the contents

if ($row = mysqli_fetch_array($result)) {
$output .= "Pokemon: " . $row['pokemon_name'] . ", Gif: " .

$row['gif'] . "<br />";
}

else {
$output = 'Well, you must have invented a new Pokémon, cause it is not on this website!';
}

mysqli_free_result($result);
}
else {
$output = 'No Pokemon was requested';
}

// CODE TO QUERY END

}

// Close connection!
// YOUR CODE HERE BIT end

echo $output;

$output = '';

mysqli_select_db($db_server, $db_database);

$query = "WHERE pokemon_name = $pokemon_submit SELECT * FROM pokemon_info";

$result = mysqli_query($db_server, $query);

if (!$result) die("Database access failed: " . mysqli_error($db_server));

while($row = mysqli_fetch_array($result)){

$output .= "Pokemon: " . $row['pokemon_name'] . ", Gif: " .

$row['gif'] . "<br />";

}

mysqli_free_result($result);

echo $output;

感谢您的帮助,我现在已经修改了,它说没有请求任何神奇宝贝。

其基本思想是有一个带有神奇宝贝下拉列表的主页。当用户从列表中进行选择时,将显示存储在我的数据库中的有关该特定神奇宝贝的信息。

下拉列表直接链接到我数据库中的 pokemon_name 列。我不明白为什么它返回时好像没有选择任何内容?

非常感谢大家的帮助,我在这里学到的东西比在论坛中搜索的要多得多。

最佳答案

存在多个问题

  $query = "SELECT * FROM pokemon_info where pokemon_name=$pokemon_submit"; 

如果 pokemon_submit 是一个数字 - 那就可以了。如果它是一个字符串那么你需要$query = "SELECT * FROM pokemon_info where pokemon_name='$pokemon_submit'";注意单引号。

$query = "WHERE pokemon_name = $pokemon_submit SELECT * FROM pokemon_info";
$result = mysqli_query($db_server, $query);

我不知道你在这里尝试什么?据我所知没有以“WHERE”开头的 SQL 语句

关于php - MYSQL查询每次都出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27909039/

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