MySQL Skipping First Row

Skip First Record From MySQL Result

Can you check this out:

SELECT * FROM table WHERE id = X ORDER BY id LIMIT 10 OFFSET 1;

Here is a Demo

In my Fiddle, there is 3 records with similar id(1). After executing query, output showing 2 records by skipping first record(from a total of 3)

MySQL skipping first row

Remove the line:

$row = mysql_fetch_array($result);

The while loop will grab the first row on the first iteration.

Resulting code:

<?php
$ordre = "nom";
$croissance = "ASC";

if(isset($_GET["ordre"])){
$ordre = $_GET["ordre"];
};

if(isset($_GET["croissance"])){
$croissance = $_GET["croissance"];
};

$con = mysql_connect('localhost','root','');
mysql_select_db('sdj_jeux', $con);
$sql = "SELECT * FROM jeux ORDER BY $ordre $croissance";
$result = mysql_query($sql, $con);

$couleurcompteur = 0;
while ($row = mysql_fetch_array($result)){
$couleurcompteur += 1;
if($couleurcompteur % 2){
$classe = "pale";
} else {
$classe = "fonce";
};
?>

Mysql or PHP skipping first row of database

Notice that you are reading the first row before the while:

// get rid of this line of code
//$row = mysql_fetch_array($result);
$data = array();
while($row = mysql_fetch_array($result)) {
$data[] = array($row['house'],$row['housePoints'],$row['ID']);
}

mysql query skips first row

remove the line $record2 = mysqli_fetch_array($rslt2);, which is placed before the for loop.

new code would be:

$sqlList ="select title from podcast order by date_pd desc limit 5";
$rslt2 = mysqli_query($conn, $sqlList) or die("Fail".mysqli_error($conn));
$nRows = mysqli_num_rows($rslt2);
for ($i=0; $i<$nRows; $i++) {
$record2 = mysqli_fetch_array($rslt2);
echo $record2["title"];
}

php skipping first row of result from mysqli

The problem is that you fetch the row twice.
Once with $result->fetch_assoc and another time with mysqli_fetch_row.

So you need to remove this line: $row = mysqli_fetch_row($result); because the row is already fetched.

I think it's not the last or first line that is missing, but half of the rows from table. In one case starting with first and in another case starting with second.

UPDATED

If you want to have numeric index you will need to use fetch_row. So change the code to:

$query = "select * from sear where wrrnt_no like '%".$text."%'";
$result = $connection->query($query);
echo "<table>";
while(($row = $result->fetch_row())!==null)
{

$warrent = $row[0];
//...the rest of echo here
}

But I recommend to use fetch_assoc and after that to use the name of the column like this: $warrent = $row['wrrnt_no']; (if that is what you need)

How can I exclude the first row from a result query?

Use the offset capability of limit. In MySQL, this would often be written as:

  ORDER BY CASE WHEN name LIKE '%$searchTerm%' then 1 else 2 end, points DESC
LIMIT 1, 9999999

The second argument is just a big number to be sure you get all the rows you want.

This can also be written as:

LIMIT 99999999 OFFSET 1

The OFFSET counting starts at 0 for the first row, so 1 skips the first row, starting at the second.

sql limit fail to display first row

When you call...

$aocol = count(mysqli_fetch_array($erg, MYSQLI_NUM));

this is in fact reading the first line and therefore it is no longer available for the following loop.

I would suggest restructuring your code so that the head is built up inside the main read loop, but only when $head is empty...

//table head
$head = "";

//only if style.css included-->irrelevant
echo '<div class="table"><table id="table">';
//output of table's body --> here must be the bug, I think
while($zeile = mysqli_fetch_array($erg, MYSQLI_NUM)) {
if ( empty ($head) ) {
//counts the amount of columns
$aocol = count($zeile);
for($x = 0; $x < $aocol; $x++) {
//legt alle Informationen des Feldes $x in $finfo, darunter auch den Namen der Spalte.
//puts all information of the field $x in $finfo, also the name of the column
$finfo = mysqli_fetch_field_direct($erg, $x);
//Schreibt die Kopfzeile der Tabelle
//writes the table's head
$head .= "<th>".$finfo->name."</th>";
}
//output of table's head
echo "<th>$head</th>";
}
//new tablerow
echo "<tr>";
//filling the row
foreach($zeile as $feld) {
//format for numbers
$ra = preg_match('/^\d+$/',$feld) ? ' align="right"' : '';
//displaying table data
echo "<td$ra>".$feld."</td>";
}
}

Also, if you changed MYSQLI_NUM to MYSQLI_ASSOC, then you can just use the key names as the column name and remove the extra API calls...

while($zeile = mysqli_fetch_array($erg, MYSQLI_ASSOC)) {
if ( empty ($head) ) {
foreach ( $zeile as $name => $value ) {
$head .= "<th>".$name."</th>";
}
//output of table's head
echo "<th>$head</th>";
}

Skip first row when overwriting the CSV file in PHP

$result =[];
$rows = 0;

foreach($sumArray as $key => $totalItems) {
$result[$totalItems][ ]= $sumArray1[$key];
$timeNeeded = implode(" ", $result[$totalItems]);
list($id1, $name1) = explode('_', $key);

$procenti = round(($perMin*$totalItems)/($timeNeeded/$precentEff),2);
$procentiCon = $procenti . " %";
$pickingEffRes = [$name1 , $totalItems, $procentiCon];

if($rows == 1) continue;

fputcsv($out, $pickingEffRes);
$rows++;
}

how to skip top rows when importing csv to mysql workbench

Easier to use LOAD DATA like below:

LOAD DATA INFILE 'MYFILENAME.CSV'
INTO TABLE `mytablename`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 3 LINES;


Related Topics



Leave a reply



Submit