I am getting very close, I have been able to confirm I connect to the database, been able to confirm that my user name and password are in the "users" table. I come back from that login function returning true
Now when I try to query the other table for data it bombs. Can someone please help me sort where I am screwed up here? I am pretty new to php but am starting to get it. A little help would be deeply appreciated. 😕
The error handler has given me the following errors:
Encountered error: 8 in home/omgma/public_html/Fresh_start/member.php5, line 63: Undefined variable: conn
Encountered error: 2 in /home/omgma/public_html/Fresh_start/member.php5, line 63: mysqli_query() expects parameter 1 to be mysqli, null given
Encountered error: 2 in /home/omgma/public_html/Fresh_start/member.php5, line 64: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given
Line 63 and 64 are from this part:
59 // if logged in run query
60 $query = "select user_id, last_name, first_name from directory ";
61 echo "<p>Starting query: $query </p>";
62
63 $result = mysqli_query($conn, $query);
64 $num_results = mysqli_num_rows($result);
My connection functions are as follows:
if ($username && $passwd)
{
// they have just tried logging in
login($username, $passwd);
// if they are in the database register the user id
$_SESSION['valid_user'] = $username;
}
else
exit ;
echo "<h2>Logged in as ".$_SESSION['valid_user']." at line: ".__LINE__ ."</h2>";
That executes fine when I use a valid user and password and echos out the intended message:
Logged in as Conjurer at line: 56
But then when I try to use mysqli to query $conn it fails. Isn't $conn what is returned from my login function? Here is the login function - the first part uses a db_connect function to connect and returns the $conn. Then I am trying to make sure they are logged in as a valid user. If they are then I want to go on to query out some info for them.
function login($username, $passwd)
// check username and password with db
// if yes, return true
// else throw exception
{
// connect to db
$conn = db_connect();
if (!$conn) echo "<h2>Connection Error for user $username using $passwd at line: ".__LINE__ ."</h2>";
// check if username is unique
$userquery ="select * from users where username='".$username ."'
and password ='".$passwd ."'";
echo "<p>User query: ".$userquery ."</p>";
$result = $conn->query($userquery);
if (!$result){
$errmsg = "<h2>Login query failed at line: " .__Line__."</h2>
mysqli_connect_error: ".mysqli_connect_error()."<br />";
throw new Exception($errmsg);
}
echo "<p> Login query ran: checking number of row results...<br />";
if ($result->num_rows>0)
{echo "Number of rows greater than 0 - returning true<br />";
return true;
}
else
{$errmsg = "Number of rows NOT greater than 0 - function failed at line: " .__Line__."<br />";
throw new Exception($errmsg);
}
}
Maybe I am just screwed up here, do I return a database "handle" or should I just use my connect function again because I have passed the validation part and set them as a valid user?
This part is truly confusing to me!