使用mysql将数据库信息拉入PHP脚本内的html表

使用mysql将数据库信息拉入PHP脚本内的html表,php,html,mysql,database,html-table,Php,Html,Mysql,Database,Html Table,首先,我真的希望我不会因为问这个问题而被打倒,但我愿意冒这个险,这样我就能学会正确的方向 我有一个.php脚本,在php脚本中我编写了连接MySQL数据库的代码 问题是: 我有一张桌子,用来简单地展示食物。目前我有: <table width="1024" border="0" cellpadding="10" cellspacing="5" align="center"> <tr> <td colspan="2"><font>It

首先,我真的希望我不会因为问这个问题而被打倒,但我愿意冒这个险,这样我就能学会正确的方向

我有一个.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”表中列出不同的项目


任何帮助都将不胜感激

你真的需要对此做更多的研究。下面是一个Php代码示例,它从w3学校提供的数据库中提取数据。看这里


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

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

//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数据库
$connection=mysql_connect('host'、'user'、'password')或die(“连接错误。详细信息:“.mysql_error());
//选择数据库
$db\u select=mysql\u select\u db('dbname',$connection);
//创建查询
$query=mysql\u query(“从'foods`中选择*”)或die(“query.Details中的错误:.mysql\u Error());
//检查查询是否有结果,并对其进行迭代
if(mysql_num_rows($query)>0)
{
//创建表
回声';
//迭代MySQL结果并打印数据
while($result=mysql\u fetch\u assoc($query))
{
回声'
“.$result['item_name']”
';
}
回声';
}
其他的
{
echo“您的查询尚未返回结果”;
}
试试这个,别忘了更改mysql主机名、用户名、密码、数据库名和表名

问候。

试试这个:

 $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";
}
$conn=mysql\u connect('host','user','password')或die('Connection error.Details:“.mysql\u error());

$db\u select=mysql\u select\u db(‘来吧,在寻求帮助之前试着从一些东西开始…!!相信我。我已经尝试了很多次,但都一无所获。谢谢你的评论。这是非常基本的,不适合堆栈溢出。你想看看系统地教授这些步骤的教程。如果这是一个问题,可能会重复,当然!知道有什么好的教程吗保守党人?非常感谢!我会查出来的。问题的一部分是我甚至不知道如何“问”这个问题来回答我试图实现的目标。现在我明白你们的想法了。我过得很开心。我会接受这个答案并检查一下。再次感谢@Edwinner
 $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";
}