如何使用PHP在列中获取所有值?

时间:2021-04-15 08:00:46

I've been searching for this everywhere, but still can't find a solution: How do I get all the values from a mySQL column and store them in an array?

我到处都在搜索,但还是找不到解决方案:如何从mySQL列获取所有值并将它们存储在数组中?

For eg: Table Name: Customers Column names: ID, Name # of rows: 5

如:表名:客户列名:ID,列名:5

I want to get an array of all the 5 names in this table. How do I go about doing that? I am using PHP, and I was trying to just:

我想要得到这个表中所有5个名字的数组。我该怎么做呢?我在用PHP,我试着:

SELECT names FROM Customers

and then use the

然后使用

mysql_fetch_array

PHP function to store those values in an array.

PHP函数将这些值存储在数组中。

5 个解决方案

#1


26  

Note that this answer is outdated! The mysql extension is no longer available out of the box as of PHP7. If you want to use the old mysql functions in PHP7, you will have to compile ext/mysql from PECL. See the other answers for more current solutions.

注意,这个答案已经过时了!从PHP7开始,mysql扩展就不再可用了。如果您想在PHP7中使用旧的mysql函数,您必须从PECL编译ext/mysql。请参阅其他答案以获得更多当前的解决方案。


This would work, see more documentation here : http://php.net/manual/en/function.mysql-fetch-array.php

这可以工作,请参阅这里的更多文档:http://php.net/manual/en/function.mysql-fetch-array.php

$result = mysql_query("SELECT names FROM Customers");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $storeArray[] =  $row['names'];  
}
// now $storeArray will have all the names.

#2


24  

Here is a simple way to do this using either PDO or mysqli

这里有一种使用PDO或mysqli的简单方法

$stmt = $pdo->prepare("SELECT Column FROM foo");
// careful, without a LIMIT this can take long if your table is huge
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_COLUMN);
print_r($array);

or, using mysqli

或者,使用mysqli

$stmt = $mysqli->prepare("SELECT Column FROM foo");
$stmt->execute();
$array = [];
foreach ($stmt->get_result() as $row)
{
    $array[] = $row['column'];
}
print_r($array);

Array
(
    [0] => 7960
    [1] => 7972
    [2] => 8028
    [3] => 8082
    [4] => 8233
)

#3


3  

I would use a mysqli connection to connect to the database. Here is an example:

我将使用mysqli连接连接连接数据库。这是一个例子:

$connection = new mysql("127.0.0.1", "username", "password", "database_name", 3306);

The next step is to select the information. In your case I would do:

下一步是选择信息。就你的情况而言,我会:

$query = $connection->query("SELECT `names` FROM `Customers`;");

And finally we make an array from all these names by typing:

最后,我们用这些名字创建一个数组,输入:

$array = Array();
while($result = $query->fetch_assoc()){
    $array[] = $result['names'];
}

print_r($array);

So what I've done in this code: I selected all names from the table using a mysql query. Next I use a while loop to check if the $query has a next value. If so the while loop continues and adds that value to the array '$array'. Else the loop stops. And finally I print the array using the 'print_r' method so you can see it all works. I hope this was helpful.

在这段代码中,我使用mysql查询从表中选择所有的名称。接下来,我使用while循环检查$查询是否有下一个值。如果是,则while循环继续并将该值添加到数组“$array”中。其他循环停止。最后,我使用“print_r”方法打印数组,这样您就可以看到它都是有效的。我希望这是有帮助的。

#4


1  

Since mysql_* are deprecated, so here is the solution using mysqli.

由于不赞成使用mysql_*,所以这里有一个使用mysqli的解决方案。

$mysqli = new mysqli('host', 'username', 'password', 'database');
if($mysqli->connect_errno>0)
{
  die("Connection to MySQL-server failed!"); 
}
$resultArr = array();//to store results
//to execute query
$executingFetchQuery = $mysqli->query("SELECT `name` FROM customers WHERE 1");
if($executingFetchQuery)
{
   while($arr = $executingFetchQuery->fetch_assoc())
   {
        $resultArr[] = $arr['name'];//storing values into an array
   }
}
print_r($resultArr);//print the rows returned by query, containing specified columns

There is another way to do this using PDO

