-1

I’m trying to select all rows within my posts table based on 3 requirements.

  1. The userid from posts table = userid from users table
  2. The hostid from posts table = userid from users table
  3. The commentid in the posts table = zero

I want to select all of the rows that meet those requirements. This is the error...

Fatal error: Uncaught Error: Call to a member function fetch_assoc() on bool in C:\xampp\htdocs\loginsystem\includes\posts.inc.php:108 Stack trace: #0 C:\xampp\htdocs\loginsystem\home.php(38): getUploads(Object(mysqli)) #1 {main} thrown in C:\xampp\htdocs\loginsystem\includes\posts.inc.php on line 108

This is my code...

function getUploads($conn) {

        $userName = $_GET["user"];
        $sqluserid = "SELECT userid FROM users WHERE userName = $userName";
        $userid = mysqli_query($conn, $sqluserid);
        $sqlusercontent = "SELECT * FROM posts WHERE hostid = $userid AND userid = $userid AND commentid = 0";
        $usercontent = mysqli_query($conn, $sqlusercontent);
        $postid = 1;

    // This is the actual upload content
        while ($row = $usercontent->fetch_assoc()) {
            echo "<div class='postbox'><p>";
            echo $row['title']."<br>";
            echo $row['date']."<br>";
            echo "<div><img src='posts/".$userid."/".$postid.".*></div>";
            echo $row['description']."<br>";
            echo "</p>";
            echo "</div>";
            $postid++;
        }    
    }
Barmar
  • 741,623
  • 53
  • 500
  • 612
AJay
  • 68
  • 6
  • You need to call `$userid->fetch_assoc()`, just like you do to get the results of the second query. – Barmar May 03 '19 at 23:34

2 Answers2

0

You're not calling $userid->fetch_assoc() to fetch the results of the first query. You need to do this, and then use $row['userid'] to get the userid out of the row.

You also need quotes around the username in the first query. It would be best if you used a prepared statement and $stmt->bind_param(), to prevent SQL-injection.

But there's no need for two queries in the first place, you can join the two tables.

SELECT p.*
FROM posts AS p
JOIN users AS u ON p.userid = u.userid AND p.hostid = u.userid
WHERE u.username = '$userName' AND p.commentid = 0

You should check the results of your queries, then you would have been notified of the syntax error in the first query.

$result = mysqli_query($conn, $sql) or die($conn->error);
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

1) Try to add this

//This will prevent if you dont have any data
if ($usercontent = $conn->query($query)) {
    while ($row = $usercontent->fetch_assoc()) {
      //the rest of your code here
    }
/* free result set */
$result->free();
} else {
 echo "No Data";
}

2) You can pass the $_GET['user'] as parameter in this way you can separete how to pass the user name.

function getUploads($conn, $username) {

}

3) You can use INNER JOIN to create just one query

Your code should look like this at the end

function db () {
    static $conn;
    if ($conn===NULL){ 
        $conn = mysqli_connect ("localhost", "root", "", "database");
    }
    return $conn;
}

function getUploads($userName)
{
    $conn = db();
    $sqlusercontent = "SELECT p.*
    FROM posts AS p
    JOIN users AS u ON p.userid = u.userid AND p.hostid = u.userid
    WHERE u.username = '$userName' AND p.commentid = 0";

   // not suere why hostid is equal to userid ?
    $postid = 1; //$postid is not suppose to come from the db ?

    // This is the actual upload content
    if ($usercontent = mysqli_query($conn, $sqlusercontent)) {
        while ($row = $usercontent->fetch_assoc()) {
            echo "<div class='postbox'><p>";
            echo $row['title'] . "<br>";
            echo $row['date'] . "<br>";
            echo "<div><img src='posts/" . $row['userid'] . "/" . $row['postid']  . "'></div>";
            echo $row['description'] . "<br>";
            echo "</p>";
            echo "</div>";
            $postid++; //$postid is not suppose to come from the db ?

        }
        /* free result set */
        $usercontent->free();
    } else {
        echo "No Data";
    }
}
    $userName = $_GET["user"];
    getUploads($username);