Bryan
Bryan

Reputation: 1

PHP getting the number of results returned from DB with stored procedure

I am trying to get the number of results from a database using mysqli_num_rows but i don't know what parameter to pass in as i'm using a stored procedure. I will post what i have done. In my current code the Else is executed. I an new to PHP.

<?php

session_start();

include_once  'constants.php';
include_once 'connection.php';

$connection = new database();

$connection->connect();

$email = $_POST['email'];
$password = $_POST['password'];

// Call the login stored procedure
$conn = $connection->query("CALL LoginUser('$email','$password')");

//Check if any users match
$rows = mysqli_num_rows($conn);
if($rows > 0){

// If result was found put information into array
$user = mysqli_fetch_assoc($conn);

//Store all the users info in session variables
$_SESSION['user_id'] = $user['account_id'];
$_SESSION['user_firstName'] = $user['fname'];
$_SESSION['user_lastName'] = $user['lname'];
$_SESSION['user_password'] = $user['password'];
$_SESSION['user_dob'] = $user['dob'];
$_SESSION['user_joinDate'] = $user['joinDate'];
$_SESSION['user_picture'] = $user['picture'];

//Redirect user to logged in home page
header('Location: home.php');

// If no results are found redirect user to noAccount page
} else {
header('Location: noAccount.php');
}


?>

Upvotes: 0

Views: 29

Answers (1)

pr1nc3
pr1nc3

Reputation: 8348

$user = mysqli_fetch_assoc($conn);
    if(!empty($user) > 0){

//Store all the users info in session variables
$_SESSION['user_id'] = $user['account_id'];
$_SESSION['user_firstName'] = $user['fname'];
$_SESSION['user_lastName'] = $user['lname'];
$_SESSION['user_password'] = $user['password'];
$_SESSION['user_dob'] = $user['dob'];
$_SESSION['user_joinDate'] = $user['joinDate'];
$_SESSION['user_picture'] = $user['picture'];

//Redirect user to logged in home page
header('Location: home.php');

// If no results are found redirect user to noAccount page
} else {
header('Location: noAccount.php');
}

mysqli_num_rows is expecting the output result.

Edit: num_rows is expecting the query result not the fetched one. In this case i suggest you use something like the above code i modified.

Upvotes: 1

Related Questions