还有另一种使用PDO的方法

  $db = new PDO('mysql:host=host_name;dbname=db_name', 'username', 'password'); //to establish a connection
  //to fetch records
  $fetchD = $db->prepare("SELECT `name` FROM customers WHERE 1");
  $fetchD->execute();//executing the query
  $resultArr = array();//to store results
  while($row = $fetchD->fetch())
  {
     $resultArr[] = $row['name'];
  }
  print_r($resultArr);

#5


0  

How to put MySQL functions back into PHP 7
Step 1

如何将MySQL函数重新放到PHP 7步骤1中

First get the mysql extension source which was removed in March:

首先获取三月份删除的mysql扩展源:

https://github.com/php/php-src/tree/PRE_PHP7_EREG_MYSQL_REMOVALS/ext/mysql

https://github.com/php/php-src/tree/PRE_PHP7_EREG_MYSQL_REMOVALS/ext/mysql

Step 2

步骤2

Then edit your php.ini

然后编辑中

Somewhere either in the “Extensions” section or “MySQL” section, simply add this line:

在“扩展”部分或“MySQL”部分,只需添加这一行:

extension = /usr/local/lib/php/extensions/no-debug-non-zts-20141001/mysql.so

Step 3

步骤3

Restart PHP and mysql_* functions should now be working again.

重新启动PHP和mysql_*函数现在应该又可以工作了。

Step 4

步骤4

Turn off all deprecated warnings including them from mysql_*:

关闭所有不赞成的警告,包括来自mysql_*的警告:

error_reporting(E_ALL ^ E_DEPRECATED);

Now Below Code Help You :

下面的代码帮助你:

$result = mysql_query("SELECT names FROM Customers");
$Data= Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
        $Data[] =  $row['names'];  
}

You can also get all values in column using mysql_fetch_assoc

还可以使用mysql_fetch_assoc在列中获取所有值

$result = mysql_query("SELECT names FROM Customers");
    $Data= Array();
    while ($row = mysql_fetch_assoc($result)) 
    {
            $Data[] =  $row['names'];  
    }

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

这个扩展在PHP 5.5.0中被废弃,在PHP 7.0.0中被删除。相反,应该使用sqmyli或PDO_MySQL扩展。

Reference

参考


YOU CAN USE MYSQLI ALTERNATIVE OF MYSQL EASY WAY

你可以使用sqmyli替代MYSQL的简单方法


*

*

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql);

// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)\n",$row[0],$row[1]);

// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]);

// Free result set
mysqli_free_result($result);

mysqli_close($con);
?> 

#1


26  

Note that this answer is outdated! The mysql extension is no longer available out of the box as of PHP7. If you want to use the old mysql functions in PHP7, you will have to compile ext/mysql from PECL. See the other answers for more current solutions.

注意,这个答案已经过时了!从PHP7开始,mysql扩展就不再可用了。如果您想在PHP7中使用旧的mysql函数,您必须从PECL编译ext/mysql。请参阅其他答案以获得更多当前的解决方案。


This would work, see more documentation here : http://php.net/manual/en/function.mysql-fetch-array.php

这可以工作,请参阅这里的更多文档:http://php.net/manual/en/function.mysql-fetch-array.php

$result = mysql_query("SELECT names FROM Customers");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $storeArray[] =  $row['names'];  
}
// now $storeArray will have all the names.

#2


24  

Here is a simple way to do this using either PDO or mysqli

这里有一种使用PDO或mysqli的简单方法

$stmt = $pdo->prepare("SELECT Column FROM foo");
// careful, without a LIMIT this can take long if your table is huge
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_COLUMN);
print_r($array);

or, using mysqli

或者,使用mysqli

$stmt = $mysqli->prepare("SELECT Column FROM foo");
$stmt->execute();
$array = [];
foreach ($stmt->get_result() as $row)
{
    $array[] = $row['column'];
}
print_r($array);

Array
(
    [0] => 7960
    [1] => 7972
    [2] => 8028
    [3] => 8082
    [4] => 8233
)

#3


3  

I would use a mysqli connection to connect to the database. Here is an example:

我将使用mysqli连接连接连接数据库。这是一个例子:

$connection = new mysql("127.0.0.1", "username", "password", "database_name", 3306);

The next step is to select the information. In your case I would do:

