php 从MYSQL查询php中选择一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10605456/
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 One row from MYSQL Query php
提问by 1321941
I have the following query:
我有以下查询:
$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
$row = mysql_fetch_array($result);
print_r ($row);
and the output I am getting is:
我得到的输出是:
Resource id #2
资源 ID #2
Ultimately, I want to be able to echo out a signle field like so:
最终,我希望能够像这样回显一个信号场:
$row['option_value']
Without having to use a while loop, as since I am only trying to get one field I do not see the point.
不必使用 while 循环,因为我只是想获取一个字段,所以我不明白这一点。
I have tried using mysql_result with no luck either.
我也尝试过使用 mysql_result 也没有运气。
Where am I going wrong?
我哪里错了?
Please do not lecture me about PDO or mysqli.
请不要教我 PDO 或 mysqli。
回答by Moyed Ansari
Try with mysql_fetch_assoc.It will returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows. Furthermore, you have to add LIMIT 1if you really expect single row.
尝试使用mysql_fetch_assoc 。它将返回与获取的行相对应的字符串关联数组,如果没有更多行,则返回 FALSE。此外,如果您真的期望单行,则必须添加LIMIT 1。
$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage' LIMIT 1");
$row = mysql_fetch_assoc($result);
echo $row['option_value'];
回答by Phix
$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
$row = mysql_fetch_assoc($result);
echo $row['option_value'];
回答by Tajni
Functions mysql_are not supported any longer and have been removed in PHP 7. You must use mysqli_instead.
函数mysql_不再受支持,并且已在PHP 7. 您必须mysqli_改用。
$result = mysqli_query($con, "SELECT option_value FROM wp_10_options WHERE option_name='homepage' LIMIT 1");
$row = mysqli_fetch_assoc($result);
echo $row['option_value'];
回答by Ehsan Khodarahmi
use mysql_fetch_assocto fetch the result at an associated array instead of mysql_fetch_arraywhich returns a numeric indexed array.
用于mysql_fetch_assoc在关联数组中获取结果,而不是mysql_fetch_array返回数字索引数组。
回答by Daniel Pecher
Ultimately, I want to be able to echo out a signle field like so:
$row['option_value']
最终,我希望能够像这样回显一个信号场:
$row['option_value']
So why don't you? It should work.
那你为什么不呢?它应该工作。
回答by Narf
What you should get as output with this code is:
您应该使用此代码获得的输出是:
Array ()
... this is exactly how you get just one row, you don't need a while loop. Are you sure you're printing the right variable?
...这正是您获得一行的方式,您不需要 while 循环。您确定要打印正确的变量吗?
回答by Deepak Ghayal
It is working for me..
它对我有用..
$show = mysql_query("SELECT data FROM wp_10_options WHERE
option_name='homepage' limit 1"); $row = mysql_fetch_assoc($show);
echo $row['data'];
回答by Asuquo12
Though mysql_fetch_arraywill output numbers, its used to handle a large chunk.
To echo the content of the row, use
虽然mysql_fetch_array会输出数字,但它用于处理大块。要回显行的内容,请使用
echo $row['option_value'];
回答by Kamil
is this is a WordPress?
You shouldn't do it like you've done!
To get option from DB use get_option!
这是一个WordPress吗?你不应该像你那样做!要从 DB 获取选项,请使用get_option!
回答by soma
this shoude work
这手工作
<?php
require_once('connection.php');
//fetch table rows from mysql db
$sql = "select id,fname,lname,sms,phone from data";
$result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));
//create an array
$emparray = array();
for ($i = 0; $i < 1; $i++) {
$row =mysqli_fetch_assoc($result);
} $emparray[] = $row;
echo $emparray ;
mysqli_close($connection);
?>

