Page 1 of 1

Php MySQL Error

Posted: Mon Jan 02, 2006 3:35 am
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>

Posted: Mon Jan 02, 2006 3:38 am
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']."'");

Posted: Mon Jan 02, 2006 3:58 am
by Clive
Still Get The Same The Error

Posted: Mon Jan 02, 2006 4:01 am
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']}'");

Posted: Mon Jan 02, 2006 4:05 am
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.

Posted: Mon Jan 02, 2006 4:07 am
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 :)

Posted: Mon Jan 02, 2006 4:08 am
by mickd
yep lol i actually checked that list to make sure logged wasnt in there.

Posted: Mon Jan 02, 2006 4:10 am
by IAD
Man you got an issue :P, just do CTRL + F to search for it in that page :P

Posted: Mon Jan 02, 2006 4:11 am
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 :?

Posted: Mon Jan 02, 2006 5:42 am
by Clive
Thanks for your help guys,

Turns out i was trying to select from the wrong table

Sorry, my bad