December 15, 2015

PHP - SQL select query return array with used ID field as a key


                                               



In SQL select query while loop you can used ID fields as array key, it's simple. Here is example for that:

<?php
$selRes=mysql_query("SELECT id, name FROM TableName order by id DESC";
if(mysql_affected_rows()>0)
{
$data=array();
while($rowSale=mysql_fetch_array($selRes))
{
    $data[$row[0]] = $rowSale[1];
}
}

//Output
print_r($data);
array([1]=>'name1',[2]=>'name2',[3]=>'name3');

?>

Enjoy!

No comments:

Post a Comment

Integrating Google reCAPTCHA v3 in HTML Form with PHP

  What is Google reCAPTCHA v3? Google reCAPTCHA is a free service that helps protect websites from spam and abuse. reCAPTCHA v3 is the lates...