2010-12-05 86 views
0

這絕對是一個初學者的問題。有兩個問題。即使我從我的表中刪除行(我使用的是phpmyadmin),我的MYSQL表中的id(設置爲autoincrement)也不斷增加。至於其他問題,我似乎無法找到用戶最近輸入的行的方式。代碼回顯MYSQL中的所有現有行。 我用粗體顯示了最相關的代碼部分。當我只想輸出一行時輸出整個表格的MYSQL/PHP

<?php 
//establish connection to mysql 
$con = mysql_connect("localhost","root",""); 
if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

/*retrieve user input from input form 
and initialize variables */ 
$Word1 = $_POST["Word1"]; 
$Word2 = $_POST["Word2"]; 
$Word3 = $_POST["Word3"]; 
$Word4 = $_POST["Word4"]; 
$Word5 = $_POST["Word5"]; 

//select db 
mysql_select_db("madlibs", $con); 

//insert user input for word 1 
$sql = "INSERT INTO test (Word1, Word2, Word3, Word4, Word5) 
VALUES 
('$Word1', '$Word2', '$Word3', '$Word4', '$Word5')"; 
if(!mysql_query($sql,$con)) 
{ 
    die('Error: ' . mysql_error()); 
} 

$result = mysql_query ("SELECT * FROM test"); 

/* take note here */ 
while($row = mysql_fetch_array($result)) 
{ 
    echo $row['Word1'] . " " . $row['Word2'] . " " . $row['Word3'] . " " . 
     $row['Word4'] . " " . $row['Word5'] . " " . $row['id']; 
    echo "<br />"; 
} /* take note here */ 
mysql_close($con); 
?> 
+0

你可以把整個代碼放在`between` – andrewk 2010-12-05 02:48:03

回答

2
$result = mysql_query ("SELECT * FROM test order by id desc limit 1"); 

至於你的ID的問題......這是IDS是如何工作的。他們不填補空白。

附註:永遠不要將用戶提交的數據直接放入查詢字符串中。總是使用mysql_real_escape_string()來轉義它們。

0
SELECT * FROM test ORDER BY Id DESC LIMIT 1 
相關問題