neridaj Posted February 29, 2008 Share Posted February 29, 2008 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 https://forums.phpfreaks.com/topic/93642-mysqli-vs-mysql_conn/ Share on other sites More sharing options...
trq Posted February 29, 2008 Share Posted February 29, 2008 mysqli* and mysql* are two different interfaces, you cannot mix and match them. mysql_query() for instance returns a result resource, here..... $result->num_rows you try to use it as though it was an object. Link to comment https://forums.phpfreaks.com/topic/93642-mysqli-vs-mysql_conn/#findComment-479817 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.