dtsdave Posted March 15, 2006 Share Posted March 15, 2006 I'm trying to understand why a query on one column will work while a query on another column will not work. I use the following to create a table:[code]CREATE TABLE pages (page_id TINYINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,title VARCHAR(200) NOT NULL,PRIMARY KEY (page_id))[/code]And this is the query I use on a template page:[code]$query = "SELECT * FROM pages WHERE page_id=".$_GET['page_id'];$result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); [/code]I have some other code on the page to call various data from the row. When I go to sitename.com/template.php?page_id=1 it shows the data I want from the row where page_id=1, sitename.com/template.php?pade_id=2 shows the data I want from the row where page_id=2, etc.Now, I change the query, replacing page_id with title:[code]$query = "SELECT * FROM pages WHERE title=".$_GET['title'];$result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); [/code]When I go to sitename.com/template.php?title=title1 instead of showing the data from the row where I have a title = title1 I get an error that says Unknown column 'title1' in 'where clause'Where am I going wrong? Thanks in advance for the help. Link to comment https://forums.phpfreaks.com/topic/5068-problem-with-query-where/ Share on other sites More sharing options...
php_b34st Posted March 15, 2006 Share Posted March 15, 2006 I'm not sure if this will solve that problem but i noticed you forgot to close your quotation marks, try:[code]$query = "SELECT * FROM pages WHERE title='$_GET['title']'";[/code] Link to comment https://forums.phpfreaks.com/topic/5068-problem-with-query-where/#findComment-17961 Share on other sites More sharing options...
dtsdave Posted March 15, 2006 Author Share Posted March 15, 2006 [!--quoteo(post=355505:date=Mar 15 2006, 05:51 PM:name=php_b34st)--][div class=\'quotetop\']QUOTE(php_b34st @ Mar 15 2006, 05:51 PM) [snapback]355505[/snapback][/div][div class=\'quotemain\'][!--quotec--]I'm not sure if this will solve that problem but i noticed you forgot to close your quotation marks, try:[code]$query = "SELECT * FROM pages WHERE title='$_GET['title']'";[/code][/quote]I don't think that's the problem because when I add the last quotation mark (with either page_id or title) I get a parse error. Link to comment https://forums.phpfreaks.com/topic/5068-problem-with-query-where/#findComment-17979 Share on other sites More sharing options...
keeB Posted March 16, 2006 Share Posted March 16, 2006 well, obviously it is taking title1 as your column name..[code]<?php$title = $_GET['title'];$debug = true;$query = "SELECT * FROM pages WHERE title = '$title";if ($debug) print $query;mysql_query($query);?>[/code] Link to comment https://forums.phpfreaks.com/topic/5068-problem-with-query-where/#findComment-17984 Share on other sites More sharing options...
dtsdave Posted March 16, 2006 Author Share Posted March 16, 2006 [!--quoteo(post=355528:date=Mar 15 2006, 07:54 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 15 2006, 07:54 PM) [snapback]355528[/snapback][/div][div class=\'quotemain\'][!--quotec--]well, obviously it is taking title1 as your column name..[/quote]Why is it taking it as the column name when it doesn't do the same for page_id? Link to comment https://forums.phpfreaks.com/topic/5068-problem-with-query-where/#findComment-17991 Share on other sites More sharing options...
dtsdave Posted March 16, 2006 Author Share Posted March 16, 2006 keeB,When I run the code you suggest all I get is the query string spit back at me. Link to comment https://forums.phpfreaks.com/topic/5068-problem-with-query-where/#findComment-18126 Share on other sites More sharing options...
keeB Posted March 17, 2006 Share Posted March 17, 2006 Sorry dave.. it looks like i didn't complete it :XAdd that to the bottom.. [=[code]<?php$result = mysql_query($query);while ( $data = mysql_fetch_row ( $result ) ) { print "<pre>"; print_r ( $result ); print "</pre>";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/5068-problem-with-query-where/#findComment-18270 Share on other sites More sharing options...
Barand Posted March 17, 2006 Share Posted March 17, 2006 [!--quoteo(post=355536:date=Mar 16 2006, 01:06 AM:name=dtsdave)--][div class=\'quotetop\']QUOTE(dtsdave @ Mar 16 2006, 01:06 AM) [snapback]355536[/snapback][/div][div class=\'quotemain\'][!--quotec--]Why is it taking it as the column name when it doesn't do the same for page_id?[/quote]Because it is a text string value not in quotes. Link to comment https://forums.phpfreaks.com/topic/5068-problem-with-query-where/#findComment-18304 Share on other sites More sharing options...
dtsdave Posted March 18, 2006 Author Share Posted March 18, 2006 [!--quoteo(post=355883:date=Mar 17 2006, 05:11 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Mar 17 2006, 05:11 AM) [snapback]355883[/snapback][/div][div class=\'quotemain\'][!--quotec--]Because it is a text string value not in quotes.[/quote]Got it! Thank you very much. Link to comment https://forums.phpfreaks.com/topic/5068-problem-with-query-where/#findComment-18618 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.