Search two tables

Reply

Join Date: Aug 2005
Posts: 9
Reputation: eugui is an unknown quantity at this point 
Solved Threads: 0
eugui eugui is offline Offline
Newbie Poster

Search two tables

 
0
  #1
Aug 17th, 2005
hi, i have two tables and i want to search in two tables and show info about the user.

i have:

ususarios
id name age login
1 ruan 14 ruan1

boleto
id name money login
5 ruan 43,00 ruan1

and i have this code:

[PHP]
if (isset($_POST['usr']) && isset($_POST['pwd'])) {

$usr = stripslashes($_POST['usr']);

$comsql = mysql_query("SELECT * FROM usuarios WHERE login = '".$usr."'");

$campo = mysql_fetch_array($comsql);

if ($_POST['pwd'] != $campo['senha']) {
print "Senha Inválida!<br><a href=''>Clique aqui</a> para tentar novamente.\n";

} else {
session_start();
$_SESSION['usr'] = $campo['id'];

print "
".userinfo($campo['id'], "name")."
".userinfo($campo['id'], "age")."
";
[/PHP]

how i search at boleto´s table to print the money?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 514
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: Search two tables

 
0
  #2
Aug 17th, 2005
two ways...

First way is to JOIN:

  1. SELECT table1.* FROM table1
  2. LEFT JOIN table2 ON table1.id=table2.id
  3. WHERE table2.id IS NULL;

The second way is to have two sql statements in your php.

First one gets the user name then the seocnd one does ANOTHER sql lookup on the seocnd table.

Alot of extra code. But it works fine.

Like THis:

  1. <?
  2. // make your sql connectin then
  3.  
  4. $sql = "SELECT * from users where id ='$id' ";
  5. $result = mysql_query($sql);
  6. while ($row = mysql_fetch_array($result)) {
  7. $user=$row[id];
  8. echo $user;
  9. }
  10. $sql2 = "SELECT * from secondtable where id ='$id' ";
  11. $result2 = mysql_query($sql2);
  12. while ($row2 = mysql_fetch_array($result2)) {
  13. $money=$row2[money];
  14. echo $money;
  15.  
  16. }
  17.  
  18. ?>


VERY dirty code there but should give you general idea.

the proper way is to do it with a JOIN
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 9
Reputation: eugui is an unknown quantity at this point 
Solved Threads: 0
eugui eugui is offline Offline
Newbie Poster

Re: Search two tables

 
0
  #3
Aug 17th, 2005
thanks but not work i tried this:

[PHP]
$sql = "SELECT * from usuarios where login = '".$usr."' ";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
$user=$row[id];

echo $user;
}
[/PHP]

and not work. i have this code and works fine
[PHP]
$comsql = mysql_query("SELECT * FROM usuarios WHERE login = '".$usr."'");

$campo = mysql_fetch_array($comsql);

print "
".userinfo($campo['id'], "name")."
";
[/PHP]

why your code no work?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 514
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: Search two tables

 
0
  #4
Aug 17th, 2005
Well what is the error you are getting?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 9
Reputation: eugui is an unknown quantity at this point 
Solved Threads: 0
eugui eugui is offline Offline
Newbie Poster

Re: Search two tables

 
0
  #5
Aug 17th, 2005
my script now:

[PHP]
<?

if (isset($_POST['usr']) && isset($_POST['pwd'])) {

$usr = stripslashes($_POST['usr']);

$sql = "SELECT * FROM usuarios WHERE login = '".$usr."' ";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$nome=$row[nome];


if ($_POST['pwd'] != $row['senha']) {
print "Senha Inválida!<br><a href=''>Clique aqui</a> para tentar novamente.\n";

} else {

session_start();
$_SESSION['usr'] = $campo['id'];

print "
<div align='center'>
<center>

<table border='0' width='100%'>
<tr>
<td width='100%'><center><font size='1'>$nome</font></center></td>
</tr>
</table>

</center>
</div>
";
?>

<?
$sql2 = "SELECT * FROM boleto WHERE login = '".$usr."' ";
$result2 = mysql_query($sql2);
while ($row2 = mysql_fetch_array($result2)) {
$id=$row2[id];

print "
<div align='center'>
<center>
<table border='0' width='100%'>
<tr>
<td width='100%'><center><font size='1'><a href='ver_boleto.php?id=$id'>Ver Boleto</a></font></center></td>
</tr>
</table>
</center>
</div>
";
}
?>

<?
}
}

} else {

print "<center><form action='?act=login' method='post'>\n";
print "Usuário:<br><input type='text' name='usr' size='20'><br>\n";
print "Senha:<br><input type='password' name='pwd' size='20'><p>\n";
print "<input type='submit' value='( Entrar ))'></form>\n";

}

mysql_close() or die("");

?>
[/PHP]

now is working...thanks guy..!!
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 514
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: Search two tables

 
0
  #6
Aug 18th, 2005
No problem. Glad I could help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC