0

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.

2
  • 2
    Start with nz.php.net/manual/en/pdo.query.php Commented Jun 5, 2012 at 4:13
  • Thank for your instruction,Now I am trying with your link. Commented Jun 5, 2012 at 4:16

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Don't use array_push to push single elements. $array[] = $element; is better.
1

Check here for info about the mysql functions: https://www.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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.