How get all values in a column using PHP?
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.
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.
get all values from SQL column using PHP (only returns one value)
You should use mysqli_fetch_all($acc_names, MYSQLI_ASSOC)
instead of mysqli_fetch_assoc
.
How can I get all the values of a column using PHP
Here is a simple way to do this using PDO
$stmt = $pdo->prepare("SELECT Column FROM foo LIMIT 5");
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_UNIQUE | PDO::FETCH_GROUP);
var_dump(array_keys($array));
array(5) {
[0]=>
int(7960)
[1]=>
int(7972)
[2]=>
int(8028)
[3]=>
int(8082)
[4]=>
int(8233)
}
Get Column All Values in One Array Using php
Thanks to every one finally i got it
<?php
// 1. Enter Database details
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'DB Name';
$connection = mysql_connect($dbhost,$dbuser,$dbpass);
// Check connection
if (!$connection) {
die("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db($dbname,$connection);
$result = mysql_query("SELECT parentproduct_id FROM my_entity_data");
$storeArray = array();
while ($row = mysql_fetch_array($result)) {
array_push($storeArray,$row['parentproduct_id']);
}
for ($i=0; $i < 10; $i++) {
echo $storeArray[i];
}
//echo sizeof($storeArray);
print_r($storeArray); //to see array data
?>
How to add all values in one column in database
You can directly use MySQL's SUM() function:
SELECT sum(rating) as total from rating
This query will directly give you the sum of all the column values.
So, the final code should be:
$st='SELECT sum(rating) as total from rating';
$t=mysqli_stmt_init($conn);
mysqli_stmt_prepare($t,$st);
mysqli_stmt_execute($t);
$res=mysqli_stmt_get_result($t);
$total=0;
while($rop=mysqli_fetch_assoc($res)){
$total = $rop['total'];
}
echo $total;
How to get entire column from database and store it in an array using php?
You should loop through the records by doing the following:
$result = [];
while ($array = mysqli_fetch_array($r)) {
$result[] = $array['MOBILE'];
}
echo json_encode($result);
Related Topics
How to Make Pdo Run Set Names Utf8 Each Time I Connect, in Zendframework
Artisan Migrate Could Not Find Driver
How to Password Protect Folder/Page Using PHP Without a Db or Username
Scaling a Chat App - Short Polling VS. Long Polling (Ajax, PHP)
Laravel 4 - Logging SQL Queries
How to Re-Save the Entity as Another Row in Doctrine 2
Setting PHP Tmp Dir - PHP Upload Not Working
What Factors Make PHP Unicode-Incompatible
Remove Diacritics from a String
Get Nearest Places on Google Maps, Using MySQL Spatial Data
Handle Fatal Errors in PHP Using Register_Shutdown_Function()
PHP to Store Images in MySQL or Not
How to Identify Server Ip Address in PHP
How to Create a Zip File Using PHP