Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to Populate an Array for MYSQL

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 14-04-2009
Member
 
Join Date: Apr 2009
Posts: 79
How to Populate an Array for MYSQL
  

What I need to do to populate an array,this is because i need to pull some records information from my MySQL database.

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.

Reply With Quote
  #2  
Old 14-04-2009
Member
 
Join Date: Jan 2009
Posts: 99
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);
Reply With Quote
  #3  
Old 14-04-2009
Member
 
Join Date: Jan 2009
Posts: 143
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";
}
Reply With Quote
  #4  
Old 14-04-2009
Member
 
Join Date: Jan 2009
Posts: 140
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:
  1. We will get a new row of MySQL information that we can print out each time the while loop checks its conditional statement.
  2. When there are no more rows the function will return FALSE causing the while loop to stop!

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:
<?php
// Make a MySQL Connection
$query "SELECT * FROM example"
     
$result mysql_query($query) or die(mysql_error());


while(
$row mysql_fetch_array($result)){
    echo 
$row['name']. " - "$row['age'];
    echo 
"<br />";
}
?>
Output:
Quote:
Timmy Mellowman - 23
Sandy Smith - 21
Bobby Wallace - 15
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 06:09 AM.