I use the following PHP lines to query data from a MySQL table. I want to store the query results in an array and then later on the same page print specific values from this array based on their ID.
I am new to PHP and hope someone can help me with an explanation on what I am doing wrong or missing here. I am assuming I create the array wrongly.
My PHP (relevant part, variables are defined earlier on this page):
$con = mysqli_connect($host_name, $user_name, $password, $database);
$result = mysqli_query($con, "SELECT id, $col FROM $table ORDER BY id");
$rows = array();
if($result)
{
while($row = mysqli_fetch_array($result))
{
print_r($row);
}
}
var_dump($rows);
My current array (when printed as above):
Array ( [0] => testid1 [id] => testid1 [1] => testval1 [en] => testval1 ) Array ( [0] => testid2 [id] => testid2 [1] => testval2 [en] => testval2 ) Array ( [0] => testid3 [id] => testid3 [1] => testval3 [en] => testval3 ) array(0) { }
Example:
E.g. I want to print the value for the item with ID = "testid2" from this array.
In this case the printed value should be "testval2".
Many thanks in advance, Mike
print_rdoesn't create an array, it outputs all values in an array or object. Change to$rows[] = $row;inside yourwhile. ... or simply use php.net/manual/en/mysqli-result.fetch-all.php.print_r($set['1']);?print_r($set['1']['id']);?