Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Problem with mysql_fetch_array

Codes here !

Moderators: macek, egami, gesf

Problem with mysql_fetch_array

Postby buothz » Fri Aug 24, 2012 2:38 am

I have a problem, I'm quite sure that the query is correct because the SQL works fine, the problem is php syntax:
Code: Select all
$getSearch = mysql_query("SELECT region, rests.name FROM delivery_areas, rests WHERE city = 'Ciccagna' AND rest_id = rests.id");
   while ($search = mysql_fetch_array($getSearch)) {
echo $search[region];   
echo $search[rests.zip];
   }


I get the exact value of "region" but I do not get anything for "rests.name".
Problem is the syntax of echo $search[rests.zip];

I've try with
$restzip = rests.zip; -> $search[$restzip];
echo $search['rests.zip'];
echo $search["rests.zip"];

But it does not work. What's wrong?
Thanks in advance.
buothz
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Aug 24, 2012 2:32 am

Re: Problem with mysql_fetch_array

Postby freshnet » Fri Aug 24, 2012 3:34 am

Your query is selecting a column called rests.name. You can't echo back a column that isn't in your results. You'd have to do something like

Code: Select all
SELECT region, rests.name, rests.zip FROM delivery_areas, rests WHERE city = 'Ciccagna' AND rest_id = rests.id");
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 277
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Problem with mysql_fetch_array

Postby buothz » Fri Aug 24, 2012 6:44 am

freshnet wrote:Your query is selecting a column called rests.name. You can't echo back a column that isn't in your results. You'd have to do something like

Code: Select all
SELECT region, rests.name, rests.zip FROM delivery_areas, rests WHERE city = 'Ciccagna' AND rest_id = rests.id");

sorry, posted code is not all the original code because query is much longer, I've made the mistake when I've restricted the code to facilitate the question on the forum, but the original file is right.

Even if I do:
Code: Select all
SELECT region, rests.name, rests.zip FROM delivery_areas, rests WHERE city = 'Ciccagna' AND rest_id = rests.id");
echo $search[rests.name];

don't show the echo... where is the error?
If I copy (ctr+c) the query and start it to SQL is ok! The error occurs when I do an ECHO of rests.name, rests.zip etc... where there's a point...
buothz
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Aug 24, 2012 2:32 am

Re: Problem with mysql_fetch_array

Postby NigelRen » Fri Aug 24, 2012 11:22 pm

When your accessing fields from mysql_fetch_array, you should have quotes around the field name, so it should be
Code: Select all
echo $search['rests.name'];
NigelRen
php-forum Active User
php-forum Active User
 
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

Re: Problem with mysql_fetch_array

Postby buothz » Sat Aug 25, 2012 1:18 am

NigelRen wrote:When your accessing fields from mysql_fetch_array, you should have quotes around the field name, so it should be
Code: Select all
echo $search['rests.name'];

found the problem!
When recall on the mysql_fetch_array you must use:
echo $search["zip"]; and echo $search["name"]; //ok
echo $search["rests.zip"]; and echo $search["rests.name"]; //wrong!
buothz
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Aug 24, 2012 2:32 am


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.