duandan9680 2012-07-01 01:05
浏览 63
已采纳

mysql显示数据库数据

I'm trying to display comments on my site from my database. I know that my connection is working, as shown by $test (which displays "Array").

How do I convert the array into usable information? This is my best shot, but it doesn't display anything:

<?php
$connect;

$sql_get_topic_info = "SELECT * FROM wall ORDER BY time ASC"; 
$res_get_comments_info = mysql_query($sql_get_comments_info); 
$num_get_comments_info = mysql_numrows($res_get_comments_info); 
$test = mysql_fetch_array(mysql_query($sql_get_topic_info)); //just to make sure the connection is working
echo $test; //displays "Array"


//Runs comment loop 
$i=0; 
while ($i < $num_get_comments_info) { 

$sel_comments_info_time = mysql_result($res_get_comments_info,$i,"time"); 
$sel_comments_info_message = mysql_result($res_get_comments_info,$i,"message"); 
$sel_comments_info_company = mysql_result($res_get_comments_info,$i,"company"); 

echo "<li>Company: $sel_comments_info_company<br/>"; 
echo "Comment: $sel_comments_info_message <br/>";
echo "$sel_comments_info_time"; 
echo "</li>"; 

$i++; 
} 
?>

Updated script: (from answer below)

$result = mysql_query("SELECT company, message FROM wall");

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("company: %s  message: %s", $row["company"], $row["message"]);
}

mysql_free_result($result);

which prints:

company: company 1 message: message 1company: company 2 message: message 2company: company 3 message: message 3
  • 写回答

1条回答 默认 最新

  • douyun8885 2012-07-01 01:07
    关注

    Usage of mysql_ is discouraged, you should be using mysqli_ functions instead.

    From mysql_fetch_array documentation:

    $result = mysql_query("SELECT id, name FROM mytable");
    
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        printf("ID: %s  Name: %s", $row[0], $row[1]);  
    }
    

    Use print_r($test) to see the array to see its format. Then you can use the loop above to display the parts of it in whatever order you want. Each column from your database is in the resulting row

    row = [col1, col2, col3...]
    

    You can access them using array indices $row[0], $row[1].... You can see the order by either looking at the order of columns in the database or seeing what it is in the print_r. Then you can just echo it out in the order you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答