0

I have two tables: userfollow

`id` int(11) NOT NULL AUTO_INCREMENT,
`theuserid` int(11) NOT NULL,
`follower` int(11) NOT NULL,
`followstatus` int(11) NOT NULL,

users

`users_id` int(11) NOT NULL AUTO_INCREMENT,
`users_username` int(11) NOT NULL,
etc...

When I make this SQL query in phpmyadmin it works as intended:

SELECT * 
FROM `userfollow` 
    LEFT JOIN users on userfollow.theuserid = users.users_id 
WHERE users_username = 'hello' 

However, in php this does not seem to work:

$stmt = $conn->prepare('SELECT * 
                        FROM userfollow     
                            LEFT JOIN users on userfollow.theuserid = users.users_id 
                        WHERE users_username = ?');

I am able to get the results from the users table in php but not from userfollow table. For example:

$userurl = $_GET['user']; 
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare('SELECT * FROM userfollow LEFT JOIN users on userfollow.theuserid = users.users_id WHERE users_username = ?');
$stmt->bind_param('s', $userurl);
$stmt->execute();

$result = $stmt->get_result();
while($row = $result->fetch_array()){
    $profilepageusername = $row['users_username'];
    $thestatus = $row['followstatus'];

I am able to print out the username of the profile page but followstatus is null. var_export($thestatus); returns a null.

It's worth pointing out that if I did the other way around the results would be the same, meaning that the query would return null results from the userfollow table:

SELECT * 
FROM `users` 
    LEFT JOIN userfollow on userfollow.theuserid = users.users_id 
WHERE users_username = 'hello'; 

This does not seem to work either. I've never had to use aliases before in php and to my knowledge the columns arent overwriting eachother or am I wrong? Any tips would be greatful thanks!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Votum
  • 77
  • 6
  • 1
    You are sure that ` $userurl` is the expected value? – Jens Dec 15 '22 at 15:36
  • remove the `where` condition on the php part and see if you get anything at all. – Bagus Tesa Dec 15 '22 at 15:37
  • @BagusTesa Yes it returned a result if i remove the prepared statement and just join the tables. I suppose it has something to do with $userurl = $_GET['user']; but I use it to display the profile page of a user so for example /userstats.php?user=hello – Votum Dec 15 '22 at 15:40
  • @Votum make sure to at least trim the input – Bagus Tesa Dec 15 '22 at 15:41

0 Answers0