View Full Version : login with sessions

11-30-2010, 10:37 PM
I keep getting this error and I don't know why?

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\kickado\sign_in.php on line 25

//start session

//clear error message
$error_msg = "";

//if user is not logged in attempt to log in
if (!isset($_SESSION['user_id'])){
if (isset($_POST['submit'])){

//connect to database
$dbc = mysqli_connect('localhost', 'root', '', 'kickado');

//grab login from form input
$user_username = mysqli_real_escape_string($dbc, trim($_POST['username']));
$user_password = mysqli_real_escape_string($dbc, trim($_POST['password']));

if (!empty($user_username) && !empty($user_password)){
//compare the input with whats stored in database
$query = "SELECT user_id, user_name FROM kickado_user WHERE user_name = '$user_username' AND" .
"password = SHA('$user_password')";
$data = mysqli_query($dbc, $query);

if (mysqli_num_rows($data) == 1){
//Log in is confirmed set session and redirect to home page
$row = mysqli_fetch_array($data);
$_SESSION('user_id', $row('user_id'));
$_SESSION('username', $row('user_name'));
$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php';
header('Location: ' . $home_url);
}else {
//username and password are incorrect display an error message
$error_msg = 'Sorry, you must enter a valid username and password to log in';
}else {
//username and password are blank display an error message
$error_msg = 'User name and Password are empty!';
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>KickADo | Your 30 seconds to fame!</title>
<link type = "text/css"
href = "sign_in.css"
rel = "stylesheet" />

<div id = "all">
<?php include("header.php"); ?>
<div id = "content">
//if the session is empty display error message and the form
if (empty($_SESSION['user_id'])){
echo '<p>' . $error_msg . '</p>';
<form method = "post" action = "<?php echo $_SERVER['PHP_SELF']; ?>" >
<label>User Name</label>
<input type = "text"
name ="username"
value = "<?php if (!empty($user_username)) echo $user_username; ?>" /><br />
<input type = "password"
name ="password" /><br />
<input type = "submit"
value = "login"
name = "submit" />
<?php include("footer.php"); ?>
//confirm login success
echo ('<p class="login">You are logged in as' . $_SESSION['username'] . '.</p>');

12-01-2010, 12:45 AM
is this backwards

$data = mysqli_query($dbc, $query);

should it not be your query name then your db connection

like this

$data = mysqli_query($query, $dbc);

is that it, im not sure but i think its backwards...

12-01-2010, 01:31 PM
Nope that didnt help

12-01-2010, 01:35 PM
I assume there was a connection error.

As the PHP manual itself suggests, put this after your connect line and see what it says:

if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());

12-03-2010, 03:49 PM
I tryed that and my db connection is good I did some reading on xampp and found that many people have problems with using sessions on xampp. I will try to upload to my normal server and see if that works