Home » Php » It only write out the last “friend request” in the database i want it to write out all of the requests to a specfic user

It only write out the last “friend request” in the database i want it to write out all of the requests to a specfic user

Posted by: admin October 26, 2017 Leave a comment

Questions:

I need help with this code, it only writing out the last value (last friend request) i want it to write out all of the friend requests to the specific user.
I cant really solve the problem i dont understand what the problem is,

  public function showfriend_req($myid1){

        $DB = new DB();
      $DB->connect();

      $this->myid1 = $DB->secret($myid1);

      // checkar vänförfrågningar

      $stmt = $DB->prepare("SELECT * FROM frnd_req WHERE to1 = ? ");
      $stmt->bind_Param("s", $this->myid1);
      $stmt->execute();
      $stmt->store_result();

      if($stmt->num_rows == "0"){
        echo "Du har inga vänförfrågningar";
      }else {

        // script för visa förfrågningar

        $stmt = $DB->prepare("SELECT from1 FROM frnd_req WHERE to1 = ?");
        $stmt->bind_Param("s", $this->myid1);
         $stmt->execute();
         $stmt->store_result();

         $stmt->bind_Result($from1);
         while($stmt->fetch()){

            $from1;
         }

         // tar fram från users columnen

         $stmt = $DB->prepare("SELECT id,Username,firstname,lastname,picture FROM users WHERE id = ?");
         $stmt->bind_Param("s", $from1);
         $stmt->execute();
         $stmt->store_result();

         $stmt->bind_Result($id, $username, $firstname, $lastname, $picture);
         while($stmt->fetch()){

            echo "$username <br />";
         }

      }

    }   
Answers:

Try with the following code hoping this working with you. everything explained.

     if ($stmt = $mysqli->prepare("SELECT from1 FROM frnd_req WHERE to1 = ?")) {

                /* bind parameters for markers */
                $stmt->bind_Param("s", $this->myid1);

                /* execute query */
                $stmt->execute();

                /* bind result variables */
                  $stmt->bind_Result($from1);

                /* fetch value */
                while($stmt->fetch()){;

                printf("%s is in from %s\n", $this->myid1, $from1);

        }
}