-1

Im trying to make a custom function for my forum based on php. I have my index landing style page separate to my phpbb forums. I am trying to display the latest registered users username, as part of a statistics type widget for the home page. I have the following code so far:

//Function code

//Total Posts calculation
$result = $mysqli->query("SELECT DISTINCT post_id FROM phpBB_posts 
WHERE post_id <= '1'");

$total_posts = $result->num_rows;

//Total topics calculation
$result2 = $mysqli->query("SELECT DISTINCT topic_id FROM phpBB_topics WHERE topic_id <= '1'");

$total_topics = $result2->num_rows;

//Member count calculation
$result3 = $mysqli->query("SELECT DISTINCT user_id FROM phpBB_users WHERE user_id <= '1'");

$total_members = $result3->num_rows;

//Newest member
$result4 = $mysqli->query("SELECT * FROM phpBB_users WHERE group_id <> 6 ORDER BY user_regdate DESC LIMIT 1");

$newestMember = $result4->name;

//End of function

//function output
printf("Total Posts: ".$total_posts."<br/> Total Topics: ".$total_topics."<br/>Total Members: ".$total_members."<br/>Our newest member is: ".$newestMember);

The "Newest member" section is where i'm having trouble, the rest works as I want, if you have any pointers/criticism i'll gladly take it on board though.

I want to be able to return the value in the username column from the results set i get returned from that query, and print it as the variable $newestMember.

My logic as you can see was to get the list of users, so SELECT * then limit the list to actual users and exclude bots etc so WHERE group_id <> 6. Then sort the list by the registered date ORDER BY user_regdate and then only one row from the full list with DESC LIMIT 1. I've been checking mysql and php documentation and can't work out how to get the value from the returned row/username column (field) and store it as the newestMember variable and print to the page. Can anyone point me in the right direction please?

Cheers, Jamie

  • You need to _fetch_ the row out of the result set. – CBroe Jun 04 '17 at 15:52
  • @CBroe so using the documentation, if in place of `$newestMember = $result4->name;` I use the following `if ($result = $mysqli->query($result4)) { /* fetch object array */ while ($row = $result->fetch_row()) { printf ("%s (%s)\n", $row[0], $row[1]); } /* free result set */ $result->close(); }` I get the following error "Warning: mysqli::query() expects parameter 1 to be string" any ideas? – Jamie Bullock Jun 04 '17 at 15:58
  • The idea here would be to research the error message ... – CBroe Jun 04 '17 at 16:10

1 Answers1

0

Managed to get it working modifying that code a little, deleted the mysqli->query that prepended the SELECT query and that made it work. What finally worked is as per below if anyone else comes across the same issue. Thanks for the nudge in the right direction CBroe! Needed a fresh pair of eyes and to look at it from a different angle :)

//Newest member
$myresult = ("SELECT * FROM phpBB_users WHERE user_type <> 2 ORDER BY user_regdate DESC LIMIT 1");

if ($result = $mysqli->query($myresult)) {

/* fetch object array */
while ($row = $result->fetch_row()) {
        $newestMember = $row[7];
}

/* free result set */
$result->close();
}

//End of function



//function output
printf("Total Posts: ".$total_posts."<br/> Total Topics: ".$total_topics."<br/>Total Members: ".$total_members."<br/>Our newest member is: ".$newestMember);
  • This was the line that was wrong $result4 = **$mysqli->query** ("SELECT * FROM phpBB_users WHERE group_id <> 6 ORDER BY user_regdate DESC LIMIT 1"); – Jamie Bullock Jun 04 '17 at 16:48