如果id匹配,则从mysql中选择行 [英] selecting row from mysql if id matches

查看:53
本文介绍了如果id匹配,则从mysql中选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从mysql中选择与特定ID匹配的行.如果ID匹配,我想获取结果;如果ID在数据库中不存在,则不应执行任何操作.

我这样运行:

$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;

如果在数据库中存在id'1',它应该得到名称,否则什么也不是,至少它应该给出错误,但是当我使用它时,它只显示此之后的页面的任何内容代码.我在做什么错了?

解决方案

如果在此代码后未显示任何代码,则可能是由于发生了错误,并且已设置了错误处理,因此不显示错误. /p>

尝试搜索应包含未显示的错误的php错误日志文件(通常为php_error.log).

我要尝试的另一件事是添加更多的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 . '"';

这可能有助于获得有关您的问题的更多信息.

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.

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?

解决方案

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.

Another thing i would try is adding more echo statements to see where exactly php stops interpreting. Like this:

$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.

这篇关于如果id匹配,则从mysql中选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