duanping2809 2014-10-15 09:26
浏览 90

从mysql获取的数据显示2次

I am new to php. I want to fetch a particular data from mysql and display it in the label.I have tried a simple php coding.But it displays the fetched data two times(actually I have created 2 columns such as name and age in a table called test).Please help me.Here is the coding:

displayform.php

<body>
 <form method="post" name="display" action="display.php" >
 Enter the name you like to display the data from MySQL:<br>
<input type="text" name="name" />
<input type="submit" name="Submit" value="display" /> </form> 
</body>
</html>

display.php

<?php 
mysql_connect("localhost", "root", "") or die("Connection Failed");
 mysql_select_db("acp")or die("Connection Failed"); 
 $name = $_POST['name']; 
 $query = "select age from test where name = '$name'";
  $result = mysql_query($query); 
  while ($line = mysql_fetch_array($result)) 
  { 
  echo $line['age'];
   echo "<br>
"; 
   } 
?> 

The datas in table is

name=janani
age=25

The output is displayed as

25
25
  • 写回答

3条回答 默认 最新

  • drnxnbf199049 2014-10-15 09:28
    关注

    $line = mysql_fetch_array($result) ; //remove while loop

    echo $line['age'][0];

    try this ans this is work for one data fetch from table .. And may possible your result show two time because $name match two times in table so it fetch two record

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度