Asked  7 Months ago    Answers:  5   Viewed   34 times

I have seen other people with that problem but the solutions I've seen aren't helping me, or I don't know how to use them :P

<?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);
$row = mysql_fetch_array($result);  

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

My code is skipping the first row of my database and I don't understand why.

 Answers

71

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";  
    };
?>
Wednesday, March 31, 2021
 
TheFrack
answered 7 Months ago
95

Since these two inserts are executed independently, another program running concurrently might see the database in a state where the first insert is done but the second isn't.

Whether this is a problem or not depends on the application logic. In your case it's hard to tell without additional information. Probably not. A financial transactions involving two accounts is an example where this is a problem: you don't want the sum of all account balances to be wrong at any time.

If you think you need this, you can make the operation atomic at the cost of performance: another program will either see the database before the first insert, or after the second insert. It works like this:

$result = FALSE;
if (mysql_query('BEGIN')) {
    if (mysql_query($query1) &&
        mysql_query($query2))
        $result = mysql_query('COMMIT'); // both queries looked OK, save
    else
        mysql_query('ROLLBACK'); // problems with queries, no changes
}

The storage engine has to support transactions, i.e., it has to be InnoDB. Otherwise this will silently not work.

Saturday, May 29, 2021
 
themihai
answered 5 Months ago
14

You are calling mysql_fetch_array twice... once before the loop then once while looping.

If you need a seed row for use in building your header row you might be better served with a do.. while loop here.

$query = "SELECT * FROM members";
$results = mysql_query($query);
$row = mysql_fetch_assoc($results);

//echo my <table> start and headings;

do  
{
    echo "<tr><td>".$row['Last Name']."</td>";
    echo "<td>".$row['First Name']."</td>";
    echo "<td>".$row['Middle Name']."</td>";
    echo "<td>".$row['Sfx']."</td>";
    echo "<td>".$row['Prf']."</td>";
    echo "<td>".$row['Spouse/SO']."</td>";
    echo "<td>".$row['Ancestor']."</td>";
    echo "<td>".$row['Status']."</td>";
    echo "<td>".$row['Address 1']."</td>";
    echo "<td>".$row['Address 2']."</td>";
    echo "<td>".$row['City']."</td>";
    echo "<td>".$row['ST']."</td>";
    echo "<td>".$row['Zip 5']."</td>";
    echo "<td>".$row['Zip 4']."</td>";
    echo "<td>".$row['Home Phone']."</td>";
    echo '<td><a href="mywebsite/mypage.php?id=' . $row['id'] . '">Bio</a></td></tr>';
} while ($row = mysql_fetch_assoc($results));

echo "</table><hr>";
Wednesday, July 7, 2021
 
Skipper
answered 4 Months ago
45
SELECT c.*, d.*
FROM country c 
  INNER JOIN ducks d 
    ON d.id =                         --- guessing the ducks Primary Key here
       ( SELECT dd.id                 --- and here  
         FROM ducks dd
         WHERE c.id = dd.country_id
         ORDER BY dd.rating DESC
         LIMIT 1
       )

An index on (country_id, rating, id) for MyISAM table or (country_id, rating) for InnoDB table, would help.

This query will show only one duck per country, even with more than one having the same rating. If you want ducks with tied rating to appear, use @imm's GROUP BY answer.

Friday, August 13, 2021
 
butterwagon
answered 3 Months ago
72

You're using a double-quoted string, so the rn will be seen as literal carriage return and line feed characters. You'll need to double-escape them as well: \r\n.

The "on line 2" in the error message is evidence of this - there's no actual second line if your query, but because of the embedded newline/carriage return, there is once it gets to MySQL.

Thursday, September 2, 2021
 
Yrtymd
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :