Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.