-2

this ought to be simple but am yet to find an answer for it (i have searched the questions in stackoverflow). am on php and i have a table books on mysql. What i want is a list displayed in my webpage with these specifics on a table created by php. I know the mysql code:

SELECT title FROM books WHERE category='currently reading' 

applying that on php has brought this error, Parse error: syntax error, unexpected 'currently' (T_STRING)

Here is my php code:

<?php
include('include/databaseconnection.php');
include('include/insertingbooks.php');
// selecting data 
$result = mysql_query ('SELECT title FROM books WHERE category='currently reading'';);
//("SELECT title FROM books WHERE category LIKE $currently");
//opening table tag
echo "<table border = 1px>";
while ($data = mysql_fetch_array($result)) {
// printing table row
echo'<tr>';
echo '<td>'.$data['title'].'</td>';
echo'</tr>'; // closing table row
}
echo '</table>';
?>

If i decide to leave out WHERE clause, it works perfectly except it displays all the books. The options i have tried already

  • using WHERE category LIKE $category while setting up a variable $category = "currently reading"; but it dint work.
  • trying to link it to the form i got the the values of category from by adding include('include/insertingbooks.php'); (which contains $category =$_POST ['category'];) and trying to put $category.
  • using WHERE not but it didnt work at all.
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 20 '15 at 12:07
  • wrong types of quotes in select. error checking would have signaled the syntax error. – Funk Forty Niner Apr 20 '15 at 12:08

3 Answers3

1

You have bad quotes and an extra semi-colon here -

$result = mysql_query ('SELECT title FROM books WHERE category='currently reading'';);

Change to this (note the double quotes) -

$result = mysql_query ("SELECT title FROM books WHERE category='currently reading'");

In addition, you should stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and use PDO.

You should also add MySQL error checking to your queries and connections.

Using or die(mysql_error()) to mysql_query().

For example:

$result = mysql_query ("SELECT title 
                        FROM books 
                        WHERE category='currently reading'") 
    or die(mysql_error());
  • Which would have caught and displayed the syntax error.
Community
  • 1
  • 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • There is one most error on code that is $result = mysql_query ("SELECT title FROM books WHERE category='currently reading'";); one extra semicolon – Sourabh Apr 20 '15 at 12:14
  • @Sourabh the extra semi-colon shouldn't be a problem; it's valid syntax. Had the OP's code been set in a loop with an additional query, then yeah, that would break it, but in this case it won't. The semi-colon is a terminating character and won't affect the existing query. – Funk Forty Niner Apr 20 '15 at 12:25
  • @ManiEl-darreiny this is the answer you should have accepted. Plus, it gives an explanation and was submitted first. *Still doesn't work*. so why did you accept the other answer then? It's the same thing. Do you know what the word "ethics" means? – Funk Forty Niner Apr 20 '15 at 12:58
  • sure like to have a puff of whatever they're smoking @JayBlanchard – Funk Forty Niner Apr 20 '15 at 13:06
  • My Bad @Fred-ii- , well all i did was copy paste, weirdly it worked on the other one. Rereading later i noticed it was the same, that's why i put a tick to this one. Am both new to php and stackoverflow, and i take my words back. And thanks for the help and support. – Mani El-darreiny Apr 20 '15 at 13:24
  • You're welcome @ManiEl-darreiny welcome to the wonderful world of coding ;-) – Funk Forty Niner Apr 20 '15 at 13:37
0

Use this code:

<?php
include('include/databaseconnection.php');
include('include/insertingbooks.php');
// selecting data 
$result = mysql_query ("SELECT title FROM books WHERE category='currently reading'");
//("SELECT title FROM books WHERE category LIKE $currently");
//opening table tag
echo "<table border = 1px>";
while ($data = mysql_fetch_array($result)) {
// printing table row
echo'<tr>';
echo '<td>'.$data['title'].'</td>';
echo'</tr>'; // closing table row
}
echo '</table>';
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Sourabh
  • 500
  • 2
  • 14
  • 3
    Why should the OP try this? Please add an explanation of what you did and why you did it, not only for the OP but for future visitors to SO. – Jay Blanchard Apr 20 '15 at 12:11
  • 1
    Ok this works great, but i don't see my error. please explain. And thanks so much – Mani El-darreiny Apr 20 '15 at 12:45
  • Sourabh, you didn't respond to a comment I made on another post of yours, and you've not responded here to Jay and the OP. Please try to reply to comments. – halfer Apr 26 '15 at 08:42
0

there is only quotes problem Please replace your query to:

$result = mysql_query ("SELECT title FROM books WHERE category='currently reading'";);

Please note you can not use same quotes eg. If you are using double quotes then use single quotes inside and if single quotes then double code inside

Ravinder Kumar
  • 902
  • 10
  • 29