Jump to content

[SOLVED] error handling


ngreenwood6

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

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.