Jump to content

MySql Query Question


Lamez

Recommended Posts

alright I have googled my problem in hope of looking for a solution, but I found nothing.

 

I am trying to pull the email from the user table for the user.

but I get this: Resource id #20 for the email output

 

what does it mean?

 

here is my query:

 

$email  = mysql_query("SELECT email FROM ".TBL_USERS." WHERE username = '$uname'");

am I doing somthing wrong?

Link to comment
Share on other sites

oh ok, this is for a members list (if you would like to see it in action: http://www.lamezz.com/user/viewmembers.php?sort=online, user: test1 pass:test)

 

anyways

 

here is the whole function

/*Sorts by Online*/
function displayOnline(){
global $database;
   $q = "SELECT username,timestamp "
       ."FROM ".TBL_ACTIVE_USERS." ORDER BY timestamp DESC,username";
   
/*$q = "SELECT username, timestamp "
."FROM".TBL_ACTIVE_USERS. "INNER JOIN".TBL_USERS."ON".TBL_ACTIVE_USERS.username "=" .TBL_USERS.email	*/   
   $result = $database->query($q);
   /* Error occurred, return given name by default */
   $num_rows = mysql_num_rows($result);
   if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "Database table empty";
      return;
   }
   /* Display table contents */
   echo "<table width=\"550\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n"; //Table's Specs
   echo "<tr>\n";
   echo "<td valign=\"bottom\"><strong>Username</strong></td>\n"; //Username Column
   echo "<td width=\"80\" valign=\"bottom\"> </td>\n"; //Table Spacer
   echo "<td valign=\"bottom\"><strong>Email</strong></td>\n"; //Email Column
   echo "<td valign=\"bottom\"> </td>\n"; //Table Spacer
   echo "<td valign=\"bottom\"><strong>Status</strong></td>\n"; //Status Column
   echo "</tr>\n";
   echo "<tr>\n";
   echo "<td height=\"1\" colspan=\"5\" align=\"center\" valign=\"top\"><div class=\"dot\"></div></td>\n"; //Dotted Line
   echo "</tr>\n";

   for($i=0; $i<$num_rows; $i++){
      $uname  = mysql_result($result,$i,"username");
  $email  = mysql_query("SELECT email FROM ".TBL_USERS." WHERE username = 'email'");
      $time   = mysql_result($result,$i,"timestamp");
  
   echo "<tr>\n"; 
   echo "<td><a href=\"userinfo.php?user=$uname\">$uname</a><br></td>\n";
   echo "<td> </td>\n";
   echo "<td>$email</td>\n";
   echo "<td> </td>\n";
   echo "<td align=\"left\">\n";

/* Tells the if they are online or offline */	
$query = mysql_query("SELECT u.username FROM users u INNER JOIN active_users a ON a.username=u.username WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE) AND     u.username='$uname'")or die(mysql_error());
if (mysql_num_rows($query) > 0){
echo "<font color=\"#04db04\">Online</font>";
} else {
echo "<font color=\"#FF0000\">Offline</font>";
  }
}
   echo "</td>\n";
   echo "</tr>\n";
   echo "</table>\n";
}

 

hope that helps you to help me!

Link to comment
Share on other sites

Here is a quick and dirty crash course, of course there are many ways, this is just one, but it lists each step.

 

$query2 = "SELECT column FROM table WHERE column = '$name'";  //build the query

$result2 = mysql_query ($query2) or die ("Error in query: $query2. " . mysql_error()); //execute the query, get the Resource number in $result2 variable

 

