Jump to content


Photo

problem with query WHERE


  • Please log in to reply
8 replies to this topic

#1 dtsdave

dtsdave
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 15 March 2006 - 09:50 PM

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:
CREATE TABLE pages (
page_id TINYINT(5) UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(200) NOT NULL,
PRIMARY KEY (page_id)
)

And this is the query I use on a template page:
$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); 

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:
$query = "SELECT * FROM pages WHERE title=".$_GET['title'];
$result = mysql_query($query) or die(mysql_error()); 
$row = mysql_fetch_array($result, MYSQL_ASSOC); 

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.

#2 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 15 March 2006 - 10:51 PM

I'm not sure if this will solve that problem but i noticed you forgot to close your quotation marks, try:

$query = "SELECT * FROM pages WHERE title='$_GET['title']'";


#3 dtsdave

dtsdave
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 15 March 2006 - 11:52 PM

[!--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) View Post[/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:

$query = "SELECT * FROM pages WHERE title='$_GET['title']'";
[/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.

#4 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 16 March 2006 - 12:54 AM

well, obviously it is taking title1 as your column name..

<?php
$title = $_GET['title'];
$debug = true;

$query = "SELECT * FROM pages WHERE title = '$title";
if ($debug) print $query;

mysql_query($query);
?>

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#5 dtsdave

dtsdave
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 16 March 2006 - 01:06 AM

[!--quoteo(post=355528:date=Mar 15 2006, 07:54 PM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 15 2006, 07:54 PM) View Post[/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?

#6 dtsdave

dtsdave
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 16 March 2006 - 09:35 PM

keeB,

When I run the code you suggest all I get is the query string spit back at me.


#7 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 17 March 2006 - 06:06 AM

Sorry dave.. it looks like i didn't complete it :X

Add that to the bottom.. [=

<?php

$result = mysql_query($query);

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

?>

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 17 March 2006 - 10:11 AM

[!--quoteo(post=355536:date=Mar 16 2006, 01:06 AM:name=dtsdave)--][div class=\'quotetop\']QUOTE(dtsdave @ Mar 16 2006, 01:06 AM) View Post[/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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 dtsdave

dtsdave
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 18 March 2006 - 06:54 PM

[!--quoteo(post=355883:date=Mar 17 2006, 05:11 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Mar 17 2006, 05:11 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Because it is a text string value not in quotes.
[/quote]

Got it! Thank you very much.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users