Jump to content

pulling a information from a single mySQL record


jardane

Recommended Posts

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

$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');

}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");?>

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.