0

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.

here is my code

    // $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();

As you can see there are 1 open bracket } in the code above, that because it was wrap with the HTML.

below is the html code

     <!-- 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(); 
  ?>

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

1 Answers1

0

Look at this statement here,

$row = $stmt->fetch();

From the documentation,

->fetch() method returns,

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.

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.

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

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • OK have had a look at the doc... is what you are trying to say is I don't need the ->fetch (); – Olamilekan Oshodi Sep 26 '16 at 07:47
  • sorry for my ignorance – Olamilekan Oshodi Sep 26 '16 at 07:47
  • @OlamilekanOshodi Yes, you do need `->fetch()`. What I'm saying is, after this statement `$stmt->fetch();`, you can directly use those bounded variables `$firstname`, `$lastname` in your table to display all data. – Rajdeep Paul Sep 26 '16 at 07:49
  • oh I get it. OK I will give it ago and I will get back to you. thanks buddy. – Olamilekan Oshodi Sep 26 '16 at 07:56
  • @RajdeeopPaul... so am still not getting any result. here is my final code. – Olamilekan Oshodi Sep 26 '16 at 08:14
  • `$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 { $row = $stmt->fetch(); $stmt->bind_result($firstname, $lastname, $user_type, $profession, $email, $dob, $gender, $country, $phone, $bio, $address, $created_at);` – Olamilekan Oshodi Sep 26 '16 at 08:14
  • @OlamilekanOshodi This line `if(!$stmt->num_rows > 0) { ...` looks suspicious, change this line to `if($stmt->num_rows == 0) { ...`. Also, do `echo $stmt->num_rows;` to see how many results you're getting. – Rajdeep Paul Sep 26 '16 at 08:39
  • @OlamilekanOshodi And oh, first do `$stmt->bind_result($firstname, $lastname, ...);` **and then** do `$stmt->fetch();`, otherwise it won't work. – Rajdeep Paul Sep 26 '16 at 08:45
  • well here is what have tried... http://prntscr.com/cmg8rm and am receiving a blank result – Olamilekan Oshodi Sep 26 '16 at 08:48
  • maybe this will help... here is my none prepare statement. `// $getUserQuery = "select * from user where id = $userId"; // $resultData = mysqli_query($con, $getUserQuery); // if(!mysqli_num_rows($resultData)){ // echo 'No Data Found for this user'; // }else{ // $row = mysqli_fetch_array($resultData);` that have commented out, it works, what the easier way to turn this to statement. other than what have presently – Olamilekan Oshodi Sep 26 '16 at 08:50
  • @OlamilekanOshodi You haven't read [this comment](http://stackoverflow.com/questions/39696679/fetching-user-data-with-prepared-statement/39697226?noredirect=1#comment66695034_39697226), have you? Your code should be like this, [http://pastebin.com/UWJ8J6Xw](http://pastebin.com/UWJ8J6Xw). Also, I told you to do `echo $stmt->num_rows;`, what does it show? – Rajdeep Paul Sep 26 '16 at 08:53
  • Hi Ok... sorry about that, i was trying to fix on my side as well, so have tried the pastebin code, and have also echo it as well. here the screenshot http://prntscr.com/cmgf1q – Olamilekan Oshodi Sep 26 '16 at 09:04
  • am thinking is it because i have my html like this? ` ` – Olamilekan Oshodi Sep 26 '16 at 09:05
  • @OlamilekanOshodi As I told you, you can directly use those bound variables in your table, like this: ` ` etc. – Rajdeep Paul Sep 26 '16 at 09:26
  • That works beautifully, i think since i wasn't using the variable `$row = $stmt->fetch()` anymore i should amend the html ` ` to `` as you;ve suggested. Now it works. – Olamilekan Oshodi Sep 26 '16 at 09:38
  • @OlamilekanOshodi *Glad I could help you. :-)* – Rajdeep Paul Sep 26 '16 at 11:04
  • hey mate... how are you. Please was hoping you could help with code here. `http://stackoverflow.com/questions/39827931/mysqli-to-prepared-statement?noredirect=1#comment66946049_39827931` if you are not too busy. – Olamilekan Oshodi Oct 03 '16 at 09:30
  • @OlamilekanOshodi I've given an answer there. Hopefully that will resolve your issue. – Rajdeep Paul Oct 03 '16 at 20:07