Arnar Freyr Kristinsson Arnar Freyr Kristinsson - 26 days ago 5
MySQL Question

Why is this php code not fetching the first row from the mysql database?

It seems that this php code fetches all the rows in the database, but does not fetch the very first row. If you call to the procedure in the database (see the procedure below) it shows all the rows as expected. The database has four columns and in the code it's used echo to print out the data from the second row, What's happening is that the first record does not display, so it's not fetched I think. So, what could be the problem?

//this is the broken part of my code
$statement = $conn->prepare('call GetImage()');

$statement->execute();

while($row = $statement->fetch()){

$dest_slash = str_replace('-', '/', $row[2]);
$dest_slash_backslash = str_replace(';','\\', $dest_slash);
$replace_root = str_replace($_SERVER['DOCUMENT_ROOT'],'',$dest_slash_backslash);
$row[2] = $replace_root;

$images_paths_with_num[] = array($image_count,$row[2]);
$image_count++;
echo $row[1];
}


This is the stored procedure that I'am using:

CREATE DEFINER=`0901972749`@`%` PROCEDURE `GetImage`()
begin
select imageID,imageName,imagePath,imageText
from Images limit 500;
end

Answer

See this procedure (The same as in the question):

   CREATE DEFINER=`0901972749`@`%` PROCEDURE `GetImage`()
begin
    select imageID,imageName,imagePath,imageText
    from Images limit 500;
end

It was needed to add an order by statement in this case "order by imageID" to it as things were not displaying in the right order. To be sure about the data will display in the right order, we need to use an order by clause. But, stored procedures may not be the right solution for this. Sorry about that :)