getting data from a database with a while loop

  • Black_Legon
  • Beginner
  • Beginner
  • Black_Legon
  • Posts: 35
  • Loc: Netherlands

Post 3+ Months Ago

Hi all,

I'm trying to get from one person data from a database... and want to put it in var's. I've tryed to do this by using a while loop. but that won't do the trick..
This is the code i'm using now:

PHP Code: [ Select ]
 
mysql_connect("$server","$user", "$wachtwoord");            
 
mysql_select_db("$database");    
 
$id="$_SESSION[memberid]";          
 
$result = mysql_query("SELECT * FROM $table WHERE id ='$id'");
 
 
 
while ($object = mysql_fetch_object($result)) {
 
$mail = "$object->email";
 
$username = "$object->username";
 
}
 
 
  1.  
  2. mysql_connect("$server","$user", "$wachtwoord");            
  3.  
  4. mysql_select_db("$database");    
  5.  
  6. $id="$_SESSION[memberid]";          
  7.  
  8. $result = mysql_query("SELECT * FROM $table WHERE id ='$id'");
  9.  
  10.  
  11.  
  12. while ($object = mysql_fetch_object($result)) {
  13.  
  14. $mail = "$object->email";
  15.  
  16. $username = "$object->username";
  17.  
  18. }
  19.  
  20.  


and this one:
PHP Code: [ Select ]
 
while ($row = mysql_fetch_assoc($result)) {
 
$mail = $row['email'];
 
$username = $row['username'];
 
}
 
 
  1.  
  2. while ($row = mysql_fetch_assoc($result)) {
  3.  
  4. $mail = $row['email'];
  5.  
  6. $username = $row['username'];
  7.  
  8. }
  9.  
  10.  


But i get the folowing error using it:
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource

Someone has a better id?

Grs,
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

PHP Code: [ Select ]
$db = mysql_connect("$server","$user", "$wachtwoord");            
 
mysql_select_db("$database");    
 
$id="$_SESSION[memberid]";  
 
$query   = "SELECT * FROM $table WHERE id ='$id'";
 
$result = mysql_query($query,$db);
 
 
 
if ($row = mysql_fetch_array($result)) {
 
   $mail = $row['email'];
 
   $username = $row['username'];
 
}
  1. $db = mysql_connect("$server","$user", "$wachtwoord");            
  2.  
  3. mysql_select_db("$database");    
  4.  
  5. $id="$_SESSION[memberid]";  
  6.  
  7. $query   = "SELECT * FROM $table WHERE id ='$id'";
  8.  
  9. $result = mysql_query($query,$db);
  10.  
  11.  
  12.  
  13. if ($row = mysql_fetch_array($result)) {
  14.  
  15.    $mail = $row['email'];
  16.  
  17.    $username = $row['username'];
  18.  
  19. }


the problem is that you have not assigned a valid mysql connection in the mysql_query call. Also you have no need loop if it is a single result.

the above code should work :roll: (just hacked it together)
  • Black_Legon
  • Beginner
  • Beginner
  • Black_Legon
  • Posts: 35
  • Loc: Netherlands

Post 3+ Months Ago

tnx....

But i got the same error using that one.

The query was wrong.. had some trouble with the var's in it :).. but i use this one now.. and it works ok for now:
PHP Code: [ Select ]
("SELECT * FROM ".$table." WHERE memberid ='".$id."'")


tnx for your help :)

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 154 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.