Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打印时有';在MySQL/PHP中只有一条记录_Php_Mysql - Fatal编程技术网

打印时有';在MySQL/PHP中只有一条记录

打印时有';在MySQL/PHP中只有一条记录,php,mysql,Php,Mysql,当您知道只有一条记录时,使用PHP从MySQL打印的最佳方式是什么 我的SQL语句是: select user from users where user = 'norman'; 这将只返回一条记录。那么打印它的最佳方式是什么?我目前: while ($info=mysql_fetch_assoc($data)) 等 但这对不止一张唱片来说是可以的。当只有一个数据库时,还有更好的方法吗?$row=mysql\u fetch\u assoc(mysql\u query($sql)) 然后做你想

当您知道只有一条记录时,使用PHP从MySQL打印的最佳方式是什么

我的SQL语句是:

select user from users where user = 'norman';
这将只返回一条记录。那么打印它的最佳方式是什么?我目前:

while ($info=mysql_fetch_assoc($data))


但这对不止一张唱片来说是可以的。当只有一个数据库时,还有更好的方法吗?

$row=mysql\u fetch\u assoc(mysql\u query($sql))

然后做你想做的:

echo$row['value']

您需要提前知道这将返回一行。您可以使用以下函数:

function fetch_single_row($sql){
  $result = mysql_query($sql);
  if (mysql_num_rows($result) > 1){
    return false;
  }
  return mysql_fetch_assoc($result);
}

$row=mysql\u fetch\u assoc(mysql\u query($sql))

然后做你想做的:

echo$row['value']

您需要提前知道这将返回一行。您可以使用以下函数:

function fetch_single_row($sql){
  $result = mysql_query($sql);
  if (mysql_num_rows($result) > 1){
    return false;
  }
  return mysql_fetch_assoc($result);
}

如果您完全确定此查询将始终检索1行,那么这就足够了:

$row = mysql_fetch_assoc(mysql_query($sql));

然后,您可以随意操作$row(单行)。

如果您完全确定此查询将始终检索1行,那么这就足够了:

$row = mysql_fetch_assoc(mysql_query($sql));

然后,您可以随意操作$row(单行)。

您可以暂时停止操作。这可能会使执行速度加快一分钟。

您可以暂时停止。这可能会使执行速度加快一分钟。

试试以下方法:

<?php
    include 'config.php';
    include 'opendb.php';

    $query  = "select user from users where user = 'norman";
    $result = mysql_query($query);

    while($row = mysql_fetch_array($result))
    {
        echo "Name :{$row['user']}";
    }

    include 'closedb.php';
    ?>

试试这个:

<?php
    include 'config.php';
    include 'opendb.php';

    $query  = "select user from users where user = 'norman";
    $result = mysql_query($query);

    while($row = mysql_fetch_array($result))
    {
        echo "Name :{$row['user']}";
    }

    include 'closedb.php';
    ?>


检查您至少有1个值,然后获取它并进行处理。如果检查num_rows及其1,则可以进行提取;如果its>1,则可以使用while循环。为确保始终返回单个结果并加快查询速度,请在查询末尾添加
LIMIT 1
。检查至少有1个值,然后提取并处理。如果您检查num_rows和它的1,您可以进行一次提取,如果它>1,您可以使用while循环。为了确保始终返回一个结果并加快查询速度,请在查询末尾添加
LIMIT 1
。使用该函数比只执行
$row=mysql_fetch\u assoc(mysql_query($sql)),速度要慢得多,但使您的代码更干净、更抗错误:-)使用该函数比仅执行
$row=mysql\u fetch\u assoc(mysql\u query($sql))要慢得多,但使您的代码更干净、更抗错误:-)