php 如果 id 匹配,则从 mysql 中选择行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5110021/
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
selecting row from mysql if id matches
提问by Jay
I want to select a row from mysql which matches a specific id. I want to get the result if the ID matches, if the ID does not exists in the database, it should not do anything.
我想从 mysql 中选择与特定 id 匹配的行。如果 ID 匹配,我想得到结果,如果 ID 在数据库中不存在,则不应执行任何操作。
I run sth like this:
我是这样运行的:
$q = "SELECT * FROM entries where id= '1'";
$result = mysql_query($q) or die(mysql_error());
if($result){
$row = mysql_fetch_array($result) or die(mysql_error());
$name = $row['name'];
}
echo "hello".$name;
If the id '1' exists in the db, it should get the name, otherwise nothing or at least it should give the error, but when i use this, it just display no any content of the page which comes after this code. What I'm doing wrong?
如果 id '1' 存在于 db 中,它应该获取名称,否则什么也没有,或者至少它应该给出错误,但是当我使用它时,它只是不显示此代码之后的页面的任何内容。我做错了什么?
回答by Andresch Serj
If it does not display any code after this code, this is probably due to an error occuring and your error handling being set so the error is not displayed.
如果在此代码之后没有显示任何代码,这可能是由于发生了错误并且您的错误处理被设置为不显示错误。
Try searching for the php error log file (normaly php_error.log) that should contain the error that you do not see.
尝试搜索 php 错误日志文件(通常为 php_error.log),该文件应包含您看不到的错误。
Another thing i would try is adding more echo statements to see where exactly php stops interpreting. Like this:
我会尝试的另一件事是添加更多 echo 语句以查看 php 停止解释的确切位置。像这样:
$q = "SELECT * FROM entries where id= '1'";
$result = mysql_query($q);
echo '<br />Query is send';
if(!$result) {
die('<br/>MySQL Error: ' . mysql_error());
}
else {
echo '<br />Result is true';
$row = mysql_fetch_array($result);
echo '<br />tryed fetching row';
if ($row === FALSE) {
echo '<br />$row is not false.';
$name = $row['name'];
echo '<br />$name now is "' . $name . '"';
}
else {
die('<br/>MySQL Error: ' . mysql_error());
}
}
echo '<br />hello: "' . $name . '"';
That might help to get some more information about your problem.
这可能有助于获得有关您的问题的更多信息。
回答by Phoenix
$id = 1;
$sql = "SELECT `name` FROM `entries` WHERE `id` = $id LIMIT 1" //Since the id is probably an integer type on the DB, the single quotes aren't necessary, and sometimes screw it up. I think MySQL possibly thinks it's a string when in quotes.
$result = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result))
{
$row = mysql_fetch_assoc($result) or die(mysql_error());
$name = $row['name'];
echo 'Hello ' . $name;
}
回答by Blagovest Buyukliev
A SELECT
query can return 0 rows if the condition you specified doesn't match any rows, and that isn't an error.
一个SELECT
查询可以返回0行,如果你指定的条件不匹配任何行,这是不是一个错误。
You should rather check the result of mysql_num_rows()
after sending the query.
您应该mysql_num_rows()
在发送查询后检查结果。
回答by Phaenotyp
Maybe you should add a else statement to your if.
也许你应该在你的 if 中添加一个 else 语句。
if($result)
....
else
do something
You might even want to do a try catch statement.
您甚至可能想要执行 try catch 语句。