简体   繁体   English

使用PHP显示从MySQL数据库检索到的数据时出现问题

[英]Having issues with displaying data retrieved from a MySQL database with PHP

I'm very new to PHP; 我是PHP的新手。 and am doing a few sample projects to help me learn, this project was done for fun. 并且正在做一些示例项目来帮助我学习,这个项目的完成很有趣。 There's probably some idiotic and highly unintelligibly created code in here. 这里可能有一些愚蠢且难以理解的代码。 Considering I'm a newbie, there's probably a syntax error but I've been searching for hours without any luck. 考虑到我是新手,可能存在语法错误,但是我一直在寻找没有运气的小时。

Basically I'm making a extremely simple comment/shoutbox system in which the user who posts the data is kept anonymous. 基本上,我正在制作一个非常简单的注释/聊天框系统,在其中发布数据的用户保持匿名。 I'm doing things separate at the moment; 我现在正在分开做事; then I plan on combining them into one. 然后我计划将它们合并为一个。 I know that the submitting part is working correctly as I've checked the MySQL database via PHPMyAdmin, however I can't seem to get the data to display right. 我知道提交部分可以正常工作,因为我已经通过PHPMyAdmin检查了MySQL数据库,但是我似乎无法正确显示数据。 I've searched for over 2 hours for my simple syntax mistake, and have given up deciding to bring it to a reliable and friendly community. 我已经搜索了两个多小时的简单语法错误,并放弃了将其引入可靠和友好的社区的决定。

I've removed all of the connection information to my database, as I know all of this is correct and I'd rather not expose it on a public 'forum'. 我已将所有连接信息删除到数据库中,因为我知道所有这些信息都是正确的,因此我不希望将其公开显示在公共“论坛”上。

<html>
<body>
<?php
$username="";
$password="";
$database="";

mysql_connect("localhost",$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM data";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="2" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Name(No specific order)</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$    name=mysql_result($result,$i,"");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $name; ?></font></td>
</tr>

<?php
$i++;
}
?>
</body>
</html>

I have two tables, id and name. 我有两个表,ID和名称。 ID is a automatic increment INT, and name is set up a text. ID是一个自动递增的INT,而名称则是一个文本设置。 ID is also the primary of the database. ID也是数据库的主要对象。 The submitting system is working perfectly fine, however the displaying isn't. 提交系统运行正常,但显示效果不佳。

When I view the page, where there should be text($name), instead I get a blank area, and no text. 当我查看页面时,应该有text($ name),而我得到的是空白区域,没有文本。 This is what I received when I visited the page, with only one database entry: 这是我访问该页面时收到的,只有一个数据库条目:

<html>

<body>

<table border="2" cellspacing="2" cellpadding="2">

<tr>

<th><font face="Arial, Helvetica, sans-serif">Name(No specific order)</font></th>

</tr>





<tr>

<td><font face="Arial, Helvetica, sans-serif"></font></td>

</tr>



</body>

</html>

Any help would be appreciated, thanks. 任何帮助,将不胜感激,谢谢。

I would recommend not using the older mysql_*() functions. 我建议不要使用较早的mysql _ *()函数。 You could do something like this instead (via PDO_mysql extension): 您可以改为执行以下操作(通过PDO_mysql扩展):

<?php
    $pdo = new PDO('mysql:host=localhost;dbname=YOURDBNAME', $username, $password);
    $query = "SELECT * FROM data";
    $stmt = $pdo->prepare($query);
    $stmt->execute(array());
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

Then later, you would loop through with something like: 然后,您将使用以下内容循环遍历:

    foreach ($results as $res) {
        echo $res['column_name'];
    }

Something is wrong in while loop where you trying to display names. while loop中尝试显示名称的地方出了点问题。

Replace with below : 替换为以下内容:

 <?php
    if($num >0) {
       while ($row=mysql_fetch_assoc($result)) {
         ?>
        <tr>
           <td><font face="Arial, Helvetica, sans-serif"><?php echo $row['name']; ?></font></td>
        </tr>
        <?php

        }
      }
  ?>

change : 变化:

$num=mysql_numrows($result);

to

$num=mysql_num_rows($result);

You have the query result from MySQL but you're not looping through it properly. 您有来自MySQL的查询结果,但没有正确遍历它。 Try: 尝试:

$result=mysql_query($query);
if(mysql_num_rows($result) > 0)
{
    while ($row = mysql_fetch_assoc($result))
    {
        // print data
    }
}

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

相关问题 使用PHP从数据库检索到的数据未以HTML格式显示 - Data not displaying in HTML format retrieved from database using PHP PHP MYSQL如果没有检索到数据,则从数据库中获取数据并回显消息 - PHP MYSQL Get data from database and echo message if no data retrieved 显示使用PHP从数据库检索的图像 - Displaying images retrieved from database using PHP 如何使用php从mysql数据库中分页检索到的数据 - how to paginate the retrieved data from mysql database by using php 如何使用php从mysql数据库中分页检索到的数据? - how to paginate the retrieved data from mysql database by using php? 显示从数据库检索的数据作为表头 - Displaying data retrieved from database as table headers PHP 显示来自 MySQL 数据库的数据 - PHP Displaying data from MySQL database PHP和jQuery-使用自动完成功能创建两个不同的文本字段,这些字段具有从数据库中检索到的不同数据列表 - PHP & jQuery - Create two different textfields with autocomplete having different lists of data retrieved from the database PHP修改从数据库检索的值(编码和特殊字符问题) - Php modifies values retrieved from database (codification and special characters issues) PHP - 显示从 MySQL DB 检索的乌尔都语文本 - PHP - Displaying Urdu text retrieved from MySQL DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM