使用预准备语句获取用户数据

时间:2016-09-26 07:08:37

标签: php mysqli prepared-statement

嘿家伙所以我试图通过显示用户数据来创建个人资料表。已经使用了一些代码示例,您将在下面的代码中看到,已经注释了一些,所以请注意。

这是我的代码

    // $stmt = $con->prepare("SELECT * FROM user WHERE id = ? ");
    // $stmt->bind_param('s', $id);
    // if($stmt->execute()){
    // //$result = $stmt->get_result();
    // if($result->num_rows > 0){
    // //     username exists
    // //$stmt->bind_result($firstname, $lastname, $user_type, $email, $profession);
    //   echo 'No Data Found for this use';
    // }else{
    //    $row = $stmt->fetch_array();
    // $stmt = "select firstname, lastname, user_type, profession, email, dob, gender, country, phone, bio, address, created_at from user where id = $userId";
    // if ($stmt = $mysqli->prepare($stmt)) {

    //     /* execute statement */
    //      $stmt->execute();
    // // $stmt->store_result();
    // // if($stmt->num_rows){
    //     /* bind result variables */
    //     $stmt->bind_result($firstname, $lastname, $user_type, $profession, $email, $dob, $gender, $country, $phone, $bio, $address, $created_at);

    //     /* fetch values */
    //     while ($stmt->fetch()) {
    // //    }

    $stmt = $con->prepare("SELECT firstname, lastname, user_type, profession, email, dob, gender, country, phone, bio, address, created_at FROM user WHERE id = ?");
    $stmt->bind_param('s', $userId);
    $stmt->execute();
    $stmt->store_result();   
    if(!$stmt->num_rows > 0) {  
      echo 'No Data Found for this user';
      }
    else {
    $stmt->bind_result($firstname, $lastname, $user_type, $profession, $email, $dob, $gender, $country, $phone, $bio, $address, $created_at);  // <- Add; #args = #cols in SELECT

       $row = $stmt->fetch();

正如您所看到的,上面的代码中有1个开放式括号},因为它是用HTML包装的。

下面是html代码

     <!-- self post back url  -->
  <?php
      $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
  ?>

  <table>

        <tr>
          <td>
                <center>
                      <img src="userfiles/avatars/<?php echo $row['avatar'];?>" width="150" height="150">
                  </center>

          </td>
        </tr>
         <tr>
           <td><label><strong>First Name</strong></label></td>
           <td></td>
            <td><label><?php echo $row['firstname'];?> </td>
        </tr>
          <tr>
           <td><label><strong>Last Name</strong></label></td>
           <td></td>
            <td><label><?php echo $row['lastname'];?> </td>
        </tr>
   <tr>
           <td><label><strong>User Type</strong></label></td>
           <td></td>
            <td><label><?php echo $row['user_type'];?> </td>
        </tr>
       <tr>
           <td><label><strong>Profession</strong></label></td>
           <td></td>
            <td><label><?php echo $row['profession'];?> </td>
        </tr>
     <tr>
         <tr>
           <td><label><strong>Phone</strong></label></td>
           <td></td>
            <td><label><?php echo $row['phone'];?> </td>
        </tr>
     <tr>
           <td><label><strong>Gender</strong></label></td>
           <td></td>
            <td><label><?php echo $row['gender'];?> </td>
        </tr>
        <tr>
           <td><label><strong>Date Of Birth</strong></label></td>
           <td></td>
            <td><label><?php echo $row['dob'];?> </td>
        </tr>
        <tr>
           <td><label><strong>Email</strong></label></td>
           <td></td>
            <td><label><?php echo $row['email'];?> </td>
        </tr>
        <tr>
           <td><label><strong>Country</strong></label></td>
           <td></td>
            <td><label><?php echo $row['country'];?> </td>
        </tr>
      <tr>
           <td><label><strong>Address</strong></label></td>
           <td></td>
            <td><label><?php echo $row['address'];?> </td>
        </tr>
        <tr>
           <td><label><strong>Biography</strong></label></td>
           <td></td>
            <td><label><?php echo $row['bio'];?> </td>
        </tr>
        <tr>
           <td><label><strong>Join Date</strong></label></td>
           <td></td>
            <td><label><?php echo $row['created_at'];?> </td>
        </tr>

  </table>

  <?php 

     }  

  $mysqli->close(); 
  ?>

上面的代码工作,但没有显示用户数据。这是截图:http://prntscr.com/cmfa0n

1 个答案:

答案 0 :(得分:0)

请看这里的陈述,

$row = $stmt->fetch();

来自the documentation

->fetch()方法返回,

  

TRUE成功。已提取数据   FALSE发生错误
  NULL不存在更多行/数据或发生数据截断

它不会返回结果集中的任何行。

使用->bind_result()方法,您已经为结果存储绑定了变量,而->fetch()的作用是,它将预准备语句的结果提取到绑定变量中,因此您可以直接使用{在您的表格中{1}},$firstname$lastname等。

注意:您尚未在$user_type语句中选择avatar列,并在SELECT语句中添加$avatar。< / p>