简体   繁体   中英

how to get array format from mysql result with php

I want to get array format from mysql database. Example,Let I have name table .

Name Table

Id    name

1    jone
2    smith
3   waiyan

I want to get this data array form.

$name=array('jone','smith','waiyan');(//I want to get this format)

How can get this result? I am beginner for php.Please answer me.Thank your contribute. Sorry for my english.

Something like this:

$sth = mysql_query("SELECT name FROM your_table");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
    $rows[] = $r["name"];
    //OR
    array_push($rows, $r['name']);
}
print_r($rows);

See: mySQL PHP

Check here for info about the mysql functions: http://jp2.php.net/manual/en/ref.mysql.php

For the specific task you're asking about, something like this should do it:

$result = msql_query('YOUR QUERY'); // returns a result resource
if ($result === false)  
{
    // handle the error
}

$names = array();
// get each row from your result one-by-one
while ($row = mysql_fetch_assoc($result))
{
    $names[] = $row['name']; // the keys in $row are named like your mysql columns
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM