PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
while ($userChecked = mysql_fetch_array($checkUser)){
extract($userChecked);
//Instead of extracting all the users details I would like to setup session data
// $_SESSION['userDetails'][COLUMNNAME_FROM_DB_OR_NUMBER] = value1stcolumn;
// $_SESSION['userDetails'][COLUMNNAME_FROM_DB_OR_NUMBER] = value2ndcolumn;
If anyone can help with this it would be great
My overall aim is the be able to access this data like this
$userid = $_SESSION['userDetails'][0];
$firstname = $_SESSION['userDetails'][1];
or like this
$userid = $_SESSION['userDetails'][id];
$firstname = $_SESSION['userDetails'][username];
you get the idea?
<?php
$checkUser = mysql_query("SELECT * FROM users WHERE username = '$username' and password = '$password'") or die(mysql_error());
while ($row = mysql_fetch_array($checkUser)
{
// Everything you want is in the $row array now
$_SESSION['username'] = $row['username']; // or whatever the username field is
}
?>
<?php
$checkUser = mysql_query("SELECT * FROM users WHERE username = '$username' and password = '$password'") or die(mysql_error());
while ($row = mysql_fetch_array($checkUser)
{
// Everything you want is in the $row array now
$_SESSION['username'] = $row['username']; // or whatever the username field is
}
?>
//I was thinking of maybe something like this
$_SESSION['userDetails'][] = $row['I DONT KNOW THIS NAME'];
//My idea is to make this so that if new columns are added to the database in the future my php file will hold the data in the session, I will not have to keep going back through the files.
you get what I mean?
Thanks
The example on this page is exactly what I want but I need it storing
in the session
<?php
$checkUser = mysql_query("SELECT * FROM users WHERE username = '$username' and password = '$password'") or die(mysql_error());
while ($row = mysql_fetch_array($checkUser))
{
// Everything you want is in the $row array now
$_SESSION['userData'][] = $row; // or whatever the username field is
}
?>
You are expecting one row returned from the query, correct? If so, then you don't need to keep looping like that. You can literally read the result into an array and foreach it...
<?php
// You may want to consider a limit clause on this...
$sql = "SELECT * FROM users WHERE username = '$username' and password = '$password' LIMIT 1";
if (!$result = mysql_query($sql))
{
die('Could not run the query: ' . mysql_error());
}
// Its a good idea to make sure you have a result count
if (mysql_num_rows($result))
{
$row = mysql_fetch_array($result);
foreach ($row as $k => $v)
{
$_SESSION['userData'][$k] = $v;
}
}
?>