TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   foreach statement? (http://www.talkphp.com/absolute-beginners/4933-foreach-statement.html)

Randy 09-09-2009 04:49 AM

foreach statement?
 
Alright so I am looking to create a foreach statement using mysql query. I am not sure how I can do this seeing as how it requires an array_expression and value.

Here's my code:

PHP Code:

// Retrieve Data From The Database
$query 'SELECT * FROM entries ORDER BY DESC ID';

// Run The Above MySQL Query
$results 'mysql_query($query)'

How can i use the foreach statement to use the data over and over for each entry that has the ID tag.

Thanks,
Randy

Village Idiot 09-09-2009 04:13 PM

Quote:

Originally Posted by Randy (Post 28331)
Alright so I am looking to create a foreach statement using mysql query. I am not sure how I can do this seeing as how it requires an array_expression and value.

Here's my code:

PHP Code:

// Retrieve Data From The Database
$query 'SELECT * FROM entries ORDER BY DESC ID';

// Run The Above MySQL Query
$results 'mysql_query($query)'

How can i use the foreach statement to use the data over and over for each entry that has the ID tag.

Thanks,
Randy

A few things first:
1. ORDER BY DESC ID is not a valid statement, ORDER BY ID DESC would be
2. $results = 'mysql_query($query)'; should be$results = mysql_query($query); without the single quotes.

Then what you need to do is add this code:
PHP Code:

$arr_results mysql_fetch_array($results); 

Then you are ready to do the foreach loop.

Randy 09-09-2009 10:28 PM

My Code:

PHP Code:

// Retrieve Data From The Database
$query 'SELECT * FROM entries ORDER BY ID DESC';

// Run The Above MySQL Query
$results mysql_query($query); 

// Fetch Results
$arr_results mysql_fetch_array($results);  

foreach (
$arr_results as $value)
    {
        echo 
$value['description'];
    } 

and I get an output like this:

Quote:

11EEssffLLLL22SSww

Database Structure Looks Like This:


Values Look Like:


------------------

I tried changing it to ID, description, name, everything and it gives me the same thing. Any thoughts?

adamdecaf 09-10-2009 12:29 AM

PHP Code:

// Credit should be given to wordpress, I've been studying 
// their code and came across this, it really is nice.
$query mysql_query('SELECT * FROM entries ORDER BY ID DESC');

foreach (
$query as $data) {
    
$results mysql_fetch_assoc($data);
}

// Now you should have an associative array 
// with all of the rows returned from the query.
// So, we can just print them out to show you.
print_r($results); 


The reason that "mysql_fetch_array()" was not working is because it accepts two arguments (2nd is optional but recommended):
$result - The MySQL query...
$type - How you want the data back, numeric or assoc. $data[0] vs $data['name']

E.G. mysql_fetch_array($query, MYSQL_ASSOC);

Randy 09-10-2009 05:15 AM

Code:

PHP Code:

    // Credit should be given to wordpress, I've been studying 
// their code and came across this, it really is nice.
$query mysql_query('SELECT * FROM entries ORDER BY ID DESC');

foreach (
$query as $data) {
    
$results mysql_fetch_assoc($data);

// Now you should have an associative array 
// with all of the rows returned from the query.
// So, we can just print them out to show you.
print_r($results);  



ERROR:

Warning: Invalid argument supplied for foreach() in C:\wamp\www\pbird\index.php on line 20

line 20 is the foreach statement

sketchMedia 09-10-2009 09:23 AM

The problem is that you can't loop over a mysql resource, you must have the array assignment before the loop otherwise there is nothing to loop over.
Whats wrong with the good ol' while loop, eh?
PHP Code:

 while($row=mysql_fetch_assoc($query)) 

    
print_r($row);
    
//echo $row['description'];
 


If you really must use a for loop, this should work:
PHP Code:

$query mysql_query('SELECT * FROM entries ORDER BY ID DESC');
$rows mysql_num_rows($query);

for(
$i 0$i $rows; ++$i)
{
    
mysql_data_seek($query$i);
    
$data mysql_fetch_assoc($query);
    echo 
$data['description'] . '<br />';



Randy 09-10-2009 07:14 PM

Thanks to both of you its working now with sketchMedia's first code.

Much appreciated.


All times are GMT. The time now is 05:58 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0