proud Posted January 24, 2010 Share Posted January 24, 2010 I'm having a problem in retrieving a record from my books table, which is a book titled 'C++'.. The code below retrieved any (title,author) record from my books table except for the record which contains the book with the C++ title.. So has the '+' sign got anything to do with that? Any ideas on how I can solve this problem? <?php $book_title=$_GET['book_title']; $query = mysql_query("SELECT * FROM books where title='$book_title' ORDER BY id asc ") or die(mysql_error()); while($rows = mysql_fetch_array( $query )) { ?> <table> <tr> <td>Book Title</td> <td> <?php echo $rows['title']; ?> </td> </tr> <td>Book Author</td> <td> <?php echo $rows['author']; ?> </td> </tr> </table> <? } ?> By the way, just to add: In another query where I did not use $_GET to get the value of the book title from the URL, the C++ record was retrieved successfully.. So I might add that it also may have a problem with $_GET and '+' sign Quote Link to comment https://forums.phpfreaks.com/topic/189607-cant-retrieve-a-record-with-a-sign/ Share on other sites More sharing options...
jl5501 Posted January 24, 2010 Share Posted January 24, 2010 Your query has a variable between ' ' which will not be evaluated. I tend to construct such queries differently with sprintf as I personally find that easier. Also, I would define the query and its result separately for easier debugging So I would write $query = sprintf("SELECT * FROM books where title='%s' ORDER BY id asc",$book_title); $result = mysql_query($query) or die(mysql_error()); you could also construct the query like this $query = 'SELECT * FROM books where title=\''.$book_title.'\' ORDER BY id asc'; Quote Link to comment https://forums.phpfreaks.com/topic/189607-cant-retrieve-a-record-with-a-sign/#findComment-1000735 Share on other sites More sharing options...
Daniel0 Posted January 24, 2010 Share Posted January 24, 2010 Your query has a variable between ' ' which will not be evaluated. Yes it will, the single quote is part of the SQL syntax, not part of PHP's syntax. The variable appears inside a PHP double quoted string, so it will be substituted with its value before it's given to mysql_query(). As for the problem, you can't do like foo.php?book_title=C++ because a + will be decoded as a space. Quote Link to comment https://forums.phpfreaks.com/topic/189607-cant-retrieve-a-record-with-a-sign/#findComment-1000741 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.