简体   繁体   English

PHP MYSQL列表中的表数据问题

[英]PHP MYSQL List Data inside Table issue

below is my PHP code to display all the data in my database in a table.. 下面是我的PHP代码,用于在表中显示数据库中的所有数据。

if I use this code below to login and select DB rather than using PDO: 如果我在下面使用此代码登录并选择数据库而不是使用PDO:

 mysql_connect("localhost","root","12345678") or die(mysql_error());
   mysql_select_db("clubresults") or die(mysql_error());
   $query=mysql_query("SELECT * from events ORDER By EventID ASC") or die(mysql_error());

The output gives me a table with all the data listed correctly. 输出为我提供了一张表格,其中正确列出了所有数据。

However If I use PDO.. it gives me this Error: 但是,如果我使用PDO ..,则会出现此错误:

Warning: mysql_num_fields() expects parameter 1 to be resource, object given in C:\xampp\htdocs\clubresults\listevents.php on line 52

Warning: mysql_fetch_row() expects parameter 1 to be resource, object given in C:\xampp\htdocs\clubresults\listevents.php on line 58

Below is the full php code to grab data to and place in a table (Doesn't work) anyone able to point out where i have a mistake? 以下是完整的php代码,可将数据捕获到表中并放置在表中(无效),任何人都可以指出我在哪里出错了? because I can't seem to figure it out myself.. All suggestions are really appreciated! 因为我似乎无法自己弄清楚所有建议。 Thanks! 谢谢!

$pdo = new PDO('mysql:host=localhost;dbname=clubresults', 'root', '12345678');
        #Set Error Mode to ERRMODE_EXCEPTION.
        $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  


   $query=$pdo->query('SELECT * from events ORDER By EventID ASC');


   <<<<<<<<<<<<<<<<THIS BIT WORKS IF NOT USING PDO>>>>>>>>>>
$numfields = mysql_num_fields($query);
       print("<table border=\"1\">\n<tr>\n");
       for ($i=0; $i<$numfields; $i++) {
          printf("<th>%s</th>\n", mysql_field_name($query,$i));
       }
       print("</tr>\n");
       while ($row = mysql_fetch_row($query)) {
          print("<tr>\n");
          for ($i=0; $i<sizeof($row); $i++) {
             printf("<td><a href=\"index.php?ID=%s\">%s</a></td>\n", $row[0],$row[$i]);
          }
          print("</tr>\n");
       }
       print("</table>\n");

You won't be able to use mysql_num_fields() or mysql_fetch_row() with PDO -- they're part of a different API, the old MySQL extension. 您将无法通过PDO使用mysql_num_fields()mysql_fetch_row() ,它们是不同的API(旧的MySQL扩展)的一部分。

Instead, since you have called PDO::query() , call $query->fetch() . 相反,由于调用了PDO::query() ,因此请调用$query->fetch() Use the first row's array keys to build your table heading. 使用第一行的数组键构建表标题。

$query = $pdo->query('SELECT * from events ORDER By EventID ASC');
$rowset = array();

if ($query) {
  while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    // Build array of rows
    $rowset[] = $row;
  }    

  // Output header first
  $headrow = $rowset[0];
  print("<table border=\"1\">\n<tr>\n");
  // Use $rowset[0] to write the table heading
  foreach ($headrow as $col => $val) {
    printf("<th>%s</th>\n", $col);
  }
  print("</tr>");

  // Then output table rows.
  // Outer loop iterates over row
  foreach ($rowset as $row) {
     print("<tr>");
     // Inner loop iterates over columns using $col => $val
     foreach ($row as $col => $val) {
        // We don't know your column names, but substitute the first column (the ID) for FIRSTCOL here
        printf("<td><a href=\"index.php?ID=%s\">%s</a></td>\n", $row['FRISTCOL'],$val);
     }
     print("</tr>");
  }
}
print("</table>");

Addendum 附录

Generally, I would recommend against using SELECT * and dynamically writing out your column headings. 通常,我建议不要使用SELECT *并动态地写出列标题。 Instead, be explicit about the columns you request in your SELECT statement, and be explicit about them in the table you write out. 相反,请明确说明您在SELECT语句中请求的列,并明确说明您写出的表中的列。 That way, you can be deterministic about the order the columns appear in, and you won't ever get caught by having added columns to the table which should not have appeared in output on screen. 这样,您就可以确定列的显示顺序,并且永远不会因为向表中添加了本不应该出现在屏幕输出中的列而陷入困境。

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

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