php - Multi-dimensional array from SQL query -


i have multi-dimensional array trying build sql query. having trouble adding id in inner array.

code:

$checkboxes = "select id, name review_sites active=1 order name asc";  $result = mysql_query($checkboxes) or die(mysql_error()); $names = array();  while ($row = mysql_fetch_array($result)) {     $names[]['name'] = $row['name'];  } 

currently array looks like:

array ( [0] => array     (         [name] => 411.ca     )  [1] => array     (         [name] => automd     ) 

i need array this:

array ( [0] => array     (         [id] => 4         [name] => 411.ca     )  [1] => array     (         [id] => 9         [name] => automd     ) 

you selecting id , name in query so, in loop:

while ($row = mysql_fetch_assoc($result)) {     $names[] = $row; } 

or simply:

while ($names[] = mysql_fetch_assoc($result)) {} 

notice, mysql_fetch_assoc() return associative array. mysql_fetch_array() returns associative , numerically indexed array.

also:

this extension deprecated of php 5.5.0, , removed in future. instead, mysqli or pdo_mysql extension should used. see mysql: choosing api guide , related faq more information. alternatives function include: mysqli_fetch_array() pdostatement::fetch()

if use mysqli extension may able use:

$names = mysqli_fetch_all($result, mysqli_assoc); 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -