微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从db检索数据并在php中的表中显示它查看此代码有什么问题吗?

如何解决从db检索数据并在php中的表中显示它查看此代码有什么问题吗?

尝试这个:

<?PHP

 # Init the MysqL Connection
  if( !( $db = MysqL_connect( 'localhost' , 'root' , '' ) ) )
    die( 'Failed to connect to MysqL Database Server - #'.MysqL_errno().': '.MysqL_error();
  if( !MysqL_select_db( 'ram' ) )
    die( 'Connected to Server, but Failed to Connect to Database - #'.MysqL_errno().': '.MysqL_error();

 # Prepare the INSERT Query
  $insertTPL = 'INSERT INTO `name` VALUES( "%s" , "%s" , "%s" , "%s" )';
  $insertsql = sprintf( $insertTPL ,
                 MysqL_real_escape_string( $name ) ,
                 MysqL_real_escape_string( $add1 ) ,
                 MysqL_real_escape_string( $add2 ) ,
                 MysqL_real_escape_string( $mail ) );
 # Execute the INSERT Query
  if( !( $insertRes = MysqL_query( $insertsql ) ) ){
    echo '<p>Insert of Row into Database Failed - #'.MysqL_errno().': '.MysqL_error().'</p>';
  }else{
    echo '<p>Person\'s @R_331_4045@ion Inserted</p>'
  }

 # Prepare the SELECT Query
  $selectsql = 'SELECT * FROM `names`';
 # Execute the SELECT Query
  if( !( $selectRes = MysqL_query( $selectsql ) ) ){
    echo 'Retrieval of data from Database Failed - #'.MysqL_errno().': '.MysqL_error();
  }else{
    ?>
<table border="2">
  <thead>
    <tr>
      <th>Name</th>
      <th>Address Line 1</th>
      <th>Address Line 2</th>
      <th>Email Id</th>
    </tr>
  </thead>
  <tbody>
    <?PHP
      if( MysqL_num_rows( $selectRes )==0 ){
        echo '<tr><td colspan="4">No Rows Returned</td></tr>';
      }else{
        while( $row = MysqL_fetch_assoc( $selectRes ) ){
          echo "<tr><td>{$row['name']}</td><td>{$row['addr1']}</td><td>{$row['addr2']}</td><td>{$row['mail']}</td></tr>\n";
        }
      }
    ?>
  </tbody>
</table>
    <?PHP
  }

?>

注意事项,注意事​​项和警告

在将值传递到数据库之前,您的初始解决方案没有显示任何明显的价值。这就是sql注入攻击(甚至是通过sql传递的意外错误)发生的方式。不要做

您的数据库似乎没有主键。尽管从技术上讲,在所有使用中这些都不是必需的,但它们是一种好习惯,并且为引用表中的特定行提供了一种更加可靠的方式,无论是添加相关表还是在该表中进行更改。

您需要在每个阶段检查每个操作是否有错误。大多数PHP函数都足够好以产生响应,它们将在错误条件下返回。随时检查这些条件是您的工作- 永远不要假设PHP会按照您的期望,期望的方式和期望的顺序进行操作。这就是事故的发生…

我上面提供的代码包含许多要点,如果发生错误,将返回一条消息。尝试一下,查看是否报告了任何错误消息,查看“错误消息”,如果适用,还返回错误代码并进行一些研究。

祝好运。

解决方法

  $db = mysql_connect("localhost","root","");
  $er = mysql_select_db("ram");
  $query = "insert into names values('$name','$add1','$add2','$mail')";
  $result = mysql_query($query);
  print "<p> Person's Information Inserted </p>";
  $result = mysql_query("SELECT * FROM names");
?>

<table border="2">
   <tr>
      <th>Name</th>
      <th>Address Line 1</th>
      <th>Address Line 2 </th>
      <th>E-mail Id </th>
    </tr>
    <? 
    while ($array = mysql_fetch_row($result));
    {
        print "<tr> <td>";
        echo $array[0]; 
        print "</td> <td>";
        echo $array[1]; 
        print "</td> <td>";
        echo $array[2]; 
        print "</td> <td>";
        echo $array[3]; 
        print "</td> </tr>";
    }
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。