0
<?php
class Page {
    function getPage($urlOfPage){
        $result = mysql_query('SELECT category, title FROM rs-planet WHERE url = "'.  mysql_real_escape_string($urlOfPage));
        if(mysql_num_rows($result) === 0){
            header('HTTP/1.0 404 Not Found');
            exit;
        }
        else{
            return $page[] = mysql_fetch_array($result);
        }
    }
}

?>

And I have this errors:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\rs-planet\classes\page.php on line 6

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\rs-planet\classes\page.php on line 11 And I don't see the problem...

And if I do var_dump @ $result I get a boolean. (If it's founded @ the db it gives true, if it isn't it gives me false.)

PS. Sorry for my bad English, my main language is Dutch.

EDIT: var_dump(mysql_error) =

string(152) "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"index' at line 1"

4
  • 1
    No closing quotes for the url string value in your SQL query... when will people learn to debug their sql queries, even if they can't be persuaded to enter the 21st century and use prepared statements Commented Apr 10, 2012 at 21:56
  • Yup. Prepared statements are a gift from the SQL gods - USE THEM. :) Commented Apr 10, 2012 at 22:00
  • Using PDO, which encourages placeholders, is a really good idea. Commented Apr 10, 2012 at 22:22
  • possible duplicate of Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error Commented Jul 29, 2012 at 7:25

3 Answers 3

2

mysql_query returns false if the query cannot be executed (i.e. bad syntax). Try surrounding rs-planet with backticks:

$result = mysql_query('SELECT category, title FROM `rs-planet` WHERE url = "'.  mysql_real_escape_string($urlOfPage) . '"');

I'm thinking the - in the table name is screwing up the syntax.

EDIT: Also, PDO makes it much easier to debug these issues since you don't need to use mysql_error() to get the error message - it's included in the Exception thrown by PDO. http://php.net/manual/en/book.pdo.php

SECOND EDIT: Actually the problem is likely the missing quote. Fixed my code, but see the other answers...

Sign up to request clarification or add additional context in comments.

3 Comments

The query syntax is not fine, it is missing the close quote.
@citizenen Actually the missing quote is probably the problem, as noted in the other answers.
@Robin Sorry, had the concat outside the function call; fixed now.
1

mysql_query returns false on an error.

You have WHERE url=" but no closing quote after the url string.

Comments

1

mysql_query returns FALSE on error. Your error is that you are not closing the quote in your query. Try this:

        $result = mysql_query('SELECT category, title FROM rs-planet WHERE url = "'.  mysql_real_escape_string($urlOfPage).'"');

1 Comment

Also, try return $page instead of return $page[]. The former will return the array from mysql_fetch_array, the later will return the array from mysql_fetch_array in the first element of the $page array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.