| | |
Search two tables
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Aug 2005
Posts: 9
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: May 2005
Posts: 514
Reputation:
Solved Threads: 19
two ways...
First way is to JOIN:
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:
VERY dirty code there but should give you general idea.
the proper way is to do it with a JOIN
First way is to JOIN:
PHP Syntax (Toggle Plain Text)
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:
PHP Syntax (Toggle Plain Text)
<? // 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
•
•
Join Date: Aug 2005
Posts: 9
Reputation:
Solved Threads: 0
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?
[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?
•
•
Join Date: May 2005
Posts: 514
Reputation:
Solved Threads: 19
Well what is the error you are getting?
•
•
Join Date: Aug 2005
Posts: 9
Reputation:
Solved Threads: 0
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..!!
[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..!!
•
•
Join Date: May 2005
Posts: 514
Reputation:
Solved Threads: 19
No problem. Glad I could help.
![]() |
Similar Threads
- Why do people wish for tableless with CSS? (HTML and CSS)
- php drop down menu to search multiple sql tables (PHP)
- Query multiple tables with duplicate data (MySQL)
- ORDER BY - join column (MS SQL)
- MS access help (Visual Basic 4 / 5 / 6)
- Searching within arrays problem (PHP)
- URGENT: Implementing search with multiple dissimilar MySQL tables (MySQL)
- hello and plz help (PHP)
Other Threads in the PHP Forum
- Previous Thread: rand number
- Next Thread: checkbox values calculation with PHP
| Thread Tools | Search this Thread |
.htaccess ajax apache api array beginner binary broken cakephp checkbox class cms code crack cron curl database date directory display download dynamic echo email error fcc file files folder form forms freelancing function functions google href htaccess html image include incode insert integration ip java javascript joomla limit link login loop mail menu method mlm mod_rewrite multiple mysql oop pageing pagerank paypal pdf php problem query radio random recursion recursiveloop regex remote script search server sessions sms soap source space sql strip_tags subversion support! syntax system table template tutorial update upload url validation validator variable video web window.onbeforeunload=closeme; xml youtube





