Friday 9 November 2018

How get all values in a column using PHP?





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?
For eg: Table Name: Customers Column names: ID, Name # of rows: 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:
SELECT names FROM Customers
and then use the
mysql_fetch_array
PHP function to store those values in an array.

 Answers




This would work, see more documentation here :
 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.






I would use a mysqli connection to connect to the database. Here is an example:
$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.






Step 1
First get the mysql extension source which was removed in March:
Step 2
Then edit your php.ini
Somewhere either in the “Extensions” section or “MySQL” section, simply add this line:
extension = /usr/local/lib/php/extensions/no-debug-non-zts-20141001/mysql.so
Step 3
Restart PHP and mysql_* functions should now be working again.
Step 4
Turn off all deprecated warnings including them from 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
$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.
YOU CAN USE MYSQLI ALTERNATIVE OF MYSQL EASY WAY

*
<?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);
?> 

0 comments:

Post a Comment