1

I have a weird problem where mysql_ limits results from a query, but only when it's in a while loop & adding ORDER BY listing_name ASC. Here's an example:

$listings = mysql_query("SELECT * FROM listings");
$listingcount = mysql_num_rows($listings);

$listingcount returns 157 rows in the listings table.

And here's what happens in the while loop.

$listings = mysql_query("SELECT * FROM listings ORDER BY listing_name ASC");
while($listingresult = mysql_fetch_assoc($listings)){
//grab some data from the query using $listingresult
//e.g: $somedata = $listingresult['listing_name'];
echo "some data here:". $somedata;
}

This only echos 97 rows. None of the values in listing_name are NULL, empty etc they all contain & start with letters.

What am I missing here? It doesn't seem right

nulled
  • 383
  • 1
  • 5
  • 20
  • 2
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 11 '16 at 19:52
  • 1
    Is it sufficient to just upgrade to `mysqli_` (procedural way)? – nulled May 11 '16 at 19:55
  • As long as you use prepared statements, yes. – Jay Blanchard May 11 '16 at 19:55
  • I'd still like to work out a solution to my problem however. Is there anything blatantly obvious here? (aside from `mysql_` functions, it shouldn't make a difference with regards to the simple query, should it?) – nulled May 11 '16 at 20:03
  • Nothing that you've shared here would help us to help you to solve the problem. – Jay Blanchard May 11 '16 at 20:04
  • Aside from a database connection this is everything. What else could I share? – nulled May 11 '16 at 20:07
  • I don't know that there is anything you can share. Without being able to query the database ourselves it would be nearly impossible for us to guess at the issue. Perhaps you could create a SQLFiddle with your database, but even at that we would not be able to run code against it. – Jay Blanchard May 11 '16 at 20:11
  • @JayBlanchard In this specific query would you agree he wouldn't need to do a prepared statement? (assuming he isn't doing this query more than once, and seeing that he isn't using user output) – Webeng May 11 '16 at 20:20
  • 1
    You're right @Webeng, but for consistency sake if you're going to do one as a prepared statement, do them all that way. – Jay Blanchard May 11 '16 at 20:21
  • 1
    @JayBlanchard Completely agree, and I might even recommend making a function that receives an sql statement and an array for the parameters that need binding, since repetitive code is one of the killers of beautiful code – Webeng May 11 '16 at 20:24

0 Answers0