MySQL skipping first row and I am NOT double fetching my set

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mrvic
Forum Newbie
Posts: 1
Joined: Thu May 21, 2015 10:21 pm

MySQL skipping first row and I am NOT double fetching my set

Post by mrvic »

Hello.

My name is Victor Rivarola. I am currently working on a system that is currently eating the first row. Yes, I have made sure that I am not fetching the rows at an incorrect time.

But first, I know that the mysql_* functions have been depreciated and should not be added to new code. Well, this is not new code but existing code I am maintaining.

This is my code:

Code: Select all

<?php
include_once("config.php");

ob_start('mb_output_handler');
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
header("Content-type: text/html; charset=utf-8");

// These combined checks both verify the validity of the input 
// and prevent against SQL injection
$city = @$_GET['city'] or die("Required parameter(s) is missing.");
is_numeric($city) ? true : die("Improper city code received.");

$sql  = "
SELECT schools_ID
,name
FROM InfResp_schools
WHERE cities_ID=$city
ORDER BY name ASC
";

$result = mysql_query($sql) or die(mysql_error());

while ($row = mysql_fetch_assoc($result)) {
	echo "<options value=\"".$row["schools_ID"]."\">".$row["name"]."<option>\n";
}

mysql_free_result($result);
DB_disconnect();

?>


The InfResp_schools table has 3 fields in it, schools_ID which is the primary key, cities_ID which is a foreign key to a different cities table, and the name field whose contents should be obvious.

This is fed using AJAX and jquery right into a select box. This part is working perfectly.

I have searched for this problem's solution for a long time. While I have found similar problems, it is always caused by people inserting a superfluous mysql_fetch_assoc(). As you can see, I am not doing that.

I have checked the above SQL command for various city codes in my database. The result is perfect. So I know that my SQL is fine. It must be a problem in my PHP.

Anyway, the result is always the same. The first school gets omitted for all cities in my database. Does anyone has an idea why?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MySQL skipping first row and I am NOT double fetching my

Post by Christopher »

If you comment out the lines at the top for output buffering and headers, what does it output then?
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: MySQL skipping first row and I am NOT double fetching my

Post by requinix »

Invalid HTML:

Code: Select all

<options
And the "closing" tag is missing the slash.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MySQL skipping first row and I am NOT double fetching my

Post by Christopher »

:roll:
(#10850)
Post Reply