if (mysql_num_rows ($result2) > 0)  {  //see if something was found

while ($row2 = mysql_fetch_object ($result2)) {  //get the info from that result using a mysql_fetch statement

Link to comment
Share on other sites

ok I am getting errors about my brackets,

 

here is what I am using as my query and result

      $eq = "SELECT email FROM ".TBL_USERS." WHERE email = '$uname'";  //build the query 
  $email = mysql_query ($eq) or die ("Error in query: $eq. " . mysql_error()); //execute the query, get the Resource number in $result2 variable  

if (mysql_num_rows ($email) > 0)  {  //see if something was found
while ($row2 = mysql_fetch_object ($email)    //get the info from that result using a mysql_fetch statement
}

 

am I doing it wrong, maybe If I explain better what I am trying to do:

 

I am trying to pull the column email from the users table, and place it with the matching username in the active users table.

Link to comment
Share on other sites

ok now I get another error about I have a unexpected } on like 166

/*Sorts by Online*/
function displayOnline(){
global $database;
   $q = "SELECT username,timestamp "
       ."FROM ".TBL_ACTIVE_USERS." ORDER BY timestamp DESC,username";
   

   $result = $database->query($q);
   /* Error occurred, return given name by default */
   $num_rows = mysql_num_rows($result);
   if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "Database table empty";
      return;
   }
   /* Display table contents */
   echo "<table width=\"550\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n"; //Table's Specs
   echo "<tr>\n";
   echo "<td valign=\"bottom\"><strong>Username</strong></td>\n"; //Username Column
   echo "<td width=\"80\" valign=\"bottom\"> </td>\n"; //Table Spacer
   echo "<td valign=\"bottom\"><strong>Email</strong></td>\n"; //Email Column
   echo "<td valign=\"bottom\"> </td>\n"; //Table Spacer
   echo "<td valign=\"bottom\"><strong>Status</strong></td>\n"; //Status Column
   echo "</tr>\n";
   echo "<tr>\n";
   echo "<td height=\"1\" colspan=\"5\" align=\"center\" valign=\"top\"><div class=\"dot\"></div></td>\n"; //Dotted Line
   echo "</tr>\n";

   for($i=0; $i<$num_rows; $i++){
      $uname  = mysql_result($result,$i,"username");
      $eq = "SELECT email FROM ".TBL_USERS." WHERE email = '$uname'";  //build the query 
  $email = mysql_query ($eq) or die ("Error in query: $eq. " . mysql_error()); //execute the query, get the Resource number in $result2 variable  
      $time   = mysql_result($result,$i,"timestamp");

if (mysql_num_rows ($email) > 0) {  //see if something was found
while ($row2 = mysql_fetch_object ($email))   //get the info from that result using a mysql_fetch statement
}
   echo "<tr>\n"; 
   echo "<td><a href=\"userinfo.php?user=$uname\">$uname</a><br></td>\n";
   echo "<td> </td>\n";
   echo "<td>$email</td>\n"; 
   echo "<td> </td>\n";
   echo "<td align=\"left\">\n";

/* Tells the if they are online or offline */	
$query = mysql_query("SELECT u.username FROM users u INNER JOIN active_users a ON a.username=u.username WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE) AND     u.username='$uname'")or die(mysql_error());
if (mysql_num_rows($query) > 0){
echo "<font color=\"#04db04\">Online</font>";
} else {
echo "<font color=\"#FF0000\">Offline</font>";
  }
}
   echo "</td>\n";
   echo "</tr>\n";
   echo "</table>\n";
}

what is wrong..here is my whole code here is my query

$eq = "SELECT email FROM ".TBL_USERS." WHERE email = '$uname'";  //build the query 
  $email = mysql_query ($eq) or die ("Error in query: $eq. " . mysql_error()); //execute the query, get the Resource number in $result2 variable  


if (mysql_num_rows ($email) > 0) {  //see if something was found
while ($row2 = mysql_fetch_object ($email))   //get the info from that result using a mysql_fetch statement
} //166

Link to comment
Share on other sites

right, but when I do that anything in between the {} are hidden

 

here is what I have so I can get some sort of output

 

if (mysql_num_rows ($email) > 0)   //see if something was found
while ($row2 = mysql_fetch_object ($email)) {  }//get the info from that result using a mysql_fetch statement

 

but then I still get a Resource id #20

 

 

Link to comment
Share on other sites

Here is a much clearer example of how it should be done.

 

<?php

  $sql = "SELECT email FROM ".TBL_USERS." WHERE email = '$uname' LIMIT 1";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      echo $uname . "'s email address is " . $row['email'];
    } else {
      echo "No results found";
    }
  } else {
    echo mysql_error() . "<br />$sql";
  }

?>

 

You do not need a while loop if your only expecting 1 result.

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.