douluhaikao93943 2010-12-05 02:42
浏览 54
已采纳

当我只想输出一行时,MYSQL / PHP输出整个表

This is definitely a beginner's question. There are two issues. The id in my MYSQL table (set to autoincrement) keeps going up, even though I delete rows from my table (I'm using phpmyadmin). As for the other issue, I can't seem to find a way to work with the row most recently entered by the user. The code echos all existing rows from MYSQL. I've bolded the most pertinent section of code.

<?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);
?>
  • 写回答

2条回答 默认 最新

  • dounieqi6959 2010-12-05 02:44
    关注
    $result = mysql_query ("SELECT * FROM test order by id desc limit 1");
    

    As for your id question...that's how ids work. They don't fill in gaps.

    On a side note: Never ever put user submitted data directly into the query string. Always escape them with mysql_real_escape_string().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应