简体   繁体   中英

Select the last element in a table

I want to select the last element in my table. I did it fortunately but the problem is the fact that the insertion in my loop is always duplicate the entry in my second table. I have my scrip here and I don't know what is the problem.

I found a way to avoid by checking if the last entry has not been inserted yet but it doesn't work.

$p = $pdo->query("SELECT * FROM messagein ORDER BY `Id` DESC limit 1 ");

//  SELECT * FROM messagein WHERE Id = (select MAX('Id') from messagein)

echo "<table style='border-width: 1px solid black;'>";
echo "<tr>";
echo "<td> ID </td>";
echo "<td> SendTime </td>";
echo "<td> ReceiveTime </td>";
echo "<td> MessageFrom </td>";
echo "<td> MessageTo </td>";
echo "<td> MessageText </td>";
echo "<td> MessageType </td>";
echo "</tr>";

while ($data = $p->fetch())
    {

    // $p = $pdo->query("SELECT * FROM messagein ORDER BY `Id` DESC limit 1");
    // $data = $p->fetch();

    echo "<tr>";
    echo "<td>" . $data['Id'] . "</td>";
    echo "<td>" . $data['SendTime'] . "</td> ";
    echo "<td>" . $data['ReceiveTime'] . "</td> ";
    echo "<td>" . $data['MessageFrom'] . "</td> ";
    echo "<td>" . $data['MessageTo'] . "</td> ";
    echo "<td>" . $data['MessageText'] . "</td> ";
    echo "<td>" . $data['MessageType'] . "</td>";
    echo "</tr>";
    if (substr($data['MessageText'], 0, 3) == "lat")
        {
        $final = explode(" ", $data['MessageText']);
        $latitude = substr($final['0'], 4);
        $longitude = substr($final['1'], 5);
        $vitesse = substr($final['2'], 6);
        $temps = $data['ReceiveTime'];
        $exists = $pdo->prepare("SELECT * FROM donneesgps WHERE latitude = ? AND longitude = ? AND temps = ? AND vitesse = ?");
        $response = $exists->execute(array(
            $latitude,
            $longitude,
            $temps,
            $vitesse
        ));
        echo $response;
        if ($response == true)
            {
            $donneesgps = "INSERT INTO donneesgps (latitude, longitude, temps, vitesse)
                           VALUES ('$latitude', '$longitude', '$temps', '$vitesse')";
            $pdo->exec($donneesgps);
            }
        }
}

If you are sure you have only one row from the database, then don't use "while".

Just:

$data = $p->fetch();
if (!empty($data['Id'])){
  // Do your stuff
}

You will have probably some issue because you are using in your loop the same object $pdo by executing queries like that in the fly.

From my opinion, you should store before all the "SELECT" results into arrays, and then display it dynamically into HTML table (following MVC methods)

You check the return value of {pdostatement}->execute() which will return FALSE when the query is invalid and could not be executed. Since your query is apparently valid and was executed, it will almost always return TRUE which is completely unrelated to the row already existing or not.

You should SELECT COUNT(*) ... instead and see if the actual value of fetch is 0 or 1 (or more).

Alternatively, you could just make lat/long/temps/vitesse a primary key and INSERT IGNORE all the time.

In any case you should make the insert also a prepared statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM