Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
nested while loop with mysql queries
Old 11-20-2010, 05:25 PM nested while loop with mysql queries
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
PHP Code:
$sql="SELECT * FROM countries";
$result=mysql_query($sql);
    while (
$row mysql_fetch_array($resultMYSQL_ASSOC))
        {
            
$ccode $row["ccode"];
            echo 
'<h1>'.$row["country"].'</h1>';
            
            
sql2="SELECT * FROM `numbers` WHERE `location` = '".$ccode."'";
            
$result2=mysql_query($sql2);
                while (
$row2 mysql_fetch_array($result2MYSQL_ASSOC))
                    {
                        echo 
'<table><tr>';
                        echo 
'<td><p>'.$row2["name"].'</p></td><td><a href="http://'.$row["url"].'">'.$row2["url"].'</a></td>';
                        echo 
'</tr></table>';    
                    }
        } 
getting an error for the line
PHP Code:
sql2="SELECT * FROM `numbers` WHERE `location` = '".$ccode."'"
any suggestions. the error says Parse error: parse error in C:\wamp\www\prototype1\index.php on line 42

I basically trying to make a script which will group url's depending on the country origin which is inputted by user.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by mintuz; 11-20-2010 at 06:10 PM..
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
 
Register now for full access!
Old 11-20-2010, 06:10 PM Re: nested while loop with mysql queries
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You forgot the $ on sql2.

Instead of executing that query multiple times in the loop you could just build a list of 'ccodes' and select all of the rows you need at once:
Code:
SELECT * FROM numbers WHERE location IN ('value1', 'value2', 'value3');
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 11-20-2010, 06:13 PM Re: nested while loop with mysql queries
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
how would I build a list of ccode's and with my current solution to my problem I am now presented with this error

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\prototype1\index.php on line 54. Line 54 is

PHP Code:
while ($row2 mysql_fetch_array($result2MYSQL_ASSOC)) 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
Old 11-20-2010, 06:23 PM Re: nested while loop with mysql queries
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by mintuz View Post
with my current solution to my problem I am now presented with this error

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\prototype1\index.php on line 54. Line 54 is
mysql_query returns false on an error, so there must be an error with your query.

Quote:
Originally Posted by mintuz View Post
how would I build a list of ccode's
I can't right out the code right now. Basically you just need to create an array of ccodes. One thing I try to do is gather all of my data before doing any output. When you mix your output in with the 'logic' portion of your code it makes it difficult rework your approach.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 11-20-2010, 06:31 PM Re: nested while loop with mysql queries
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
alright thanks, I got it working in the end. I basically changed my query to

PHP Code:
$sql2="SELECT * FROM `numbers` WHERE `location` = '".$ccode."'"
If you get time do you think you could help me with arrays, I haven't really worked with them before.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
Reply     « Reply to nested while loop with mysql queries
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.21122 seconds with 12 queries