从MYSQL查询php中选择一行。

时间:2022-03-01 22:38:21

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。

11 个解决方案

#1


21  

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 1 if 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'];

#2


4  

$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
$row = mysql_fetch_assoc($result);
echo $row['option_value'];

#3


1  

use mysql_fetch_assoc to fetch the result at an associated array instead of mysql_fetch_array which returns a numeric indexed array.

使用mysql_fetch_assoc获取关联数组的结果,而不是返回数字索引数组的mysql_fetch_array。

#4


0  

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循环。你确定你打印的是正确的变量吗?

#5


0  

Ultimately, I want to be able to echo out a signle field like so:

最后,我希望能够像这样回应一个信号领域:

   $row['option_value']

So why don't you? It should work.

那么你为什么不呢?它应该工作。

#6


0  

Though mysql_fetch_array will output numbers, its used to handle a large chunk. To echo the content of the row, use

虽然mysql_fetch_array将输出数字,但它用于处理大块。要回显行内容,请使用

echo $row['option_value'];

#7


0  

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!

#8


0  

this shoude work

这shoude工作

    <?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);
?>

#9


0  

Try this one if you want to pick only one option value.

如果您只想选择一个选项值,请尝试这个。

$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
$row = mysql_fetch_array($result);
echo $row['option_value'];

#10


0  

make sure your ftp transfers are in binary mode.

确保你的ftp传输处于二进制模式。

#11


-1  

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'];

#1


21  

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 1 if 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'];

#2


4  

$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
$row = mysql_fetch_assoc($result);
echo $row['option_value'];

#3


1  

use mysql_fetch_assoc to fetch the result at an associated array instead of mysql_fetch_array which returns a numeric indexed array.

使用mysql_fetch_assoc获取关联数组的结果,而不是返回数字索引数组的mysql_fetch_array。

#4


0  

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循环。你确定你打印的是正确的变量吗?

#5


0  

Ultimately, I want to be able to echo out a signle field like so:

最后,我希望能够像这样回应一个信号领域:

   $row['option_value']

So why don't you? It should work.

那么你为什么不呢?它应该工作。

#6


0  

Though mysql_fetch_array will output numbers, its used to handle a large chunk. To echo the content of the row, use

虽然mysql_fetch_array将输出数字,但它用于处理大块。要回显行内容,请使用

echo $row['option_value'];

#7


0  

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!

#8


0  

this shoude work

这shoude工作

    <?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);
?>

#9


0  

Try this one if you want to pick only one option value.

如果您只想选择一个选项值,请尝试这个。

$result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'");
$row = mysql_fetch_array($result);
echo $row['option_value'];

#10


0  

make sure your ftp transfers are in binary mode.

确保你的ftp传输处于二进制模式。

#11


-1  

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'];