justlukeyou Posted March 15, 2011 Share Posted March 15, 2011 I have put together a query which is designed to display a single question from a database. However, it currently displays all the questions (and notes and category). The problem is I cant work out how to get it to display just the qid I am querying. This should display phpquestion.php?qid=2 but it just displays all the question as does phpquestion.php?qid=7 etc ini_set('display_errors', 1); error_reporting(-1); $query = "SELECT * FROM questions"; if(isset($_GET['question'])) { $question = $_GET['question']; $query .= " WHEN qid is '$qid' LIMIT 0, 1"; } $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $question = $row['question']; $notes = $row['notes']; $category = $row['category']; $qid = $row['qid']; echo "$question | $notes | $category </br> "; } if ($_GET['qid'] == $qid ) { echo 'Sorry, this question is not available. Please visit our <a href="http://www.ukhomefurniture.co.uk">Homepage</a>.'; } function sanitizeString($string) { return mysql_real_escape_string($string); } $question = sanitizeString($_GET['qid']); $query .= " WHERE question is '%$qid%'"; // close connection mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/230749-php-code-not-querying/ Share on other sites More sharing options...
Maq Posted March 15, 2011 Share Posted March 15, 2011 You're using $qid before it is defined. Quote Link to comment https://forums.phpfreaks.com/topic/230749-php-code-not-querying/#findComment-1187950 Share on other sites More sharing options...
justlukeyou Posted March 15, 2011 Author Share Posted March 15, 2011 Thanks, I've had problems with this before. Can you advise me how I do this. I have entered the following code, I thought thats how to define strings. $qid = $row['qid']; Quote Link to comment https://forums.phpfreaks.com/topic/230749-php-code-not-querying/#findComment-1187958 Share on other sites More sharing options...
Maq Posted March 15, 2011 Share Posted March 15, 2011 If you're getting it via HTTP(phpquestion.php?qid=2) then you would want to use something like: $qid = mysql_real_escape_string($_GET['qid']); Quote Link to comment https://forums.phpfreaks.com/topic/230749-php-code-not-querying/#findComment-1187959 Share on other sites More sharing options...
justlukeyou Posted March 15, 2011 Author Share Posted March 15, 2011 Thanks, I tried that but I got even more confused, should I be listing it in a different order? I cant how I can define qid. $query = "SELECT * FROM questions"; { $qid = mysql_real_escape_string($_GET['qid']); } if(isset($_GET['question'])) { $question = $_GET['question']; $query .= " WHEN qid is '$qid' LIMIT 0, 1"; } $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $question = $row['question']; $notes = $row['notes']; $category = $row['category']; $qid = $row['qid']; echo "$question | $notes | $category </br> "; } if ($_GET['qid'] == $qid ) { echo 'Sorry, this question is not available. Please visit our <a href="http://www.ukhomefurniture.co.uk">Homepage</a>.'; } function sanitizeString($string) { return mysql_real_escape_string($string); } $question = sanitizeString($_GET['qid']); $query .= " WHERE question is '%$qid%'"; // close connection mysql_close(); Quote Link to comment https://forums.phpfreaks.com/topic/230749-php-code-not-querying/#findComment-1187961 Share on other sites More sharing options...
Maq Posted March 15, 2011 Share Posted March 15, 2011 That's correct. I'm not sure what you're trying to do. What is happening now? What exactly do you want to happen? Quote Link to comment https://forums.phpfreaks.com/topic/230749-php-code-not-querying/#findComment-1187964 Share on other sites More sharing options...
justlukeyou Posted March 16, 2011 Author Share Posted March 16, 2011 HI, I want the page to display an individual from a database using the 'qid' (ID). Currently it dispalys everything in the database not by the number of the qid phpquestion.php?qid=7 Quote Link to comment https://forums.phpfreaks.com/topic/230749-php-code-not-querying/#findComment-1188079 Share on other sites More sharing options...
sasa Posted March 16, 2011 Share Posted March 16, 2011 change if(isset($_GET['question'])) { $question = $_GET['question']; $query .= " WHEN qid is '$qid' LIMIT 0, 1"; } to if(isset($qid) { //$question = $_GET['question']; $query .= " WHERE qid = '$qid'"; } Quote Link to comment https://forums.phpfreaks.com/topic/230749-php-code-not-querying/#findComment-1188083 Share on other sites More sharing options...
justlukeyou Posted March 16, 2011 Author Share Posted March 16, 2011 Brilliant, thanks mate I shall try that. Quote Link to comment https://forums.phpfreaks.com/topic/230749-php-code-not-querying/#findComment-1188109 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.