|
| ||||||||||
| Tags: database, mysql, populate array |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| How to Populate an Array for MYSQL
I have number of fields but instead of that want to fetch data for some field name only. And these are as follows: Cust_id ¦ Cust_name ¦ Cust_loc $name_ar = array( 'Cust_name1' => 'nickname1', 'Cust_name2' => 'nickname2', 'Cust_name3' => 'nickname3', /* and on and on */ ); Please suggest how do i trigger or modify an Array to get the expected output. |
|
#2
| ||||
| ||||
| Re: How to Populate an Array for MYSQL
// get the data from the DB to an array, initializing: $myarray = array(); // iterate through DB records while ($row = mysql_fetch_object ($result)) { // now the array element with the "object_name" from the current database record gets the value "object_code" from the current database record $myarray[$row['object_name']] = $row['object_code']; } // finally we can use the array: $editor->set_inserts($myarray); |
|
#3
| |||
| |||
| Re: How to Populate an Array for MYSQL
I found this code on the internet so i brought it for you to go ahead.hope this will solve your problem. Code: while($row = mysql_fetch_row($result)) {
$myarray[$row[0]] = $row[1];
}
while(list($key,$value) = each($myarray)) {
echo "$key : $value";
} |
|
#4
| ||||
| ||||
| Re: How to Populate an Array for MYSQL
The mysql_fetch_array function returns an associative array, but it also returns FALSE if there are no more rows to return! Using a PHP While Loop we can use this information to our advantage. If we place the statement "$row = mysql_fetch_array()" as our while loop's conditional statement we will accomplish two things:
Now that we know what we need to do and how to go about doing it, the code pretty much writes itself, so let's move on to the next lesson. Just kidding! Here is the code that will print out all the rows of our MySQL Resource. PHP and MySQL Code: PHP Code: Quote:
|
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "How to Populate an Array for MYSQL" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Unable to install MySQL and MySQL Workbench on Linux mint 10 | Zared | Windows Software | 4 | 13-01-2011 12:08 AM |
| PHP - I want to retrieve records from specific field from a MySQL table and build array using PHP | bond007dev | Software Development | 3 | 19-07-2010 04:23 PM |
| Mysql Error : Can't connect to local mysql server through socket ' var lib mysql mysql.sock' 2 | roshan45 | Software Development | 3 | 07-11-2009 08:36 PM |
| Fill an array using a mysql database | Jacek | Software Development | 3 | 16-04-2009 12:12 AM |
| Add or remove programs won't populate | Klums | MediaCenter | 1 | 17-06-2007 06:38 AM |