简体   繁体   English

PHP MySQL单列/行显示?

[英]PHP MySQL single column/row display?


I'm trying to get a single result from my database, just one name. 我想从我的数据库中获取一个结果,只有一个名字。
I tried using; 我试过用;

$row = mysql_fetch_array(mysql_query("SELECT * FROM persons WHERE id = '$id'"));
echo $row['name'];

But that din't work, any other way to simply show only one result? 但那不起作用,任何其他方式只显示一个结果?
Thanks in advance! 提前致谢!

[EDIT:] [编辑:]
(I'm using PHP 5.3) (我使用的是PHP 5.3)

<?php
include("connection.php");
$id = $_GET['deletid'];
$result = mysql_query("SELECT * FROM persons WHERE id = '$id' LIMIT 1");
if(!$result){
    echo mysql_error();
}
if ($row = mysql_fetch_array($result)){
    echo $row['name'];
}
echo "<p>id:$id</p>";
?>

If you need just the name and you need just one result you should rewrite your query as follow: 如果您只需要名称并且只需要一个结果,则应该重写您的查询,如下所示:

$row = mysql_fetch_array(mysql_query("SELECT name FROM persons WHERE id = '". (int) $id ."' LIMIT 1"));

Now to get the result you should just get it with a 现在要得到结果你应该得到它

$row['name'];

EDIT Now that you posted your entire code i got what's wrong: You are deleting that result before getting its name. 编辑现在您发布了整个代码我得到了错误:您在获取其名称之前删除了该结果。 Basically you delete that user and then you attempt to get its name. 基本上你删除该用户然后尝试获取其名称。 EDIT 编辑

<?php
include("connection.php");
    if (empty($_GET['deleteid'])) { 
        exit('"deleteid" is empty'); 
    }
$id = mysql_real_escape_string($_GET['deletid']);
$result = mysql_query("SELECT name FROM persons WHERE id = '". (int) $id ."' LIMIT 1");
    if(!$result){
        echo mysql_error();
    }
$row = mysql_fetch_assoc($result); // for just one result you don't need of any loop
echo $row['name'];

echo "<p>id:". htmlspecialchars($id) ."</p>";
?>

try 尝试

$row = mysql_fetch_array(mysql_query("SELECT name FROM persons WHERE id = ". (int) $id));

echo $row['name'];

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

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