MySQL select statement not working

Codes here !

Moderators: egami, macek, gesf

Post Reply
User avatar
minimihi
New php-forum User
New php-forum User
Posts: 236
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Mon May 07, 2012 12:21 pm

Code seems to be fine.
If you have PhpMyAdmin or anything similar, could you make screen-shots of table structures?

User avatar
freshnet
php-forum Active User
php-forum Active User
Posts: 359
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Mon May 07, 2012 3:18 pm

First rule of troubleshooting queries.. echo back the query to your browser and run it on your MYSQL install, see if the query runs.

faust
New php-forum User
New php-forum User
Posts: 109
Joined: Thu May 03, 2012 7:22 am

Tue May 08, 2012 3:56 am

I think that your queries are fine but the last IF is the problem.

Code: Select all

if (empty($names)){
$names = $row1['name'];
}else {
$names = $names . "," . $row1['name'];
}	
You are checking IF the name is empty and then you add it to the array. I think it should be

Code: Select all

if (!empty($names)){...
Last edited by faust on Tue May 08, 2012 3:57 am, edited 1 time in total.

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Tue May 08, 2012 4:33 am

freshnet wrote:First rule of troubleshooting queries.. echo back the query to your browser and run it on your MYSQL install, see if the query runs.
This is correct. Since you aren't seeing a MySQL error it means the query is syntactically sound. More than likely the value you are passing in empty or there is not data in the second table for your selected $city_id.

Acesystech
New php-forum User
New php-forum User
Posts: 35
Joined: Mon Aug 20, 2012 10:27 pm
Location: Ernakulam
Contact:

Wed Aug 22, 2012 4:39 am

Not sure..plz try this..

$id = $_GET['city'];

$con1 = mysql_connect("localhost","lasith","lasith123");
if (!$con1){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con1);
$cities = mysql_query("SELECT * FROM cities where name = '$id'") or die(mysql_error());
while($city = mysql_fetch_array($cities)){
$city_id = $city['id'];
}

$result1 = mysql_query("SELECT * FROM locations where city = '$city_id'") or die(mysql_error());

$names = "";
while($row1 = mysql_fetch_array($result1)){
if (empty($names)){
$names = $row1['name'];
}else {
$names = $names . "," . $row1['name'];
}
}

Post Reply