Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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
Php 将mysql数据存储在数组中_Php_Mysql_Pdo - Fatal编程技术网

Php 将mysql数据存储在数组中

Php 将mysql数据存储在数组中,php,mysql,pdo,Php,Mysql,Pdo,在php中,不必使用while循环,就可以在数组中存储mysql数据吗? 我这里只有一列 $members = $conn->prepare(Select id from members where state = 'CA'); $members->execute(); //Result ->1, 2, 3, 4, 5, 6, 7, 8, 9 //$myarray = array of the above results. $myarray = array(1, 2, 3, 4

在php中,不必使用
while
循环,就可以在数组中存储mysql数据吗? 我这里只有一列

$members = $conn->prepare(Select id from members where state = 'CA');
$members->execute();
//Result ->1, 2, 3, 4, 5, 6, 7, 8, 9
//$myarray = array of the above results.
$myarray = array(1, 2, 3, 4, 5, 6, 7, 8, 9)//Has to be this kind of array.

我试图在这里避免另一个while循环,因为它包含了更多的代码。

看。首先,您必须了解,在没有循环的情况下,将一些数据放入数组是不可能的。你可以在地毯下扫一圈,但你必须明白它仍然存在于某处

如果您正在寻找这样一种语法糖:


您可以尝试使用fetch_row函数

即使您发现一些构造不会向您显示循环,它仍然必须循环所有行以获取您的数据:因此,我认为尝试避免它是徒劳的如果您的输出仅在一行中,则不需要while循环here@cartina所以下面的答案是唯一的方法吗?或者你有更好的方法?我想那是mysqli。我正在用php PDO做这件事。
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);