MySQL search method through PHP -


quick question on method search mysql database php.

right now, have function supposed search , return results. have 2 databases, 1 being user , 1 being profile. user stores username, email, password. while profile stores user first name, last name, address, birth day. right now, i'm not sure how go this, have far.

i want able search both tables , return list of results via table i've got covered, don't know how intricacies down. function contain either null or value of variable. right now, sketch up:

if(!empty($username)): $append .= "where"; $append .= "username = ".$username.""; endif; if(!empty($email)): $append .= "where"; $append2 .= "email= ".$email.""; endif; if(!empty($firstname)): $append .= "where"; $append2 .= "firstname = ".$firstname.""; endif; if(!empty($lastname)): $append .= "where"; $append2 .= "lastname= ".$lastname.""; endif;  $sql = "select * users ".$append.""; $result = mysql_query($sql); $sql2 = "select * profile ".$append2.""; $result2 = mysql_query($sql2); $userid = mysql_fetch_row($result2); $userid['id'] = $id; <-- 1 call display data. 

how can efficiently , search/return unique/distinct user id's? both tables include user id / incremented id number (user table user_id, profile table acct_id). know code bit wrong... don't worry escaping - i;'ve gotten sorted. should use join statement?

other problem facing changing between , and because if 1 var set isn't, must use , instead of 1 where. idea how tackle issue?

thanks input!

for where clause best use arrays , implode() this

$and_where = array(); if (!empty($username))     $and_where[] = "username = ".$username; if (!empty($email))     $and_where[] = "email = ".$email; //etc if (count($and_where) > 0)     $where = " ".implode(" , ", $and_where); else     $where = ""; 

are 2 tables related in matter? if acct_id foreign key user_id can use inner join ($where shown above)

$query = "select users.col, ..., profile.col, ... users     inner join profile on users.user_id = profile.acct_id".$where; 

if aren't, union them

$users_and_where = array(); $profiles_and_where = array(); if (!empty($username))     $users_and_where[] = "username = ".$username; if (!empty($email))     $users_and_where[] = "email = ".$email; //etc if (!empty($firstname))     $profiles_and_where[] = "firstname = ".$firstname; if (!empty($lastname))     $profiles_and_where[] = "lastname = ".$lastname; //etc if (count($users_and_where) > 0)     $users_where = " ".implode(" , ", $users_and_where); else     $users_where = ""; if (count($profiles_and_where) > 0)     $profiles_where = " ".implode(" , ", $users_and_where); else     $profiles_where = ""; $query = "(select col1, col2, ... users".$users_where.")     union         (select col1, col2, ... profile".$profiles_where.")"; 

you should try avoid * in queries , select rows specifically, way don't have overhead in future, when additional columns introduced code doesn't use here.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -