Jump to content


MySql and Php Help


8 replies to this topic

#1 pizzaboy9010

    Young Padawan

  • Members
  • Pip
  • 9 posts

Posted 06 March 2006 - 01:38 AM

Here's the code:

	$check = mysql_query("SELECT username FROM users WHERE username = '$user'") or die(mysql_error());
		$true = mysql_num_rows($check) or die(mysql_error());
		if($true == 0) {
			echo "<center><font color=red>Not a valid user.</font></center>";
		} else {
			echo "good.";
		}

$user is gotten from the $_GET variable.

When the query is ran, but there is no username in the database equal to $user, it makes the page blank where mysql_num_rows is ran. I've had this problem a few times, and I've worked around it, but I can't seem to here. I'm just wondering what I can do to make it work.

#2 rus321

    Young Padawan

  • Members
  • Pip
  • 49 posts

Posted 06 March 2006 - 02:30 AM

just wonderin

why are you using the {or die(mysql_error()} twice?

#3 pizzaboy9010

    Young Padawan

  • Members
  • Pip
  • 9 posts

Posted 06 March 2006 - 02:39 AM

View Postrus321, on Mar 6 2006, 07:30 AM, said:

just wonderin

why are you using the {or die(mysql_error()} twice?

I've always done that in all my coding. I use it where ever it can go... is that bad? It's always seemed fine to me.

Edited by pizzaboy9010, 06 March 2006 - 02:39 AM.


#4 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 06 March 2006 - 03:46 AM

the mysql_num_rows error means that nothing has been found from the database, try this:

$check = mysql_query("SELECT `username` FROM `users` WHERE `username` = '$user'") or die(mysql_error());
		$true = mysql_num_rows($check) or die(mysql_error());
		if(!$true) {
			echo "<center><font color=\"FF0000\">Not a valid user.</font></center>";
		} else {
			echo "good.";
		}
i changed the if ($true == 0) to if (!$true) which is a better way of doing it, and changed the font color=red to the actual colour as that could be doing it.

Also check that:
-you are selecting the right rows
-$user is defined somewhere
-you have a connection file / text to connect you that works (to the db)

#5 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 06 March 2006 - 04:08 PM

The newest version of PHP has left some people struck deaf and dumb. One of the problems i'm having is that every variable parsed through the URL has to be defined. So put this at the top of your code:

if (isset($_GET['user'])){
$user = $_GET['user'];
}else{
echo 'No user specified!';
}


#6 pizzaboy9010

    Young Padawan

  • Members
  • Pip
  • 9 posts

Posted 06 March 2006 - 10:19 PM

View Postdeadly, on Mar 6 2006, 08:46 AM, said:

the mysql_num_rows error means that nothing has been found from the database, try this:

$check = mysql_query("SELECT `username` FROM `users` WHERE `username` = '$user'") or die(mysql_error());
		$true = mysql_num_rows($check) or die(mysql_error());
		if(!$true) {
			echo "<center><font color=\"FF0000\">Not a valid user.</font></center>";
		} else {
			echo "good.";
		}
i changed the if ($true == 0) to if (!$true) which is a better way of doing it, and changed the font color=red to the actual colour as that could be doing it.

Also check that:
-you are selecting the right rows
-$user is defined somewhere
-you have a connection file / text to connect you that works (to the db)

Didn't work. And $user is defined, as when $user ='s a username in the database, it loads correctly.

View PostLang, on Mar 6 2006, 09:08 PM, said:

The newest version of PHP has left some people struck deaf and dumb. One of the problems i'm having is that every variable parsed through the URL has to be defined. So put this at the top of your code:

if (isset($_GET['user'])){
$user = $_GET['user'];
}else{
echo 'No user specified!';
}

$user is defined on the page above it.

#7 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 07 March 2006 - 03:50 PM

Is $user defined on a page thats being included? That might have something to do with it.

Here's what i'd do to your code:

	 $getUser = "SELECT username FROM users WHERE username = '$user'";
	 $getUser = mysql_query($getUser);
	 if (mysql_num_rows($getUser) < 1){
		 echo 'no valid user';
	 }else{
		 echo 'good';
	 }


#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 07 March 2006 - 09:15 PM

If php stops working at mysql_num_rows, it has nothing to do with the definition of $user, and who said he was running php 5?

Try removing the $true variable and using this instead:
if(mysql_num_rows($check) == 0) {
There is no point in having the or die(mysql_error()) part with that, as the mysql_query() is the only function that affects mysql_error, and simply having it there can potentially slow your script down. Also, the if-statement would do the error checking.

Edited by rc69, 07 March 2006 - 09:16 PM.


#9 pizzaboy9010

    Young Padawan

  • Members
  • Pip
  • 9 posts

Posted 07 March 2006 - 09:44 PM

View Postrc69, on Mar 8 2006, 02:15 AM, said:

If php stops working at mysql_num_rows, it has nothing to do with the definition of $user, and who said he was running php 5?

Try removing the $true variable and using this instead:
if(mysql_num_rows($check) == 0) {
There is no point in having the or die(mysql_error()) part with that, as the mysql_query() is the only function that affects mysql_error, and simply having it there can potentially slow your script down. Also, the if-statement would do the error checking.

Thank you, this works.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users