BluwAngel Posted March 1, 2012 Share Posted March 1, 2012 i need to create search in database like this select everything from database but it cant be included rows where session id is equal to id of user code $datum = "$godina-$mjesec-$dan"; $event_select = mysql_query("SELECT * FROM events WHERE event_date='$datum'"); //izlistat evente while ($events = mysql_fetch_array($event_select)) { $id_user = $events['id_user']; $user_select= mysql_query("SELECT * FROM users WHERE id='$id_user' "); $user = mysql_fetch_array($user_select); ... creating table i tried to put something like AND id!=$_SESSION[id] but it didnt work so i need create table for every row where id_user is not equal to session id now it works and i get table for every row do i need if loop or? Link to comment https://forums.phpfreaks.com/topic/258042-php-with-mysql-session-id/ Share on other sites More sharing options...
AyKay47 Posted March 1, 2012 Share Posted March 1, 2012 Instead of having a query inside of a loop, which you should always avoid doing, use a JOIN and filter the results of the join using the ON clause. $event_select_sql = "SELECT e.*,u.* FROM events e LEFT JOIN users u ON (u.id = e.id_user) WHERE e.event_date = '$datum' AND e.id_user != {$_SESSION['id']}"; $event_select_query = mysql_query($event_select_sql) or die($event_select_sql . "<br />" . mysql_error()); while($events = mysql_fetch_array($event_select_query)) { //create table with db fields... } Also, avoid selecting every field from a db table, this will slow down the query. Instead, select only the fields that you are going to use. Link to comment https://forums.phpfreaks.com/topic/258042-php-with-mysql-session-id/#findComment-1322722 Share on other sites More sharing options...
BluwAngel Posted March 1, 2012 Author Share Posted March 1, 2012 it works, thanks but can you explain me a little what did you do there (im still new) im confused with that first row ( or can you link me manual for it) Link to comment https://forums.phpfreaks.com/topic/258042-php-with-mysql-session-id/#findComment-1322738 Share on other sites More sharing options...
AyKay47 Posted March 1, 2012 Share Posted March 1, 2012 Sure, I used whats called a join, which can link two or more tables together based on specific conditions. JOIN docs Link to comment https://forums.phpfreaks.com/topic/258042-php-with-mysql-session-id/#findComment-1322751 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.