0

但我不知道如何以及在哪里将 sql 代码放在 php 中,有人可以帮我吗?我知道它像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>
4

3 回答 3

2

不要Klant_ID用单引号将列括起来。单引号与反引号非常不同。

SELECT * 
FROM form tablename 
ORDER BY Klant_id DESC 
LIMIT 1

或者

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

差异:

Backticks( ` ) 用于表和列标识符,但仅当标识符是MySQL 保留关键字时才需要。

Single quotes( ' ) 应该用于VALUES()列表中的字符串值。

Double quotesMySQL 也支持字符串值,但单引号被其他 RDBMS 更广泛地接受,因此使用单引号而不是双引号是一个好习惯。

于 2012-09-03T07:20:02.733 回答
1

当您拥有带有 auto_increment 的主键时,您应该使用DESC降序获取它。为了获取最后插入的记录。

尝试这个。

SELECT * FROM form ORDER BY `Klant_id` DESC LIMIT 1
于 2012-09-03T06:51:44.107 回答
0

如果您有设置了 auto_increment 的主键。你可以通过两种方式做到这一点

第一的

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

第二

SELECT * FROM form ORDER BY Klant_id DESC LIMIT 1;
于 2012-09-03T07:15:59.743 回答