Php MySQL Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Clive
Forum Newbie
Posts: 3
Joined: Mon Jan 02, 2006 3:30 am

Php MySQL Error

Post by Clive »

This is code i have made for a messenger part of my site but every time i try to load the page i get this error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/clive91/public_html/messenger/index.php on line 44

Code: Select all

<?php
	$first = $_GET['first'];

	if($first == true)
	{
		$msg = "<font face=\"Arial\"><form method=\"post\" action=\"index.php\">";
		$msg.= "<center>";
		$msg.= "<table>";
		$msg.="<tr>";
		$msg.="<td colspan=\"2\"><b>Please Sign In With Your www.ClivesDen.com Username & Password</b></td>";
		$msg.="</tr>";
		$msg.= "<tr>";
		$msg.= "<td align=\"right\">Username: </td>";
		$msg.= "<td><input type=\"text\" name=\"username\"></td>";
		$msg.= "</tr>";
		$msg.= "<tr>";
		$msg.= "<td align=\"right\">Password: </td>";
		$msg.= "<td><input type=\"password\" name=\"password\"></td>";
		$msg.= "</tr>";
		$msg.= "<tr>";
		$msg.= "<td></td>";
		$msg.= "<td><input type=\"submit\" value=\"Log-In!\"></td>";
		$msg.= "</tr>";
		$msg.= "</table>";
		$msg.= "</center></font>";
	}
	else
	{
		if(isset($_COOKIE["loggedin"]))
		{
			$username = $_COOKIE['username'];

			mysql_connect ("localhost", "clive91_admin", "jvcfjmr") or die ('I cannot connect to the database because: ' . mysql_error());
			mysql_select_db ("clive91_messenger");

			mysql_query("INSERT INTO logged (Username) VALUES (\"$username\")");
			
			$friends = mysql_query("SELECT * FROM friends WHERE Username=\"$username\"");
			$friend = "<table width=\"100%\">";

			while($row = mysql_fetch_array($friends))
			{
				$online = mysql_query("SELECT * FROM logged WHERE Username=\"$username\" AND Friend=\"".$row['friend']."\"");
				$checking = mysql_num_rows($online);
				
				if($checking < 1)
				{
					$friend.= "<tr><td align=\"center\">".$row["Friend"]."</td></tr>";
				}
				else
				{
					$friend.= "<tr><td align=\"center\">Online".$row['Friend']."</td></tr>";
				}
			}

			$friend.= "<tr><td align=\"center\"><a href=\"newfriend.php\">Add New Friend</a></td></tr></table>";

			mysql_select_db("clive91_Users");
			$emaila = mysql_query("SELECT * FROM users WHERE Username=\"$username\"");

			$info = "<table width=\"100%\" valign=\"top\">";
			$info.= "<tr valign=\"top\"><td align=\"left\">Username: $username</td>";

			while($rows = mysql_fetch_array($emaila))
			{
				$info.= "<td align=\"left\">E-Mail Address: ".$rows['Email']."</td></tr>";
			}

			$msg = "<font face=\"Arial\"><table width=\"100%\">";
			$msg.= "<tr>";
			$msg.= "<td colspan=\"2\"><h3><center>$username</center></h3></td>";
			$msg.= "</tr>";
			$msg.= "<tr>";
			$msg.= "<td width=\"30%\"><center><h4>Contacts</h4></center></td><td><center><h4>User Info</h4></center></td>";
			$msg.= "</tr>";
			$msg.= "<tr>";
			$msg.= "<td>$friend</td><td>$info</td>";
			$msg.="</tr>";
			$msg.= "</table></font>";
		}
		else
		{
			$username = $_POST['username'];
			$password = $_POST['password'];

			if(!$username || !$password)
			{
				$msg = "<font face=\"Arial\"><form method=\"post\" action=\"index.php\">";
				$msg.= "<center>";
				$msg.= "<table>";
				$msg.="<tr>";
				$msg.="<td colspan=\"2\"><b>Please Sign In With Your www.ClivesDen.com Username & Password</b></td>";
				$msg.="</tr>";
				$msg.= "<tr>";
				$msg.= "<td align=\"right\">Username: </td>";
				$msg.= "<td><input type=\"text\" name=\"username\"></td>";
				$msg.= "</tr>";
				$msg.= "<tr>";
				$msg.= "<td align=\"right\">Password: </td>";
				$msg.= "<td><input type=\"password\" name=\"password\"></td>";
				$msg.= "</tr>";
				$msg.= "<tr>";
				$msg.= "<td></td>";
				$msg.= "<td><input type=\"submit\" value=\"Log-In!\"></td>";
				$msg.= "</tr>";
				$msg.= "</table>";
				$msg.= "</center></font>";
			}
			else
			{
				mysql_connect("localhost","clive91_admin","jvcfjmr") or die("Database Error Please Contact The Administrator");
				mysql_select_db("clive91_Users");

				$result = mysql_query("SELECT * FROM users WHERE Username = \"$username\" and Password = \"$password\"");
				$num_rows = mysql_num_rows($result);

				if($num_rows < 1)
				{
					$msg = "<font face=\"Arial\"><form method=\"post\" action=\"index.php\">";
					$msg.= "<center>";
					$msg.= "<table>";
					$msg.="<tr>";
					$msg.="<td colspan=\"2\"><b>Please Sign In With Your www.ClivesDen.com Username & Password</b></td>";
					$msg.="</tr>";
					$msg.= "<tr>";
					$msg.= "<td align=\"right\">Username: </td>";
					$msg.= "<td><input type=\"text\" name=\"username\"></td>";
					$msg.= "</tr>";
					$msg.= "<tr>";
					$msg.= "<td align=\"right\">Password: </td>";
					$msg.= "<td><input type=\"password\" name=\"password\"></td>";
					$msg.= "</tr>";
					$msg.= "<tr>";
					$msg.= "<td></td>";
					$msg.= "<td><input type=\"submit\" value=\"Log-In!\"></td>";
					$msg.= "</tr>";
					$msg.= "</table>";
					$msg.= "</center></font>";
				}		
				else
				{
					$msg = "Log-In Sucssesful";
					setcookie("loggedin","true", time()+600000);
					setcookie("username",$username, time()+600000);
					header("Location:index.php");
					exit();
				}
			}
		}
	}
