Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/117875-solved-error-handling/
Share on other sites

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");
}
?>

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

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");
}
}
?>

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.