jardane Posted April 16, 2010 Share Posted April 16, 2010 i have a mySQL login script that pulls a single record from my database. $q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = @mysqli_query ($dbc, $q); // Run the query What i need is to pull the values from user_id and first_name or what ever information i want from that single record and store it in session variables like. $_SESSION['user_id'] = user_id It's easy when your working with a lot of records but i have no idea how to pull from just one. Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/ Share on other sites More sharing options...
Lamez Posted April 16, 2010 Share Posted April 16, 2010 <?php $f = mysqli_fetch_array($r);//corrected $_SESSION['user_id'] = $f['user_id']; ?> EDIT: OOPS $q should be $r Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043233 Share on other sites More sharing options...
jardane Posted April 16, 2010 Author Share Posted April 16, 2010 Thanks i am really starting to love this forum Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043236 Share on other sites More sharing options...
jardane Posted April 16, 2010 Author Share Posted April 16, 2010 ok now it's giving me an error Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in C:\xampp\htdocs\test_server\login.php on line 27 Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043240 Share on other sites More sharing options...
Lamez Posted April 16, 2010 Share Posted April 16, 2010 did you see my corrected post? I made a mistake. Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043241 Share on other sites More sharing options...
jardane Posted April 16, 2010 Author Share Posted April 16, 2010 yes i saw that and corrected it before you even fixed the mistake. here is the whole code: $q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = @mysqli_query ($dbc, $q); // Run the query if (!empty($r)) { $f = mysql_fetch_array($r); $_SESSION['user_id'] = $f['user_id']; $_SESSION['first_name'] = $f['first_name']; $_SESSION['email'] = $_POST['email']; $_SESSION['password'] = $_POST['password']; $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']); //require_once ('redirect_user_profile.html'); } And the error is: Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in C:\xampp\htdocs\test_server\login.php on line 28 and for reference line 28 is: $f = mysql_fetch_array($r); Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043245 Share on other sites More sharing options...
Lamez Posted April 16, 2010 Share Posted April 16, 2010 you are using mysqli_query, but when you call the array, you are using mysql_fetch_array(), try mysqli_fetch_array instead. Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043248 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 $q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = @mysqli_query ($dbc, $q); // Run the query if (!empty($r)) { $f = $r->fetch_assoc(); $_SESSION['user_id'] = $f['user_id']; $_SESSION['first_name'] = $f['first_name']; $_SESSION['email'] = $_POST['email']; $_SESSION['password'] = $_POST['password']; $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']); //require_once ('redirect_user_profile.html'); } Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043251 Share on other sites More sharing options...
jardane Posted April 16, 2010 Author Share Posted April 16, 2010 both $f = $r->fetch_assoc(); and mysqli_fetch_array don't work Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043259 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Oh I'm sorry. I used OO when it's procedural. $q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = @mysqli_query ($dbc, $q); // Run the query if (!empty($r)) { $f = mysqli_fetch_assoc($r); $_SESSION['user_id'] = $f['user_id']; $_SESSION['first_name'] = $f['first_name']; $_SESSION['email'] = $_POST['email']; $_SESSION['password'] = $_POST['password']; $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']); //require_once ('redirect_user_profile.html'); } That really doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043261 Share on other sites More sharing options...
Lamez Posted April 16, 2010 Share Posted April 16, 2010 I am a bit curious. What is the difference between using mysql and mysqli? The difference between fetch_array and fetch_assoc? Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043265 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 MySQLi is faster and better which is why it's called MySQL Improved. I think as of PHP 6, MySQL will be dropped in terms of development. mysql_fetch_array returns both an indexed and associative array (by default unless the optional parameter is passed). mysql_fetch_assoc only returns an associative array. The former one does more work. Rarely do you need both an indexed and associative array for the result. The former one can go up to 2x or even more slower than the latter because of putting together such a big array. Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043269 Share on other sites More sharing options...
Lamez Posted April 16, 2010 Share Posted April 16, 2010 I now have some code revision. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043276 Share on other sites More sharing options...
jardane Posted April 16, 2010 Author Share Posted April 16, 2010 I still can't get it to work here is the whole code: <?php include("head_start.php"); echo '<title>Densar Data Information - Login</title>'; include("head_end.php"); ?> <?php $errors = array(); require_once ('mysqli_connect.php'); // Connect to the db. if (isset($_POST['submitted'])) { //email if (empty($_POST['email'])) { $errors[] = 'You forgot to enter your email.'; }else { $e = mysqli_real_escape_string($dbc, trim($_POST['email'])); } //password if (empty($_POST['password'])) { $errors[] = 'You forgot to enter your password.'; }else { $p = mysqli_real_escape_string($dbc, trim($_POST['password'])); } if (empty($errors)) { // If everything's OK. $q = "SELECT user_id, first_name FROM users WHERE email='$e' AND pass=SHA1('$p')"; $r = @mysqli_query ($dbc, $q); // Run the query if (!empty($r)) { $row = mysqli_fetch_assoc($r); $_SESSION['user_id'] = $row['user_id']; $_SESSION['first_name'] = $row['first_name']; $_SESSION['email'] = $_POST['email']; $_SESSION['password'] = $_POST['password']; $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']); echo $row['user_id']; require_once ('redirect_user_profile.html'); } }else{ include("head_start.php"); include("head_end.php"); echo '<h1>Error!</h1> <p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br />\n"; } echo '</p><p>Please try again.</p><p><br /></p>'; // End of if (empty($errors)) IF. include("admin_form.html"); } mysqli_close($dbc); // Close the database connection. }else{ echo "<h1>Admin Login</h1>"; include("admin_form.html"); } ?> <?php include("footer.html");?> Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043315 Share on other sites More sharing options...
Lamez Posted April 16, 2010 Share Posted April 16, 2010 Are you getting the same error? Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043323 Share on other sites More sharing options...
jardane Posted April 16, 2010 Author Share Posted April 16, 2010 i feel so stupid i was using SHA1 for the password but the password was not saved to the database using SHA1 so it was not matching. It works now thanks everyone. Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043329 Share on other sites More sharing options...
jardane Posted April 16, 2010 Author Share Posted April 16, 2010 i feel so stupid i was saving the password to the database without using SHA1 but when i was doing my select statement i was using SHA1 it all works now. Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043331 Share on other sites More sharing options...
Lamez Posted April 16, 2010 Share Posted April 16, 2010 Ya, I was about to say, it has to be the query. Quote Link to comment https://forums.phpfreaks.com/topic/198777-pulling-a-information-from-a-single-mysql-record/#findComment-1043343 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.