Jump to content

mysqli vs mysql_conn


neridaj

Recommended Posts

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:

 

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;
  }      
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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