I have been following a tutorial to better understand php’s oop, but have come across a stumbling block, im not sure if it’s an oop thing or something that i just haven’t come across in the procedural way of doing things, i’m pretty new and trying to learn a lot quickly. I like to know what’s happening before moving on.
Below is the code, which I understand apart from why under users_username i need ‘, and another ‘, before selecting users_email? At first I thought it was a way of separating them so as a test I removed them and was returned the error: Trying to get property of non-object. So I am guessing by removing them i am somehow stopping the name being created as an object? or am I way off the mark.
"SELECT CONCAT(users_username,
',
',
users_email)
AS name,
DATE_FORMAT(users_joined, '%M %d, %Y') AS dr
FROM users
ORDER BY users_joined
ASC";
$r = $mySqli->query($q);//run the query
$num = $r->num_rows;//assign the number of rows
if($num > 0){//if there are members in the database
echo '<p>There are currently ' . $num . ' members registered.</p>' . "\n";
while($row = $r->fetch_object()){
echo $row->name . ' ' . $row->dr . "<br/>";
}
}
Apologies if this has been asked many times before but I didn’t know how to search this question.
What CONCAT(users_username, ‘,’,users_email) is doing is taking the users_username appending a comma and then appending the users_email. So if users_username is bob and users_email is bob@example.com that will return “bob,bob@example.com”.
If you remove the single quotes then it is just CONCAT(users_username,,,users_email) and PHP might not like that there is nothing between the commas. In this case the query may be failing and returning NULL for $row. So when you do $row->name it tries to get the name property of $row but since $row is NULL it doesn’t have any properties.