Teknikk
Teknikk

Reputation: 251

MYSQLI Returns only 1 value while there's 2

I've started making a Video script that loads the videos using MySql and I am using Mysqli. However, There's 2 Rows that it should post, but it only post the second none, not the first one. It loads the results using "Brand" so if there's 2 rows named "Test", it only loads the second one, but not the first one. So, what is causing this? I've tried with 3 rows, and it did not include the first row. Code

<?php
{ /* Global Data */
ini_set('display_errors', 0); 
ini_set('error_reporting', -0);
$GetPath = $_GET['b'];
$SqlUser = "root";
$SqlPass = "**Private**";
$SqlHost = "localhost";
$SqlData = "heisteknikk";
}
{ /* Mysql Connect */
$Sql = new mysqli($SqlHost, $SqlUser, $SqlPass, $SqlData);
if ($Sql->connect_error) { die("Sorry, Could not connect (".$Sql->connect_errno.") ".$Sql->connect_error);}
}
{ /* Test */

$Brand = $Sql->real_escape_string($GetPath);
$SqlData = "SELECT * FROM videos WHERE Brand = '".$Brand."'";
$SqlQuery = $Sql->query($SqlData);
if (!$SqlQuery) {
    echo $Sql->error;
}
if ($SqlQuery->num_rows == 0) {
die("Nothing was found");
}
$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);
echo "<table border='1'>";
while ($Heis = $SqlQuery->fetch_assoc()) {
echo "
<tr><td>".$Heis['Brand']."</td><td>".$Heis['Name']."</td><td>".$Heis['Location']."


";
}
echo "</table>";
$Sql->close();
}
?>

Upvotes: 0

Views: 276

Answers (3)

Dancrumb
Dancrumb

Reputation: 27529

In your code

$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);
echo "<table border='1'>";
while ($Heis = $SqlQuery->fetch_assoc()) {

you're fetching a row (with fetch_array), throwing it away and then fetching another row (with fetch_assoc). That's probably why you're only seeing one row instead of two

Upvotes: 3

Jeremy Harris
Jeremy Harris

Reputation: 24549

Comment out (or better yet, remove) this line:

$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);

It is grabbing the first row...then you aren't doing anything with $Data and then grabbing the second (and consecutive rows) for display in your while loop. Commenting out that line above will make your loop grab and display them starting at the first one.

Upvotes: 3

raina77ow
raina77ow

Reputation: 106375

This line causes the bug:

$Data = $SqlQuery->fetch_array(MYSQLI_ASSOC);

With this line you effectively throw out the first row of the result set, as you don't process $Data at all in the code below. Just remove it, and your script should work fine (I assume that closing </td></tr> sequence was lost when pasting the code, am I right?)

Upvotes: 4

Related Questions