简体   繁体   English

简单的MySQL查询不起作用

[英]Simple mysql query not working

I have this very simple function: 我有这个非常简单的功能:

function getCatName($id){
$sql = "SELECT * FROM biznet_category WHERE ID ='".$id."';";
$res = mysql_query ($sql) or die (mysql_error ());
$row = mysql_fetch_assoc ($res);
$name = $row["Name"];
return $name;
}

So with this function I should be able to get the category name, but it doesn't work with the parameter. 因此,使用此功能,我应该能够获得类别名称,但是它不适用于该参数。 If I put 8 or 9, the categoryname is displayed correctly. 如果输入8或9,则正确显示类别名称。

The id is also passed on like it should, when I print it out, it shows 8 or 9. id也会像应该传递的那样传递,当我打印出来时,它显示8或9。

I know the solution is quite simple, I just don't see it. 我知道解决方案很简单,只是看不到。

To fix remove the quotes and check the column name for case id or ID. 要解决此问题,请删除引号,并检查列名以获取案例ID或ID。 Since the query string is in double quotes you don't have to use the . 由于查询字符串用双引号引起来,因此您不必使用。 join 加入

$sql = "SELECT * FROM biznet_category WHERE ID = $id";

You can use curly brackets which I find easier to read 您可以使用大括号,我觉得它更易于阅读

$sql = "SELECT * FROM biznet_category WHERE ID = {$id}";

If you were querying a string rather than an integer you can simply do 如果您要查询的是字符串而不是整数,则只需

$sql = "SELECT * FROM biznet_category WHERE ID = '{$id}'";
$sql = "SELECT * FROM biznet_category WHERE ID ='".$id."';";

$sql = "SELECT * FROM biznet_category WHERE ID = ".$id;

尝试这个

$sql = "SELECT * FROM biznet_category WHERE ID = ".$id;

列名ID的拼写是否正确?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM