Jump to content

problem with query WHERE


dtsdave

Recommended Posts

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

[!--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.
[!--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?
Sorry dave.. it looks like i didn't complete it :X

Add that to the bottom.. [=

[code]
<?php

$result = mysql_query($query);

while ( $data = mysql_fetch_row ( $result ) ) {
    print "<pre>";
        print_r ( $result );
        print "</pre>";
}

?>
[/code]
[!--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.
[!--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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.