Jump to content

Can't retrieve a record with a '++' sign


proud

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/189607-cant-retrieve-a-record-with-a-sign/
Share on other sites

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';

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.