Jump to content

[SOLVED] Returning database results


soycharliente

Recommended Posts

I'm trying to return the entries in a database that meet certain criteria.

I have a database called "users", hopefully what's in it is obvious, where each entry has 4 properties: Username, Password, Email, and Status. I want to return the entries that have Status = Unapproved and list them on a page as text.

I have this code:
[code]<?php
$loggedIn = FALSE;
if (isset($HTTP_SESSION_VARS['LoggedInUser'])) {
$loggedIn = TRUE;
}
if ($loggedIn) {
//Connect To Database
$hostname="...";
$username="...";
$password="...";
$dbname="...";

mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect! Please try again.");
mysql_select_db($dbname);

$query = "SELECT * FROM Users WHERE Status='Unapproved'";
$result = mysql_query($query);
}
?>

<p>
<?php
if ($loggedIn && $HTTP_SESSION_VARS["Role"] == "Admin") {
if ($result) { echo $result; }
else { echo "No unapproved users."; }
} else {
echo "You do not have permission to view this file.";
} ?>
</p>[/code]

Which returns: Resource id #5

What does that mean? Does that mean that the only entry in the database that has Status = Unapproved is in slot number 5? That can't be correct because I have 11 dummy entries and all of them are "Unapproved", except for myself of course.

Any help is appreciated and thanks in advance.
Link to comment
https://forums.phpfreaks.com/topic/26884-solved-returning-database-results/
Share on other sites

When you do a database query the results are in an 'object' - in this case "Resource id #5". You need to pull the records from the object and act upon them.

[code]<?php
$loggedIn = FALSE;
if (isset($HTTP_SESSION_VARS['LoggedInUser'])) {
$loggedIn = TRUE;
}
if ($loggedIn) {
//Connect To Database
$hostname="...";
$username="...";
$password="...";
$dbname="...";

mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect! Please try again.");
mysql_select_db($dbname);

$query = "SELECT * FROM Users WHERE Status='Unapproved'";
$result = mysql_query($query);


if ($loggedIn && $HTTP_SESSION_VARS["Role"] == "Admin") {

    //cHECK IF ANY ROWS WERE RETURNED IN THE QUERY
    if (mysql_num_rows($result)) {

echo "<table>";

//CREATE A LOOP TO GO THROUGH ALL THE RECORDS ONE AT A TIME
//SET $row = AN ARRAY WITH THE RECORDS FOR THAT ROW
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

    echo "<tr>";
    echo "<td>".$row[Username]."</td>";
    echo "<td>".$row[Email]."</td>";
    echo "<td>".$row[Status]."</td>";
    echo "</tr>";
        }
echo "</table>";

    } else {

echo "No unapproved users.";
    }
} else {
echo "You do not have permission to view this file.";
} ?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.