pedromsouza Posted January 27, 2011 Share Posted January 27, 2011 hi! I need a query that checks if typed login is new or already in use... when I run it... [27-Jan-2011 20:13:59] PHP Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /Applications/MAMP/htdocs/recebe_dados_cadastro_usuario.php on line 30 Please help me (newbie)! <?php include "include_conecta_bd.inc"; $nome = $_POST["nome"]; $cpf = $_POST["cpf"]; $cnpj = $_POST["cnpj"]; $login = $_POST["login"]; $email = $_POST["email"]; $ddd = $_POST["ddd"]; $tel = $_POST["tel"]; $sexo = $_POST["sexo"]; $senha1 = $_POST["senha1"]; $senha2 = $_POST["senha2"]; $erro=0; if(empty($nome)) {echo "Favor digitar um nome.<br>"; $erro=1;} elseif(strlen($nome)<15) {echo "Favor digitar nome completo.<br>"; $erro=1;} elseif(empty($login) or strlen($login)<5) {echo "O login precisa ter 5 caracteres no mínimo.<br>"; $erro=1;} elseif((($senha1)!==($senha2)) or (strlen($senha2)<2)) {echo "As senhas não são idênticas.<br>"; $erro=1;} else {echo "Dados digitados corretamente!<br>";} {echo "<p>NOME:$nome<br>CPF:$cpf<br>LOGIN:$login<br>EMAIL:$email<br>TEL:$ddd.$tel<br>SEXO:$sexo<br></p>";} $verif = mysql_query ("select * from usr where login = $login"); $chk = mysql_num_rows ($verif); if($chk != 0) {break; echo "Este LOGIN já está em uso!<br>"; } elseif($verif == 0) {$resultado = mysql_query ("insert into usr (nome,cpf,login,sexo,email,ddd,tel,senha) values ('$nome','$cpf','$login','$sexo','$email','$ddd','$tel','$senha1')") or die(mysql_error()); echo mysql_error(); $linhas = mysql_affected_rows (); mysql_close ($conexao); echo "$linhas linha(s) afetada(s)!";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/225901-mysql_num_rows-error/ Share on other sites More sharing options...
l4nc3r Posted January 27, 2011 Share Posted January 27, 2011 As the error implies, $verif is not a MySQL resource as expected, but is instead null. Try printing mysql_error() after defining $verif and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/225901-mysql_num_rows-error/#findComment-1166284 Share on other sites More sharing options...
jcbones Posted January 27, 2011 Share Posted January 27, 2011 Your query is failing. You need to find out why. Try echo'ing the query, or just echo mysql_error on failure. $sql = "select * from usr where login = $login"; $verif = mysql_query ($sql) or die(mysql_error() . ' <br /> in ' . $sql); $chk = mysql_num_rows ($verif); Quote Link to comment https://forums.phpfreaks.com/topic/225901-mysql_num_rows-error/#findComment-1166285 Share on other sites More sharing options...
litebearer Posted January 27, 2011 Share Posted January 27, 2011 I believe it is due to the variable $login NOT being encased by single quotes Quote Link to comment https://forums.phpfreaks.com/topic/225901-mysql_num_rows-error/#findComment-1166287 Share on other sites More sharing options...
jcbones Posted January 27, 2011 Share Posted January 27, 2011 I thought so to, litebearer. But we shall see. Quote Link to comment https://forums.phpfreaks.com/topic/225901-mysql_num_rows-error/#findComment-1166289 Share on other sites More sharing options...
blew Posted January 27, 2011 Share Posted January 27, 2011 I believe it is due to the variable $login NOT being encased by single quotes yep $verif = mysql_query ("select * from usr where login = $login") shall be $verif = mysql_query ("select * from usr where login = '$login'") Quote Link to comment https://forums.phpfreaks.com/topic/225901-mysql_num_rows-error/#findComment-1166292 Share on other sites More sharing options...
pedromsouza Posted January 27, 2011 Author Share Posted January 27, 2011 That's it! I put quotes and mysql_error()... now it (query) seems to be working but a new error shows up... [27-Jan-2011 20:44:38] PHP Fatal error: Cannot break/continue 1 level in /Applications/MAMP/htdocs/recebe_dados_cadastro_usuario.php on line 33 I wanted it to do the following: 1. if $login is already in use, BREAK to avoid duplicate line... 2. if $login is new, insert into.... Thanks a lot for replying! Quote Link to comment https://forums.phpfreaks.com/topic/225901-mysql_num_rows-error/#findComment-1166298 Share on other sites More sharing options...
sasa Posted January 27, 2011 Share Posted January 27, 2011 just remove break Quote Link to comment https://forums.phpfreaks.com/topic/225901-mysql_num_rows-error/#findComment-1166303 Share on other sites More sharing options...
pedromsouza Posted January 28, 2011 Author Share Posted January 28, 2011 It works! "break" is unnecessary because of the "if" clause... If it was a loop, then I should use break, correct? Thanks! PS: how do I set this topic "solved"? Quote Link to comment https://forums.phpfreaks.com/topic/225901-mysql_num_rows-error/#findComment-1166458 Share on other sites More sharing options...
sasa Posted January 28, 2011 Share Posted January 28, 2011 If it was a loop, then I should use break, correct? yes Quote Link to comment https://forums.phpfreaks.com/topic/225901-mysql_num_rows-error/#findComment-1166504 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.