How to select data from multiple table in a datatbase.

Now i have a problem In my case
i have more than 1
table and i want to use
the
user_exist(); function construct
i use
the sql clause count
(id) Where username
= variable username

and all this you selected
from a table. My problem
is that i want to
perform this same
function but on three
tables at the same time. What i did was
to create an array for
the three table
[php] $table = array
(‘admin’,‘users’,’
moderate’);
$table = '' .implode(',
' , $table). '';
function user_exist($
username){
$sql=myql_query
(“SELECT COUNT(id)
FROM $table WHERE
username=$username”)
; return (mysql_result($
sql, 0)==1)? true :
false;}[/php]
. So what am trying to do is
check if the username
from the form is in any
of the tables in my
array $table. If the user
is not found we return
false. But it seems i
have sucessed in
scripting rubbish. So can
you be so kind in helping
me out with the right
snippet to do this
argument. Thanks in
advance. And i did some research and discovered that there what called table join. And it of three types left join right join and join in but i can’t use it cause i don’t understand it at all. Can some one assit me pls write the code for the table join with my three tables they all have id column n username column what i actualy need is to select the username count from the database. Thanks once again.

Firstly, if you are storing a username in more than one table, you have a bad database design.

Secondly, you are using deprecated MySQL calls. You need to use PDO or MySQLI with prepared statements.

On another note, I once had to do a job that had a similar bad database design, and had to find out if a user was in one of several different databases. It can be as simple as using an if else clause. Query your first table, if there are no results then query the second table, if no results in the second table, query the third table.

to answer your 1st question. The table is for different categories of people. And therefore the username are different but each table has a column 4 user name. Thou i wasn’t the originator of the database. I am only trying to correct some errors without altering the database. And i don’t know PDO. About the logical expression i tried that before posting here i was pretty wrong.[php]
$sql=mysql_query("SELECT COUNT(id) From admin where username=’$username’ ");
if ($sql<1)
{ // then i run anoda count for the next table
}
if($sql2<1){//run the last select query }if($sql3<1){ return false; }
[/php] it will always return false. Please Boss write a valid logic for me Please thank a lot.

In order of your response, I realize you inherited the database, but there is still a better way to not have redundant usernames. We can get to that later.

If you’re interested in learning PDO download the PDO bump start database from the link in my signature. That will get you a good start to learn and seeing how it works.

I am traveling and using a phone to respond to you so I am not able to easily write code for you at this time. You are missing some basic code to handle the results of your query.

Look over some tutorials on how to do a simple MySQL query. Do a query on a known table for a known username and get a result that you can display. Once you get that part figured out you can get into the basic if else of each table.

Since you’re using deprecated code, you can go to w3schools.com and view their tutorial on MySQL.

Thank for the assitance. But i haven’t gotten it. And thanks for the link for pdo. I will start that class later.

Bro i am tried d logical expression again but am nt stil getin it. Please wenever you arrieved at ur destination. Please script the logic for me. And safe Journey bro.

Sponsor our Newsletter | Privacy Policy | Terms of Service