如何将作为mysql查询结果返回的列名的所有行存储在字符串数组中
I want to store all rows of a column name returned as a mysql query result in a string array. I am new to php. I have retrieved the rows but how do I store all rows of a column in an array? I mean how to iterate the counter of rows and store the column in an array? Or is there any direct function/method call? Please see my code below and its possible ways of storing in an array:
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'users';
mysql_select_db($dbname);
$result = mysql_query("SELECT users.email FROM users");
// AND THEN FINALLY STORING IN A STRING ARRAY:
$array = $result;
// OR SOMETHING LIKE:
Store($array, $result);
// OR SOMETHING LIKE:
for(int i = 0; i < $result.Rows.Count; i++)
{
if (null != $row[0])
{
$array[i] = $result[i];
}
}
// OR SOMETHING LIKE:
while($null != result.read())
{
$array.Add($result.Read());
}
Please help me in writing the code.
我想将作为mysql查询结果返回的列名的所有行存储在字符串数组中。 我是php的新手。 我检索了行但是如何在列中存储列的所有行? 我的意思是如何迭代行的计数器并将列存储在数组中? 或者是否有任何直接的函数/方法调用? 请参阅下面的代码及其在数组中存储的可能方式: p>
$ dbhost ='localhost';
$ dbuser ='user';
$ dbpass = 'password';
$ conn = mysql_connect($ dbhost,$ dbuser,$ dbpass)或die('连接到mysql'时出错');
$ dbname ='users';
mysql_select_db($ dbname); \ n
$ result = mysql_query(“SELECT users.email FROM users”);
//然后最后存储在一个字符串阵列中:
$ array = $ result;
// OR 有趣的是:
存储($ array,$ result);
//或者类似于:
for(int i = 0; i&lt; $ result.Rows.Count; i ++)
{
if(null!= $ row [0])
{
$ array [i] = $ result [i];
}
}
//或者像SOMETHING一样:
而 ($ null!= result.read())
{
$ array.Add($ result.Read());
}
code> pre>
请 帮我编写代码。 p>
div>
$result = mysql_query("SELECT users.email FROM users");
while($row = mysql_fetch_array($result)){
$array[] = $row;
}
This will return an array of associated arrays. The $row
will contain the index of the column name along with integer index.
For example:
echo $row['email'];
//or
echo $row[0];
In the end you can get the first row by:
$array[0]['email']; //or $array[0][0];
NOTICE: Do not use MySQL_* for it has been deprecated as of PHP 5.5. Use MySQLi_* or PDO instead.
This is how you can do it
$rec = mysqli_query("SELECT users.email FROM users");
$data = array();
$i=0;
while($row = mysql_fetch_assoc($rec)){
$data[$i]['column1'] = $row['column1_from_table'];
$data[$i]['column2'] = $row['column2_from_table'];
$data[$i]['column3'] = $row['column3_from_table'];
}