MySql PHP,将1行绑定到变量中

MySql PHP,将1行绑定到变量中,php,mysql,Php,Mysql,您好,到现在为止,我一直在使用以下代码: $query = $db->prepare("SELECT f_name, l_name FROM users WHERE user_id=1"); $query->execute(); $query->bind_result($f_name, $l_name); <?php while($query->fetch):?>something HTML where I use $f_name and $l_name&l

您好,到现在为止,我一直在使用以下代码:

$query = $db->prepare("SELECT f_name, l_name FROM users WHERE user_id=1");
$query->execute();
$query->bind_result($f_name, $l_name);
<?php while($query->fetch):?>something HTML where I use $f_name and $l_name<?php endwhile; ?>
要从表中获取数据并将其绑定到变量,但如果我想要一个结果,如上面的代码(其中user_id=1),我并不总是希望使用以下代码:

$query = $db->prepare("SELECT f_name, l_name FROM users WHERE user_id=1");
$query->execute();
$query->bind_result($f_name, $l_name);
<?php while($query->fetch):?>something HTML where I use $f_name and $l_name<?php endwhile; ?>
我使用$f_name和$l_name的HTML内容

是否有用于绑定结果的代码,其中唯一的资源是bind to 1变量,我可以在没有while和endwhile的代码中包含该变量?

@marcio它看起来正在使用PDO。是否使用?@john:mysqli也支持bind_result。我错了,PDO没有bind_result方法。(不是用它的书写方式)。因此,我们在这里处理mysqli。您不必在while循环中调用
fetch()
。如果只需要一行,只需在顶层或
If
中调用它即可。