Create PHP array from MySQL column
you could loop through the array, and create a new one, like so:
$column = array();
while($row = mysql_fetch_array($info)){
$column[] = $row[$key];
//Edited - added semicolon at the End of line.1st and 4th(prev) line
}
How to create PHP array from MySQL table
You need to change your code a bit and it will work fine:-
$results = array();
while($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$results[$line['type_id']][] = $line['Type_name']; //check change here
}
print_r($results);
Note:- check your column-names and correct them if any mistake is there.
Turning a mysql column into a php array and echoing it
First if you wish to work with column names in your array you need to replace mysql_fetch_array
with mysql_fetch_assoc
.
Then you need to access the row, not the result, inside your while
loop:
$array[] = $row['id'];
Generate PHP array from MySQL with key value from id column
This is doable as an associative array.
All you have to do is change
$array[] = $row
to
$array[$row['ID']] = $row
You need to make sure that the ID
column in your database is unique so that the associative array doesn't overwrite keys (in which case only the last record with duplicate ID's would remain)
EDIT (19-11-2015)
Also about your 'mysql loop' - it's just a regular PHP while
loop that loops through records given by either mysql_*
or mysqli_*
functions. You're looping through the returned result from executing an sql statement on the database which just fetches and formats rows for you in a certain way.
MySQL and MySQLi are two different things but the SQL they use is no different (except for maybe prepared statements?).
Learning SQL is what you're going to do and to execute it you're going to use mysqli_*
from now on since mysql_*
functions are deprecated
I just wanted to point those things out to clear out some confusion you might already have or will have in the future. :)
For more reading on mysqli read the php.net manual, it's all a bit complex if you're just starting but that's fine - understand what you can and work to understand what you can't.
EDIT 2 (19-11-2015)
the indexes in your $row
array are case-sensitive so ID
and id
are something different entirely. one will add up to an undefined index
error.
I noticed in your output that the id
key is actually lowercase so I would try changing to that.
Good luck!
Php array from mysql column
You're re-executing the query endlessly, because you're doing it as part of your while, so if any records are returned , your code will re-query and return the same result time and again
Execute the query, then iterate over the result set
$result = mysql_query("SELECT Username FROM inloggen");
$array = array();
while ($row = mysql_fetch_array($result)) {
$array[] = $row['Username'];
}
Caveat: The MySQL extension is a deprecated interface; you should be using MySQLi or PDO
How to create PHP array from MySQL data
I ended up sussing it out. Thanks to Create PHP array from MySQL column
Here's what I did:
$column = array();
while($row = mysqli_fetch_array($result)){
$column[] = $row['regId'];
//Edited - added semicolon at the End of line.1st and 4th(prev) line
}
I did earlier try this however it was originally mysql and not mysqli!!
I then also set my $registrationIDs variable equal to $column
How get an array from a MySQL column with mysqli_fetch_array
Just change:
while($row = mysql_fetch_array($result, MYSQL_NUM)){
$bannedips[] = $row;
And you can't merge mysqli function with mysql
$result = mysqli_query($conn, $result);
while($row = mysql_fetch_array($result, MYSQL_NUM)){
finally (EDIT):
$host = "example";
$username = "example";
$password = "example";
$database = "example";
$ip = $_SERVER['REMOTE_ADDR'];
$bannedips = array();
$notBanned = true; //later used to check, if ip is banned
$connection = new mysqli($host, $username, $password, $database);
$query = $connection->query("SELECT ip FROM ipban");
while ($row = $query->fetch_array()) {
$bannedips[] = $row['ip'];
}
if (in_array($ip, $bannedips)) {
$notBanned = false;
}
You can read more at:
http://php.net/manual/en/function.mysql-fetch-array.php
Create single array from MySQL with PHP/PDO
What you need is PDO::FETCH_COLUMN
mode:
$sql = "SELECT name FROM " . $this->table_name . " ORDER BY id";
$prep_state = $this->db_conn->prepare($sql);
$prep_state->execute();
// 0 indicates first column of result
$names = $prep_state->fetchAll(PDO::FETCH_COLUMN, 0);
print_r($names);
// then do what you want.
Related Topics
Curl - Load a Site with Cloudflare Protection
Execute Two Shell Commands in Single Exec PHP Statement
Change Cart Total Price in Woocommerce
I am Confused About PHP Post/Redirect/Get
Allow Users to Download Files Outside Webroot
How to Send an Email Using PHP at Windows Azure
Large Hex Values with PHP Hexdec
PHP Script to Delete Files Older Than 24 Hrs, Deletes All Files
At' Symbol Before Variable Name in PHP: @$_Post
How to Decode Numeric HTML Entities in PHP
Sending Bcc Emails Using a Smtp Server
PHP Read File as an Array of Bytes
How to See the Actual Xml Generated by PHP Soap Client Class
Get the Metadata of an Order Item in Woocommerce 3