Page 1 of 1
It is just blank page when I try to get city from database
Posted: Wed May 13, 2009 4:44 am
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";
?>
Re: It is just blank page when I try to get city from database
Posted: Wed May 13, 2009 4:52 am
by requinix
I sure hope you have more code than that. We need to see it.
Re: It is just blank page when I try to get city from database
Posted: Wed May 13, 2009 5:08 am
by deejay
yeah, we'll probably need to see more code. however
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
Re: It is just blank page when I try to get city from database
Posted: Wed May 13, 2009 7:23 am
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.
Re: It is just blank page when I try to get city from database
Posted: Wed May 13, 2009 7:30 am
by onion2k
deejay wrote:yeah, we'll probably need to see more code. however
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.
Re: It is just blank page when I try to get city from database
Posted: Wed May 13, 2009 8:36 am
by deejay
sorry for the bad advice and thanks onion2k . i stand corrected. will look into that myself

Re: It is just blank page when I try to get city from database
Posted: Wed May 13, 2009 3:02 pm
by crazycoders
Guys, if the user says he's not too good with php and only shows a:
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 />';
}
Re: It is just blank page when I try to get city from database
Posted: Wed May 13, 2009 3:05 pm
by crazycoders
onion2k wrote:deejay wrote:yeah, we'll probably need to see more code. however
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!
Re: It is just blank page when I try to get city from database
Posted: Thu May 14, 2009 8:15 am
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 />';
}
?>