显示数据库数据

时间:2015-09-21 18:46:51

标签: php

我输入了此代码的variaties并且没有显示。 我设法让另一个页面显示,但这个页面根本不会显示。 我正在尝试创建一个页面,一个php文件,友好地查看数据库内容,更新,删除和添加内容,如果需要。

但首先我需要能够在获得其余代码之前显示内容。

我确实尝试添加代码来更新=,从类似的显示中删除和添加数据,但没有成功。 请帮助你? 没有显示错误,它只是在'th'内容标记后面的空白。

我已经测试过它直到哪一行编译,不确定是否有帮助。 只是fyi。

有时会提到它找不到数据库,有时却很少。不知道为什么,但它导致了正确的来源。

 <?php
            require 'config.php';
            
            $con = mysql_connect( $db_host, $db_username, $db_password, $db_name);
            
            if ($con->connect_error) {
                die("Connection failed: " . $con->connect_error);
            }//else { echo 'it works'; }

            echo '<table>';
            echo '  <tr>';
            echo '      <th>';
            echo '          Comment';
            echo '      </th>';
            echo '  </tr>';
            
            $myData = "SELECT * FROM demo ";
            $res = mysql_query($myData,$con);
// especially not working after this line.
                    while($row = mysqli_fetch_assoc($res)){
        echo '  <tr>';
        echo '      <td>';
        echo '          <input type=text name=ID value="';
        echo                $row["ID"];
        echo '          "/>';
        echo '      </td>';
        echo '      <td>';
        echo '          <input type=text name=ID value="';
        echo                $row["Name"];
        echo '          "/>';
        echo '      </td>';
        echo '      <td>';
        echo '          <input type=text name=ID value="';
        echo                $row["Org"];
        echo '          "/>';
        echo '      </td>';
        echo '      <td>';
        echo '          <textarea name=Comment value="'.$row["Comment"].'"></textarea>';                        
        echo '      </td>';
        echo '      <td>';
        echo '           <input type=submit value=Update name=UpBut id=UpBut/>';
        echo '           <input type=submit value=Delete name=DelBut id=DelBut/>';
        echo '      </td>';
        echo '  </tr>';
    }
    echo '</table>';

            $conn->close();  

1 个答案:

答案 0 :(得分:0)

避免使用已弃用的mysql函数。 而不是:

$res = mysql_query($myData,$con);

之前传递$con

$res = mysqli_query($con, $myData);

这绝对是错误的:

while($row = $res->fetch_assoc()){

正确的方式:

while($row = mysqli_fetch_assoc($res)){

试试这个:

<?php

 require 'config.php';
 /*
 $db_host = "localhost";
 $db_username = "yourusernamehere";
 $db_password = "yourpasswordhere";
 $db_name = "yourdbnamehere";           
 */
    $con = new mysqli($db_host, $db_username, $db_password, $db_name);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }


            echo '<table>';
            echo '  <tr>';
            echo '      <th>';
            echo '          Comment';
            echo '      </th>';
            echo '  </tr>';

            $myData = "SELECT * FROM demo";
            $res = mysqli_query($con, $myData);
            while($row = mysqli_fetch_assoc($res)) {
                    echo '<div id="main">' . '<p>';
                    echo $row["Comm"]; . '<br>';
                    echo $row["Name"]; . '<br>';
                    echo $row["Company"]; . '</p>';
                    echo "</div>";
            }

         ?>
  

注意:研究mysqli或PDO。正确关闭</table>标记是   因为我不知道你之后可能会有其他事情。

相关问题