ngreenwood6 Posted August 2, 2008 Share Posted August 2, 2008 I have a script where users can login to view certain pages of my site which works perfectly. I have also create a registration form for them to register with. If they leave the fields blank i have created the errors that it will give them. However, if they enter in an already used username it will add another user. I would like to know how to check the database to see if the user is there and if it is give an error. any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/117875-solved-error-handling/ Share on other sites More sharing options...
MadTechie Posted August 2, 2008 Share Posted August 2, 2008 Heres a very basic way. $username will be the input <?php $username = "what ever"; $result = mysql_query("SELECT * from table WHERE username = '$username' "); $num_rows = mysql_num_rows($result); if($num_rows > 0) { die("User already exists"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/117875-solved-error-handling/#findComment-606310 Share on other sites More sharing options...
ngreenwood6 Posted August 2, 2008 Author Share Posted August 2, 2008 I am getting these errors when I try to do that: Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\wamp\www\login\register.php on line 21 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\login\register.php on line 22 Any help Quote Link to comment https://forums.phpfreaks.com/topic/117875-solved-error-handling/#findComment-606319 Share on other sites More sharing options...
Adam Posted August 2, 2008 Share Posted August 2, 2008 Whats on lines 21 and 22? the code above? Quote Link to comment https://forums.phpfreaks.com/topic/117875-solved-error-handling/#findComment-606320 Share on other sites More sharing options...
Adam Posted August 2, 2008 Share Posted August 2, 2008 Also if I were you i'd change the field in the database to unique.. that way mysql would give off an error if you try to enter duplicate usernames... Adam Quote Link to comment https://forums.phpfreaks.com/topic/117875-solved-error-handling/#findComment-606322 Share on other sites More sharing options...
MadTechie Posted August 2, 2008 Share Posted August 2, 2008 For MySQLi try <?php $link = mysqli_connect("localhost", "user", "password", "database"); if ($result = mysqli_query($link, "SELECT * from table WHERE username = '$username' ")) { $num_rows = mysqli_num_rows($result); mysqli_free_result($result); if($num_rows > 0) { die("User already exists"); } } ?> OR <?php $mysqli = new mysqli("localhost", "user", "password", "database"); if ($result = $mysqli->query("SELECT * from table WHERE username = '$username' ")) { /* determine number of rows result set */ $num_rows = $result->num_rows; $result->close(); if($num_rows > 0) { die("User already exists"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/117875-solved-error-handling/#findComment-606324 Share on other sites More sharing options...
ngreenwood6 Posted August 2, 2008 Author Share Posted August 2, 2008 Thanks you madtechie your suggestion worked like a charm. Quote Link to comment https://forums.phpfreaks.com/topic/117875-solved-error-handling/#findComment-606329 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.