?>
<html>
	<body background="bg.gif">
		<?php echo($msg); ?>
	</body>
</html>
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

try changing line 43

Code: Select all

$online = mysql_query("SELECT * FROM logged WHERE Username=\"$username\" AND Friend=\"".$row['friend']."\"");
to

Code: Select all

$online = mysql_query("SELECT * FROM logged WHERE Username='$username' AND Friend='".$row['friend']."'");
Clive
Forum Newbie
Posts: 3
Joined: Mon Jan 02, 2006 3:30 am

Post by Clive »

Still Get The Same The Error
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

Change:

$online = mysql_query("SELECT * FROM logged WHERE Username=\"$username\" AND Friend=\"".$row['friend']."\"");

to:

Code: Select all

$online = mysql_query("SELECT * FROM `logged` WHERE Username = '$username' AND Friend = '{$row['friend']}'");
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

make sure everything in that query is spelt right and the case is all the same, Friend might not work on linux if in the database its set as friend i believe.
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

You're right, case-sensetive, and another problem that could happend is saved characters like "group" and more.

You can check for it here:

http://dev.mysql.com/doc/refman/5.0/en/ ... words.html


Good luck :)
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

yep lol i actually checked that list to make sure logged wasnt in there.
IAD
Forum Commoner
Posts: 42
Joined: Wed Dec 28, 2005 12:36 pm
Location: Israel, Soon Canada :)

Post by IAD »

Man you got an issue :P, just do CTRL + F to search for it in that page :P
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

yep i did but i dont trust IE, ive had issues with them with searching but thats a different story ;)

no idea how i opened IE, all my other pages are FF :?
Clive
Forum Newbie
Posts: 3
Joined: Mon Jan 02, 2006 3:30 am

Post by Clive »

Thanks for your help guys,

Turns out i was trying to select from the wrong table

Sorry, my bad
Post Reply