It is just blank page when I try to get city from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dinku33
Forum Newbie
Posts: 10
Joined: Thu Jan 17, 2008 4:43 pm

It is just blank page when I try to get city from database

Post by dinku33 »

I have table "citystatecountry" in my database. Which has "State" and "City" fields.
I want to retrive cities belonging to Albama State. when i try it, it gives just blank page, no error nothing. Any suggession? Here is the page - http://www.wewillbook.com/world-hotel-d ... tabase.php

Code: Select all

 
<?
$sql = "SELECT `City` FROM `citystatecountry` WHERE 1 AND `State` = 'Alabama' ORDER BY `City` ASC";
?>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: It is just blank page when I try to get city from database

Post by requinix »

I sure hope you have more code than that. We need to see it.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: It is just blank page when I try to get city from database

Post by deejay »

yeah, we'll probably need to see more code. however

Code: Select all

 
WHERE 1 AND `State` = 'Alabama'
doesn't look right to me. do you have a field called '1' and are you asking the query to match to 'Alabama'. I don't consider myself an expert on this but you'll probably want to have a look at the guide for mysql
dinku33
Forum Newbie
Posts: 10
Joined: Thu Jan 17, 2008 4:43 pm

Re: It is just blank page when I try to get city from database

Post by dinku33 »

I am not good at PHP. Please find the following details to get more idea about that.
I got table in my database which is "citystatecountry" which has field like Id, Country, State, City.
I want page where a link like "All city of Albama State". If someone click on it then all cities should be retrieved from the database.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: It is just blank page when I try to get city from database

Post by onion2k »

deejay wrote:yeah, we'll probably need to see more code. however

Code: Select all

 
WHERE 1 AND `State` = 'Alabama'
doesn't look right to me. do you have a field called '1' and are you asking the query to match to 'Alabama'. I don't consider myself an expert on this but you'll probably want to have a look at the guide for mysql
No, he doesn't. That is how people should write queries. It makes a lot more sense when you have several where clauses, eg:

[sql]SELECT `City` FROM `citystatecountry` WHERE 1 AND `State` = 'Alabama' AND `Col2` = '2' AND `Col3` = '3' AND `Col4` = '4' ORDER BY `City` ASC[/sql]
By putting the 1 at the start you can start each clause with it's boolean operation. Without the 1 it looks like:

[sql]SELECT `City` FROM `citystatecountry` WHERE `State` = 'Alabama' AND `Col2` = '2' AND `Col3` = '3' AND `Col4` = '4' ORDER BY `City` ASC[/sql]
That's not as readable.

All the "1" means is "always match this" (because it's a positive assertion), so the query is "Always match this, AND match where `state` = 'alabama', AND match where `Col2` = 2" and so on.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: It is just blank page when I try to get city from database

Post by deejay »

sorry for the bad advice and thanks onion2k . i stand corrected. will look into that myself :)
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: It is just blank page when I try to get city from database

Post by crazycoders »

Guys, if the user says he's not too good with php and only shows a:

Code: Select all

 
$sql = 'some sql string';
 
Maybe the first way to go is ask if he has made a connection and is actually outputting the data... creating a SQL string doesn't output data into the browser :)

This is the first think you need to confirm, are you actually doing something like that:

Code: Select all

 
mysql_connect('hostname', 'username', 'password');
mysql_select_db('databasename');
$sql = "SELECT `City` FROM `citystatecountry` WHERE 1 AND `State` = 'Alabama' ORDER BY `City` ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
echo $row['city'].'<br />';
}
 
Last edited by Benjamin on Wed May 13, 2009 4:04 pm, edited 1 time in total.
Reason: Changed code type from text to php.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: It is just blank page when I try to get city from database

Post by crazycoders »

onion2k wrote:
deejay wrote:yeah, we'll probably need to see more code. however

Code: Select all

 
WHERE 1 AND `State` = 'Alabama'
doesn't look right to me. do you have a field called '1' and are you asking the query to match to 'Alabama'. I don't consider myself an expert on this but you'll probably want to have a look at the guide for mysql
No, he doesn't. That is how people should write queries. It makes a lot more sense when you have several where clauses, eg:

[sql]SELECT `City` FROM `citystatecountry` WHERE 1 AND `State` = 'Alabama' AND `Col2` = '2' AND `Col3` = '3' AND `Col4` = '4' ORDER BY `City` ASC[/sql]
By putting the 1 at the start you can start each clause with it's boolean operation. All the "1" means is "always match this" (because it's a positive assertion), so the query is "Always match this, AND match where `state` = 'alabama', AND match where `Col2` = 2" and so on.
It is indeed right to use this but becareful, not all databases interpret integral results as positive assertions. MSSQL server doesn't allow 1 as a single argument of a condition, you absoluutely need 1=1 and then it's allright. So i would strongly urge people to either not use 1 as a way to better structure your queries or always use 1=1 if you wish to be multi-database compliant!
dinku33
Forum Newbie
Posts: 10
Joined: Thu Jan 17, 2008 4:43 pm

Re: It is just blank page when I try to get city from database

Post by dinku33 »

Thank you very much to all. It is working fine with below code. Thanks again

Code: Select all

 
<?
$sql = "SELECT `City` FROM `citystatecountry` WHERE `State` = 'Alabama' ORDER BY `City` ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
echo $row['City'].'<br />';
}
?>
Last edited by Benjamin on Thu May 14, 2009 12:11 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply