使用准备好的语句获取用户数据 [英] fetching user data with prepared statement

查看:63
本文介绍了使用准备好的语句获取用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,所以我正试图通过显示用户数据来创建配置文件表.使用了一些代码示例,您将在下面的代码中看到这些代码示例,并对其进行了注释,因此请当心.

hey guy so am trying to create profile table by displaying user data. have used some code samples that you'll see in the code below, have commented some out so watch out.

这是我的代码

    // $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包装的.

As you can see there are 1 open bracket } in the code above, that because it was wrap with the 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

The above code work, but no user data displayed. here is screenshot: http://prntscr.com/cmfa0n

推荐答案

在此处查看此声明

$row = $stmt->fetch();

来自文档

->fetch()方法返回,

成功.数据已提取
发生FALSE错误
NULL不再存在行/数据或发生数据截断

TRUE Success. Data has been fetched
FALSE Error occurred
NULL No more rows/data exists or data truncation occurred

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

It doesn't return any row from the result set.

使用->bind_result()方法,您已经绑定了用于存储结果的变量,而->fetch()所做的是,它将准备好的语句中的结果提取到绑定的变量中,因此您可以直接使用$firstname,<表格中的c5>,$user_type等.

With the ->bind_result() method, you've already binded variables for result storage, and what ->fetch() does is, it fetches results from a prepared statement into the bound variables, so you can directly use $firstname, $lastname, $user_type etc. in your table.

注意:您尚未在SELECT语句中选择avatar列,也未在$stmt->bind_result($firstname, $lastname,...)语句中添加$avatar.

Note: You haven't selected avatar column in the SELECT statement, and add $avatar in the $stmt->bind_result($firstname, $lastname,...) statement also.

这篇关于使用准备好的语句获取用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