在html中显示数据库中的特定值

时间:2013-12-12 20:57:25

标签: php html mysql sql

我是制作网页的新手。但我正在做一个主页,其中包含要插入我的数据库的表单。这没问题,我的问题是我想显示最后一行的特定列。我到目前为止的代码是:

<html>
<body>

<form action="insert.php" method="post">
 Publiceringsdag (OBS! En dag tidigare an foregaende):<br>
 <?php
 $con=mysqli_connect("localhost","rss","Habb0","kalender");
 if (mysqli_connect_errno())
 {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
 $lastPub = mysql_query("SELECT DISTINCT pub FROM event ORDER BY `id` DESC LIMIT 1")
 or die(mysql_error());
 echo $lastPub
 ?>
 <br>
 <input type="text" name="pub"><br>
 <input type="submit">
 </form>

 </body>
 </html>

3 个答案:

答案 0 :(得分:3)

实际上,使用已弃用的mysql_函数并不是一个好主意。请改为PDOMysqli

同时,在您当前的实现中,您只需在查询执行后获取数据:

$con = mysql_connect("localhost", "rss", "Habb0", "kalender");

if (mysql_connect_errno())    
    echo "Failed to connect to MySQL: " . mysqli_connect_error();

$lastPub = mysql_query("SELECT DISTINCT pub FROM event ORDER BY `id` DESC LIMIT 1")
  or die(mysql_error());

if($row = mysql_fetch_assoc($lastPub)))
    $result = $lastPub['pub'];

现在结果应该在您的$result变量中。

编辑:我刚刚注意到,在您的代码中,您同时使用mysqli_connectmysqli_connect_errnomysql_querymysql_error。但它们属于不同的PHP扩展。

答案 1 :(得分:0)

您必须先获取结果:

$lastPub = mysql_query("SELECT DISTINCT pub FROM event ORDER BY `id` DESC LIMIT 1")
 or die(mysql_error());
$result = mysql_fetch_array($lastPub);
echo $result['pub'];

答案 2 :(得分:0)

试试这个。

    <html>
<body>

<form action="insert.php" method="post">
 Publiceringsdag (OBS! En dag tidigare an foregaende):<br>
 <?php
 $con=mysql_connect("localhost","rss","Habb0") or die("Failed to connect to MySQL: " . mysql_error());
 $db=mysql_select_db("kalender",$con) or die("Failed to connect to MySQL: " . mysql_error());
 $result = mysql_query("SELECT DISTINCT pub FROM event ORDER BY `id` DESC LIMIT 1");
 $data = mysql_fetch_array($result);

 echo $data['pub'];
 ?>
 <br>
 <input type="text" name="pub"><br>
 <input type="submit">
 </form>

 </body>
 </html>