Jump to content

[SOLVED] If no result with that id from mysql


XeroXer

Recommended Posts

Hi again all

I have been having some php problems with my site.
Yesterday I created a simple forum and user profiles.
The thing is that if someone goes into a userid that doesn't exists I want to show an error.
Now it only shows a blank page.

My code is like this:
[code]<?php
echo "<table border='1' cellspacing='2' cellpadding='2'><tr><td><strong>Topic</strong></td><td><strong>Date</strong></td><td><strong>User</strong></td></tr>";
$threads = $_GET['t'];
include("config/database.php");
$con = mysql_connect("$mysqlhost","$mysqlusr","$mysqlpass") or die('Could not connect: ' . mysql_error());
mysql_select_db($db_name, $con) or die(mysql_error());
$results = mysql_query("SELECT * FROM v2_forum_threads WHERE catid = '$threads' ORDER BY id") or die(mysql_error());
while($row = mysql_fetch_array($results))
{
echo "<tr><td><a href='forum_thread.php?t=";
echo $row['id'];
echo "'>";
echo $row['topic'];
echo "</a></td><td>";
echo $row['date'];
echo "</td><td>";
echo "<a href='profile.php?u=";
echo $row['userid'];
echo "'>";
$userid = $row['userid'];
$results = mysql_query("SELECT * FROM v2_users WHERE id = '$userid'") or die(mysql_error());
while($rows = mysql_fetch_array($results))
{
echo $rows['name'];
}
echo "</a></td></tr>";
}
mysql_close($con);
echo "</table>";
echo "<br><p align='right'><a href='forum_post.php?t=";
echo $threads;
echo "'>Post thread</a></p>";
?>[/code]

I want that if no catid that is the same as $threads it should show a simple message to the user.
Just check the number of rows returned.

[code]<?php
$results = mysql_query("SELECT * FROM v2_forum_threads WHERE catid = '$threads' ORDER BY id") or die(mysql_error());
$num_rows = mysql_num_rows($results);
if($num_rows > 0){
while($row = mysql_fetch_array($results))
// blah blah blah


} else {
echo "No threads found";
}
?>[/code]

Ray

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.