MySQL PHP 访问三行而不带 while 循环


mysql php access three rows without while loop

此查询始终生成三行。

$Z   = $myquery->execute($v); 
$row = mysql_fetch_array($Z);

我不想做一个while循环..我怎么能作为变量访问。

例如

 $row1 = the 1st column of row1
 $row2 = the 1st column of row1
 $row3 = the 1st column of row1

我以为这样的事情会起作用吗?

$row1 = $row[0][0];

谢谢。

没有什么说

fetch 函数只能在循环条件下调用。在语言级别,唯一限制在给定上下文中可以调用的函数的是参数到另一个函数的类型提示,并且该限制在于表达式的类型,而不是表达式本身。通常,如果可以在给定上下文中调用函数,则可以在该上下文中调用任何函数。

mysqli:

$query = $db->prepare('...');
$query->bind_param(...);
$query->execute();
$result = $query->get_result();
$rows[] = $result->fetch_array();
$rows[] = $result->fetch_array();
$rows[] = $result->fetch_array();

PDO:

$query = $db->prepare('...');
$query->execute(...);
$row[] = $query->fetch();
$row[] = $query->fetch();
$row[] = $query->fetch();
$row1 = mysql_fetch_row();
$row2 = mysql_fetch_row();
$row3 = mysql_fetch_row();