PDA

View Full Version : MYSQL in PHP in BBCODE question


grey_goose
04-03-2016, 02:18 PM
I'm using Cel PHP in Custom BBCode (https://vborg.vbsupport.ru/showthread.php?t=264896).
- create a bbcode to list the past month's threads a given username has posted in.

<?php
$servername = "host";
$username = "username";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT thread.title, thread.threadid
FROM post
LEFT JOIN user ON (user.userid = post.userid)
LEFT JOIN thread ON (thread.threadid = post.threadid)
WHERE
post.username = '$value' AND
thread.`open` = 1 AND
thread.forumid NOT IN (2, 11, 15, 61, 191, 214, 289) AND
thread.lastpost >= UNIX_TIMESTAMP()-172800
GROUP BY
thread.title
ORDER BY
post.dateline DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$threads = '<a href="FQDN/forum/showthread.php?' . $row["threadid"]. '">' . $row["title"]. '</a><br>';
}
} else {
$threads = "0 results";
}
$conn->close();

return $threads;
?>

It works -- but only returns the first row. Help?

cellarius
04-03-2016, 02:48 PM
It probably returns only the last row. In your while-loop, your overwriting the $threads with every iteration. You need to create an array.

grey_goose
04-03-2016, 03:22 PM
*facepalm*

Got it. Thanks, and thanks for that mod.

$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_array()) {
$scenes[] = $row;
}
} else { $threads = "0 results";}

foreach ($scenes as $scene){
$listscenes .= '<a href="forum/showthread.php?' . $scene["threadid"]. '">' . $scene["title"]. '</a>'.'<br />';
}

$conn->close();

return $listscenes;