Jump to content

iterating through mysql_fetch_array


truegilly

Recommended Posts

Hi people  :D

 

got a quick one here for you,

 

been playing for a while with the mysql_fetch_array function and would like to be able to iterate through the return values rather that pick out certain ones like, $row[7] or $row['rank'].

 

heres my code....

 

//perform query
$query = "SELECT RAFPerId FROM personnel WHERE job !='Station Commander' AND job !='Squadron Commander'";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
	$RafSqdCdrId = $row[0];
}

 

as you can see im returning the first result with $row[$i]. Is there a way i can do this like when using a For loop and $i and the iterator, $row ?

 

i have been trying but to little success.

 

thanks for any help !!  ;)

 

Truegilly  :-*

Link to comment
Share on other sites

fetch_array doesn't really work that way. in a while loop, you will be able to call each column, one row at a time. of course, that depends on what your sql query looks like... for your sql query, it looks like you've only chosen to select one column 'RAFPerId'. so, the only thing you can do in your while loop will be the variable $row['RAFPerId']. you can't call on the variable using $row[$i] like you would in a for loop.

Link to comment
Share on other sites

My preferred method is:

 

$query = mysql_query ("SELECT * FROM some_table");

while ($row = mysql_fetch_assoc ($query)) {
  foreach ($row as $var => $value) {
    print ("$var = $value<br />");
  }
}

 

That will print out all elements from your query. You can also use the same to make your associative array elements into variables like:

 

$query = mysql_query ("SELECT * FROM some_table");

while ($row = mysql_fetch_assoc ($query)) {
  foreach ($row as $var => $value) {
    $$var = $value;
  }
}

 

Link to comment
Share on other sites

there is no need to add a foreach loop inside a while loop... they do the exact same thing... you're just using up more resources.

I'm pretty sure the foreach is so that if there is more then 1 row he is searching for in the query, it will display all of them, instead of just one.
Link to comment
Share on other sites

there is no need to add a foreach loop inside a while loop... they do the exact same thing... you're just using up more resources.

I'm pretty sure the foreach is so that if there is more then 1 row he is searching for in the query, it will display all of them, instead of just one.

 

The while loop will loop through all rows. What I posted was a solution to what truegilly was wanting to do. I personally use the same to give me a quick idea of all the elements being pulled in from the db. The same could be achieved with print_r which I guess may be more efficient I dont know but less flexible certainly.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.