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:

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")."
";

how i search at boleto´s table to print the money?

Recommended Answers

All 5 Replies

two ways...

First way is to JOIN:

SELECT table1.* FROM table1
      LEFT JOIN table2 ON table1.id=table2.id
    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:

<?
// make your sql connectin then

 $sql = "SELECT * from users where id ='$id' ";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result)) {
    $user=$row[id];
  echo $user;
  }
 $sql2 = "SELECT * from secondtable where id ='$id' ";
    $result2 = mysql_query($sql2);
    while ($row2 = mysql_fetch_array($result2)) {
    $money=$row2[money];
 echo $money;

  }

?>

VERY dirty code there but should give you general idea.

the proper way is to do it with a JOIN

thanks but not work i tried this:

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

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

echo $user;
}

and not work. i have this code and works fine

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

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

why your code no work?

Well what is the error you are getting?

my script now:

<?

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("");

?>

now is working...thanks guy..!!

No problem. Glad I could help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.