Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
Thread: Store MySQL row in array
05-26-2013, 03:27 PM #1
- Join Date
- Jan 2012
- Thanked 0 Times in 0 Posts
Store MySQL row in array
Hi, i'm working on a login script. My problem is that I want to grab all the data that the sql table is storing for that user. Example my table is holding the values "id, username, passsword".
What i've done so far is setting a session $_SESSION['username'] = $username;
and what i want to do now is also set a session for the id so I can use that later. $_SESSION['id']. Something like this.
so is there anyway to store the whole table row in an array and easily grab the value I want when needed? like result['id'] for the id and result['username'] for the username and so on...
$mysqli = new mysqli("localhost", "root", "password", "ls");
printf("Connect failed: %s\n", mysqli_connect_error());
if($stmt = $mysqli -> prepare("SELECT * FROM tAccounts WHERE username = ? AND password = ?"))
$stmt -> bind_param('ss', $username, $password);
$username = $_POST['username'];
$password = $_POST['password'];
$stmt -> execute();
$stmt -> bind_result($username, $password);
if($stmt -> fetch())
$_SESSION['username'] = $username;
$_SESSION['username'] = result['username'];
$_SESSION['id'] = result['id'];
Last edited by USB; 05-26-2013 at 03:30 PM.
05-26-2013, 05:31 PM #2
- Join Date
- Feb 2011
- Your Monitor
- Thanked 506 Times in 493 Posts
Jpb donePHP Code:
foreach($Row as $Key => $Value)
$_SESSION[$Key] = $Value;
My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!
05-27-2013, 07:41 PM #3
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,659 Times in 2,628 Posts
To do an actual row into a session with statements isn't quite that easy though.
You could fetch the result, and then fetch a row:
Or if you don't actually need the row and prefer the offsets, you can do what you are already doing:PHP Code:
$result = $stmt->get_result();
$_SESSION['row'] = $result->fetch_assoc(); // this is multidimensional. Can be merged as well
You could also reference a variable that's represented within the bind_result to that of a SESSION offset.PHP Code:
$stmt->bind_result($field1, $field2, $field3);
$_SESSION['field1'] = $field1;
$_SESSION['field2'] = $field2;
$_SESSION['field3'] = $field3;
Keep in mind that the bind_result order must match that of your query. Since you've use * as your query fields, I haven't a clue what fields are in there. All I know of for sure is username and password since it would have tossed an exception on prepare if they didn't exist.
header('HTTP/1.1 420 Enhance Your Calm');