Forum Moderators: coopster
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>
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'.....
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.
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