下一步是选择信息。就你的情况而言,我会:

$query = $connection->query("SELECT `names` FROM `Customers`;");

And finally we make an array from all these names by typing:

最后,我们用这些名字创建一个数组,输入:

$array = Array();
while($result = $query->fetch_assoc()){
    $array[] = $result['names'];
}

print_r($array);

So what I've done in this code: I selected all names from the table using a mysql query. Next I use a while loop to check if the $query has a next value. If so the while loop continues and adds that value to the array '$array'. Else the loop stops. And finally I print the array using the 'print_r' method so you can see it all works. I hope this was helpful.

在这段代码中,我使用mysql查询从表中选择所有的名称。接下来,我使用while循环检查$查询是否有下一个值。如果是,则while循环继续并将该值添加到数组“$array”中。其他循环停止。最后,我使用“print_r”方法打印数组,这样您就可以看到它都是有效的。我希望这是有帮助的。

#4


1  

Since mysql_* are deprecated, so here is the solution using mysqli.

由于不赞成使用mysql_*,所以这里有一个使用mysqli的解决方案。

$mysqli = new mysqli('host', 'username', 'password', 'database');
if($mysqli->connect_errno>0)
{
  die("Connection to MySQL-server failed!"); 
}
$resultArr = array();//to store results
//to execute query
$executingFetchQuery = $mysqli->query("SELECT `name` FROM customers WHERE 1");
if($executingFetchQuery)
{
   while($arr = $executingFetchQuery->fetch_assoc())
   {
        $resultArr[] = $arr['name'];//storing values into an array
   }
}
print_r($resultArr);//print the rows returned by query, containing specified columns

There is another way to do this using PDO

还有另一种使用PDO的方法

  $db = new PDO('mysql:host=host_name;dbname=db_name', 'username', 'password'); //to establish a connection
  //to fetch records
  $fetchD = $db->prepare("SELECT `name` FROM customers WHERE 1");
  $fetchD->execute();//executing the query
  $resultArr = array();//to store results
  while($row = $fetchD->fetch())
  {
     $resultArr[] = $row['name'];
  }
  print_r($resultArr);

#5


0  

How to put MySQL functions back into PHP 7
Step 1

如何将MySQL函数重新放到PHP 7步骤1中

First get the mysql extension source which was removed in March:

首先获取三月份删除的mysql扩展源:

https://github.com/php/php-src/tree/PRE_PHP7_EREG_MYSQL_REMOVALS/ext/mysql

https://github.com/php/php-src/tree/PRE_PHP7_EREG_MYSQL_REMOVALS/ext/mysql

Step 2

步骤2

Then edit your php.ini

然后编辑中

Somewhere either in the “Extensions” section or “MySQL” section, simply add this line:

在“扩展”部分或“MySQL”部分,只需添加这一行:

extension = /usr/local/lib/php/extensions/no-debug-non-zts-20141001/mysql.so

Step 3

步骤3

Restart PHP and mysql_* functions should now be working again.

重新启动PHP和mysql_*函数现在应该又可以工作了。

Step 4

步骤4

Turn off all deprecated warnings including them from mysql_*:

关闭所有不赞成的警告,包括来自mysql_*的警告:

error_reporting(E_ALL ^ E_DEPRECATED);

Now Below Code Help You :

下面的代码帮助你:

$result = mysql_query("SELECT names FROM Customers");
$Data= Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
        $Data[] =  $row['names'];  
}

You can also get all values in column using mysql_fetch_assoc

还可以使用mysql_fetch_assoc在列中获取所有值

$result = mysql_query("SELECT names FROM Customers");
    $Data= Array();
    while ($row = mysql_fetch_assoc($result)) 
    {
            $Data[] =  $row['names'];  
    }

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

这个扩展在PHP 5.5.0中被废弃,在PHP 7.0.0中被删除。相反,应该使用sqmyli或PDO_MySQL扩展。

Reference

参考


YOU CAN USE MYSQLI ALTERNATIVE OF MYSQL EASY WAY

你可以使用sqmyli替代MYSQL的简单方法


*

*

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql);

// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)\n",$row[0],$row[1]);

// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]);

// Free result set
mysqli_free_result($result);

mysqli_close($con);
?>