HansWorst
HansWorst

Reputation: 11

I want to show the last record (row) from my database

but I dont know how and where to place the sql code in php, could someone please help me? I know its something like SELECT * FROM form ORDER BY 'Klant_id' ASC LIMIT 1

<html>
    <header>
    <link rel="stylesheet" href="css/style.css" type="text/css"  />
    </header>
    <body>

     <?php 
             //makes an connection to the db

             mysql_connect("localhost", "root", '') or die(mysql_error());
             mysql_select_db('databaseimage') or die(mysql_error());

             $data = mysql_query("SELECT * FROM form ORDER BY 'Klant_id' ASC LIMIT 1")
             or die(mysql_error()); 

             echo "<table border cellpadding=3>"; 
             while($info = mysql_fetch_array( $data)) 
             { 
             echo "<tr>";    
             echo "<th>surname:</th> <td>".$info['Surname'] . "</td> "; 
             echo "<th>insertion:</th> <td>".$info['Insertion'] . "</td> ";
             echo "<th>initials:</th> <td>".$info['Initials'] . "</td> ";
             echo "<th>name:</th> <td>".$info['Name'] . "</td> "; 
             echo "<th>sex:</th> <td>".$info['Sex'] . "</td> ";
             echo "<th>adress:</th> <td>".$info['Adress'] . "</td> ";
             echo "<th>postcode:</th> <td>".$info['Postcode'] . "</td> ";
             echo "<th>location:</th> <td>".$info['Location'] . "</td> ";
             echo "<th>private phone:</th> <td>".$info['Private_phone'] . "</td> ";
             echo "<th>mobile phone:</th> <td>".$info['Mobile_phone'] . "</td> ";
             echo "<th>work phone:</th> <td>".$info['Work_phone'] . "</td> ";
             echo "<th>private email:</th> <td>".$info['Private_email'] . "</td> ";
             echo "<th>work email:</th> <td>".$info['Work_email'] . "</td> ";

             } 
             Print "</table>"; 
             ?> 
    </body>
    </html>

Upvotes: 0

Views: 2787

Answers (3)

John Woo
John Woo

Reputation: 263693

Don't wrap the column Klant_ID with single quotes. Single quote is very different from backtick.

SELECT * 
FROM form tablename 
ORDER BY Klant_id DESC 
LIMIT 1

or

SELECT * 
FROM form tablename 
ORDER BY `Klant_id` DESC 
LIMIT 1

Differences:

Backticks ( ` ) are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword.

Single quotes ( ' ) should be used for string values like in the VALUES() list.

Double quotes are supported by MySQL for string values as well, but single quotes are more widely accepted by other RDBMS, so it is a good habit to use single quotes instead of double.

Upvotes: 2

Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25745

when you have primary key with auto_increment, you should fetch it using DESC descending order. in order to fetch the last inserted record.

try this.

SELECT * FROM form ORDER BY `Klant_id` DESC LIMIT 1

Upvotes: 1

WatsMyName
WatsMyName

Reputation: 4478

if you have primary key with auto_increment set. You can do it in two ways

First

SELECT * FROM form WHERE Klant_id=(select MAX(Klant_id) FROM form)

Second

SELECT * FROM form ORDER BY Klant_id DESC LIMIT 1;

Upvotes: 0

Related Questions