简体   繁体   English

SQL查询结果到PHP表中

[英]SQL query results into php table

I'm trying to place the results of a SQL query into a table for my wordpress site. 我正在尝试将SQL查询的结果放入wordpress网站的表中。

My table is called "statetable" 我的表叫做“状态表”

The columns are "State", "num-Asn" , and "Percent" 列为“状态”,“ num-Asn”和“百分比”

The following is my code in my custom php template. 以下是我的自定义php模板中的代码。

   global $wpdb;
   $results = $wpdb->get_results("SELECT * FROM statetable;");

   echo "<table>";
   foreach($results as $result){
   echo "<tr>";
   echo "<td>".$result->State."</td>";
   echo "<td>".$result->num-ASN."</td>";
   echo "<td>".$result->Percent."</td>";
   echo "</tr>";
   }
    echo "</table>";


    ?>

I get an empty table with one cell filled with zeros. 我得到一个空表,其中一个单元格填充了零。 I think this might not be the best way to set it up, or I have the wrong syntax for the foreach loop. 我认为这可能不是设置它的最佳方法,或者我的foreach循环语法错误。

"num-ASN" is an illegal identifier name. “ num-ASN”是非法的标识符名称。 Check what's in $results . 检查$results

通过使用以下命令检查$ results的内容

 var_dump($results);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM