TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-09-2009, 04:49 AM   #1 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default 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
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Old 09-09-2009, 04:13 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Randy View Post
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.
__________________

Village Idiot is offline  
Reply With Quote
Old 09-09-2009, 10:28 PM   #3 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default

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?
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford

Last edited by codefreek : 09-10-2009 at 12:34 AM. Reason: ModNote: image resize was made.
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Old 09-10-2009, 12:29 AM   #4 (permalink)
The Addict
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

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);
__________________
My Site
adamdecaf is offline  
Reply With Quote
The Following User Says Thank You to adamdecaf For This Useful Post:
Randy (09-10-2009)
Old 09-10-2009, 05:15 AM   #5 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default

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
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Old 09-10-2009, 09:23 AM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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 />';

__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 09-10-2009 at 09:50 AM.
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Randy (09-10-2009)
Old 09-10-2009, 07:14 PM   #7 (permalink)
The Acquainted
 
Randy's Avatar
 
Join Date: May 2007
Location: Your G/F's Closet
Posts: 114
Thanks: 7
Randy is on a distinguished road
Default

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

Much appreciated.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25 - Andrew Rutherford
Send a message via AIM to Randy Send a message via MSN to Randy
Randy is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Save output from foreach into an array Sam Granger Absolute Beginners 2 01-15-2009 02:07 AM
if within a foreach ? Mithadriel Absolute Beginners 2 11-09-2008 12:20 AM
Ugh, Query in a while statement Aaron Absolute Beginners 2 08-11-2008 06:36 PM
Setting integer to 0 at start of foreach Haris General 0 10-12-2007 04:41 AM


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

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design