Forum Moderators: coopster

Message Too Old, No Replies

Query String: I don't want to use ID

         

The Cricketer

8:59 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



I have this code below on a php page. At the moment the query extracts the row from the database based on the auto incrementing primary key 'id' field. However instead of the 'id' in the URL

mysite.com/article.php?id=333

I wish to extract the row based on the 'article_name' column which has a unique key.

mysite.com/article.php?article_name=how-to-eat-cake

Firstly can someone tell me how to change the code below to achieve this. I thought it would just be a case of changing 'id' to 'article_name' etc but it didn't work.

Secondly can anyone tell me any issues with doing it this way please.

$sql = "SELECT * FROM content2 where id =". ($_GET['id']);
$queryResource = mysql_query($sql, $dbc);

while ($row = mysql_fetch_array ($queryResource ))
{

<TITLE><?php echo $row['title'];}?></TITLE>

jatar_k

9:08 pm on Nov 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$sql = "SELECT * FROM content2 where id =". ($_GET['id']);

you should be able to change this to

$sql = "SELECT * FROM content2 where article_name =". ($_GET['article_name']);

The Cricketer

9:21 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



Yeah that's what I had tried. I have checked that I have all the spelling right and that I'm refering to everything correctly.

It returns this error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/docs/article.php on line 17

Line 17 being
while ($row = mysql_fetch_array ($queryResource ))

which didn't cause any problems with the 'id'.....

jatar_k

9:45 pm on Nov 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try this for a little more info

$queryResource = mysql_query($sql, $dbc) or die(mysql_error());

The Cricketer

10:10 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



Thanks Jatar. Bad error management!

It said

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'to-eat-cake' at line 1

Is it my mistake in that the article's name in the database has got '-' between the words. It seems to have had trouble once it met the first '-' i.e. the article's name is:
'how-to-eat-cake'

I have another article called
'the-story-of-the-blind-cod' which has a different error message

Unknown column 'the' in 'where clause'

where 'the' is 'the-story-of-the-blind-cod'

I just tested the '-' though and changed all entries to 'howtoeatcake'

and it errored again

Unknown column 'howtoeatcake' in 'where clause'

I'm bemused.

jatar_k

10:13 pm on Nov 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that's my fault in part, wasn't paying attention, sry

your original query on a column that uses an integer therefore the id= does not need to be quoted.

when we switched the query it is now using a string which must be quoted

$sql = "SELECT * FROM content2 where article_name ='". ($_GET['article_name']) . "'";

single quotes around the value of the GET var now, try that

The Cricketer

10:23 pm on Nov 25, 2004 (gmt 0)

10+ Year Member



Thanks a lot that's done it. I learn from experience. From everyone's experience. Thanks for the time.

jatar_k

4:36 am on Nov 26, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sorry I screwed it up the first time