Reputation: 9855
I'm trying to return all records from my database where the userID
is equal to the logged in user.
I have the following only for some reason its not returning anything, can anybody see any obvious errors?
<?php
$interestsquery = "SELECT *
FROM user_interests
WHERE user_id = $usersClass->userID()";
$result = mysql_query($interestsquery);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "{$row['interest']}";
}
?>
Upvotes: 3
Views: 199
Reputation: 562691
The method call is a complex enough expression that you should probably enclose it in expression interpolation delimiters:
$interestsquery = "SELECT * FROM user_interests WHERE user_id = {$usersClass->userID()}"
But I also recommend abandoning the primitive PHP/mysql extension and moving to PDO, so you can use query parameters. Then you don't have to hassle with string interpolation at all, and you gain better habits for writing code that resists SQL injection vulnerabilities.
$interestsquery = "SELECT * FROM user_interests WHERE user_id = ?"
$stmt = $pdo->prepare($interestsquery);
$result = $stmt->execute(array( $usersClass->userID() ));
Upvotes: 4
Reputation: 10174
Try
$interestsquery = "SELECT * FROM user_interests
WHERE user_id = ".$usersClass->userID();
And make sure $usersClass->userID()
returns a valid integer user id.
Upvotes: 2
Reputation: 82058
Unfortunately, you can't call functions and have them parsed that way. You'll either need to concatenate manually, or set a variable and parse that.
Try this:
"SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
Or this:
$uid = $usersClass->userID();
"SELECT * FROM user_interests WHERE user_id = $uid";
Upvotes: 5
Reputation: 3211
try this:
$interestsquery = "SELECT * FROM user_interests WHERE user_id =".$usersClass->userID();
Upvotes: 2