php 使用php从数据库中返回一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3866581/
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
returing one value from the database using php
提问by djd
How do I fetch only one value from a database using PHP?
I tried searching almost everywhere but don't seem to find solution for these
e.g., for what I am trying to do is
如何使用 PHP 从数据库中仅获取一个值?
我尝试几乎到处搜索,但似乎没有找到解决这些问题的方法,例如,我想做的是
"SELECT name FROM TABLE
WHERE UNIQUE_ID=Some unique ID"
采纳答案by GuruC
You can fetch one value from the table using this query :
您可以使用以下查询从表中获取一个值:
"SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID limit 1"
"SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID limit 1"
Notice the use of limit 1in the query. I hope it helps!!
请注意在查询中使用了限制 1。我希望它有帮助!!
回答by KoolKabin
how about following php code:
下面的php代码怎么样:
$strSQL = "SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID";
$result = mysql_query($strSQL) or die('SQL Error :: '.mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['name'];
I hope it give ur desired name.
我希望它给你想要的名字。
Steps:
1.) Prepare SQL Statement.
2.) Query db and store the resultset in a variable
3.) fetch the first row of resultset in next variable
4.) print the desire column
步骤:
1.) 准备 SQL 语句。
2.) 查询数据库并将结果集存储在变量中
3.) 在下一个变量中获取结果集的第一行
4.) 打印期望列
回答by stevendesu
Here's the basic idea from start to finish:
这是从头到尾的基本思想:
<?php
$db = mysql_connect("mysql.mysite.com", "username", "password");
mysql_select_db("database", $db);
$result = mysql_query("SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID");
$data = mysql_fetch_row($result);
echo $data["name"];
?>
回答by Gihan Fernando
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["name"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();