I have a PHP login sytem in which I use the following query to compare the username and password:
$mQuery = $mysqli->query("SELECT * FROM users WHERE user = '" .
$mysqli->real_escape_string(md5($_POST['user'])) ."' AND pass= '" .
$mysqli->real_escape_string(md5($_POST['pass'])) . "'");
There are other fields in the user row besides 'user' and 'pass', like 'name' and 'email'. How can I get all these fields into an array?
In PHP 5.3 and later, fetch_all
will return all rows as an array. If you specify MYSQLI_ASSOC as the result type, each element in the array will be an associative array of fields, indexed by name.
// having done the query above
$rows = $mQuery->fetch_all(MYSQLI_ASSOC);
foreach ($rows as $row)
{
print "email: " . $row['email'] . "<br />";
// etc
}
for earlier versions, you'd do it manually:
$rows = array();
while ($row = $mQuery->fetch_array(MYSQLI_ASSOC))
$rows[] = $row;
foreach ($rows as $row)
{
print "email: " . $row['email'] . "<br />";
// etc
}
You might wanna start with the manual...
$row = mysqli_fetch_array($mQuery, MYSQLI_ASSOC);
or
$row = $mQuery->fetch_array(MYSQLI_ASSOC);
As you are using object notation, go with option 2.
perhaps this will help:
if($result = mysqli_query($conn, $mQuery))
{
while($row = mysqli_fetch_assoc($result))
{
print_r($row);
}
mysqli_free_result($result);
}