【问题标题】:Using mysql to pull database information into html tables inside PHP script使用 mysql 将数据库信息拉入 PHP 脚本中的 html 表中
【发布时间】:2013-12-18 21:51:07
【问题描述】:

首先,我真的希望我不会因为提出这个问题而被打败,但我愿意冒险,以便被指出正确的方向,以便我可以学习。

我有一个 .php 脚本,在我的 PHP 脚本中,我编写了连接 MySQL 数据库的代码。

问题来了:

我有一张桌子,用于非常简单地展示食物。目前我有:

<table width="1024" border="0" cellpadding="10" cellspacing="5" align="center">
    <tr>
    <td colspan="2"><font>Item Name</font></td>
    <td colspan="2"><font>Item Name</font></td>
    <td colspan="2"><font>Item Name</font></td>
    </tr>

它继续与其他表格行元素一起使用......

其中显示“项目名称”,我如何输入代码以提取数据库信息。我拥有的数据库名称是“TEST”,我希望每个项目名称都能从“ITEM”表中列出不同的项目。

我们将不胜感激任何帮助。

【问题讨论】:

  • 来吧,在寻求帮助之前先尝试一些事情......!!
  • 相信我。我已经尝试了很多,但一无所获。感谢您的评论。
  • 这是非常基本的,不适合 Stack Overflow。您想查看系统地教授这些步骤的教程。
  • 如果这是一个问题,当然!知道有什么好的教程吗?

标签: php html mysql database html-table


【解决方案1】:

您确实需要对此进行更多研究。这是一个从 w3 学校提供的数据库中提取数据的 PHP 代码示例。见这里http://www.w3schools.com/php/php_mysql_select.asp

<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

 while($row = mysqli_fetch_array($result))
 {
 echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
 }
echo "</table>";

mysqli_close($con);
?> 

【讨论】:

  • 非常感谢!我会检查一下。部分问题是我什至不知道如何“问”我想要完成的问题。现在我知道你们都在做什么。我玩得很开心。我会接受这个答案并查看 w3schools。再次感谢@Edwinner
【解决方案2】:

您必须遍历数据库中的结果,否则您只能访问第一行(或数组指针所在的位置)。

您的问题是编程基础知识,网络上有数百万关于此主题的资源。我只是给你一个小示例代码,你可以从这里开始调试和修改你的口味。

//Connect to MySQL Database
$connection = mysql_connect('host', 'user', 'password') or die("Connection error. Details: ".mysql_error());
//Select the database
$db_select = mysql_select_db('dbname', $connection);

//Create the query
$query = mysql_query("SELECT * FROM `foods`") or die("Error in query. Details: ".mysql_error());

//Check if the query has results, and iterate over it
if (mysql_num_rows($query) > 0)
{
    //Creates the table
    echo '<table>';

    //Iterate over the MySQL results and print the data
    while ($result = mysql_fetch_assoc($query))
    {
        echo '
        <tr>
            <td>'.$result['item_name'].'</td>
        </tr>
        ';
    }
    echo '</table>';
}
else
{
    echo "Your query hasn't returned results";
}

试试这个,不要忘记更改mysql主机名、用户名、密码、数据库名和表名。

问候。

【讨论】:

    【解决方案3】:

    试试这个:

     $conn = mysql_connect('host', 'user', 'password') or die("Connection error. Details:".mysql_error()); 
    
    $db_select = mysql_select_db('<your_db_name', $conn);
    $sql = mysql_query("SELECT * FROM `foods`") 
    
    if($sql)
    {
    
    echo '<table>';
    
    
    while ($result = mysql_fetch_assoc($query))
    {
        echo '<tr><td>'.$result['<field_name1>'].'</td>';
        echo '<td>'.$result['<field_name2>'].'</td>';
          .....
          .....
        echo '</tr>'; 
     }
     echo '</table>';
     }
    else
    {
     echo "no results is there";
    }
    

    注意:您应该知道您的表列名称,在 field_names 中提供相同的名称 您可以打印表格中存在的任意数量的列。 谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-22
      • 1970-01-01
      • 2017-08-08
      • 2015-02-05
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多