mysql_conn vs mysqli
Posted: Fri Feb 29, 2008 1:36 am
Hello,
In order to use mysql_fetch_assoc, do I need to connect and retrieve data using mysql_conn and subsequent mysql_query, etc to retrieve data? I was getting errors when I tried to use mysqli and a $conn->query scenario to fetch data using mysql_fetch_assoc, saying that the array I was returning was invalid for the object, so I decided to switch to mysql_conn, mysql_query, etc but now I can't get a valid result from the query. Can anyone explain what I'm doing wrong here:
In order to use mysql_fetch_assoc, do I need to connect and retrieve data using mysql_conn and subsequent mysql_query, etc to retrieve data? I was getting errors when I tried to use mysqli and a $conn->query scenario to fetch data using mysql_fetch_assoc, saying that the array I was returning was invalid for the object, so I decided to switch to mysql_conn, mysql_query, etc but now I can't get a valid result from the query. Can anyone explain what I'm doing wrong here:
Code: Select all
function login($username, $password)
// check username and password with db
// if yes, return true
// else throw exception
{
// connect to db
$conn = db_connect();
$sel_db = mysql_select_db('listingsbox_db');
if (!sel_db)
die(mysql_error());
// check if username is unique
$result = mysql_query("select * from user
where username='$username'
and passwd = sha1('$password')", $conn);
if (!$result)
die(mysql_error());
throw new Exception('Could not log you in. 1'); // THIS IS WHERE IT TERMINATES AND RETURNS TO CALLING FUNCTION BELOW
if ($result->num_rows>0)
return true;
else
throw new Exception('Could not log you in. 2');
}
// CALLING FUNCTION IS BELOW
session_start();
//create short variable names
$username = $_POST['username'];
$passwd = $_POST['passwd'];
if ($username && $passwd)
// they have just tried logging in
{
try
{
login($username, $passwd);
// if they are in the database register the user id
$_SESSION['valid_user'] = $username;
}
catch(Exception $e)
{
echo $e->getMessage();
// unsuccessful login
do_html_header('Problem:'); // THIS IS WHAT IS OUTPUT
echo 'You could not be logged in.
You must be logged in to view this page.';
do_html_url('login.php', 'Login');
do_html_footer();
exit;
}
}