2 Replies - 3156 Views - Last Post: 20 July 2011 - 06:54 PM Rate Topic: -----

#1 pule44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-July 11

SELECT DATA FROM TWO MYSQL TABLES

Posted 20 July 2011 - 05:05 PM

Hi everyone I have a little problem here:

I have a table named logins and another one named details. The login table has my loginID,email address and password
and the details table has my loginID, my first name and last name. So what I want to do is retrieve data from both tables
and echo it out. I came up with the following script:

<?php

$command = "SELECT m1.email, m2.first_name, m2.last_name FROM logins m1, details m2 WHERE m1.loginID = m2.loginID AND m1.loginID='$id'";
 $select = mysql_query($command, $db);
  
  if($row = mysql_fetch_array($select))
  {
      echo $row['first_name'];
      echo $row['last_name']
      echo $row['email'];
      
  }
  else
  {
    echo "Oops some error occured somewhere!";
   }


?>



The error I get is that I have not properly used the mysql_fetch_array.
For the $command I have tried using all kinds of UNIONS i still can't get the results I want. But when I select one value from one table the code works just fine. I was wondering if anyone can help me to be able to echo out the results from both tables.

I have tried using the $command as:
<?php
 $command ="SELECT m1.email, m2.first_name, m2.last_name FROM logins m1 FULL JOIN details m2 ON m1.loginID=m2.loginID WHERE m1.loginID='".$_SESSION['loginID']."'";
.
.
?>


oh one thing i forgot to mention is that I assigned session variable to $id, hence the $id in the $command.

This post has been edited by pule44: 20 July 2011 - 05:08 PM


Is This A Good Question/Topic? 0
  • +

Replies To: SELECT DATA FROM TWO MYSQL TABLES

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: SELECT DATA FROM TWO MYSQL TABLES

Posted 20 July 2011 - 05:50 PM

Hi, just echo out $command and see what your query looks like, try running it directly through mysql and see what type of error you get. Since mysql_query returns false or a PHP resource you could also do something like:
 if($select = mysql_query($command, $db))
 {

 }
 else
 {
  echo mysql_error();
 }


Was This Post Helpful? 0
  • +
  • -

#3 pule44  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-July 11

Re: SELECT DATA FROM TWO MYSQL TABLES

Posted 20 July 2011 - 06:54 PM

Ok Thanks a lot I have been able to solve the problem. I did this by writing the $command this way:

<?php
  $command = "SELECT * from logins FULL JOIN details WHERE logins.loginID='".$_SESSION['loginID']."'";
  $result = mysql_query($command, $db);
  .
  .
  .  

?>



and it worked the way I wanted.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1