timmah1 Posted May 5, 2007 Share Posted May 5, 2007 I can select the tables, I can pull out the information, what it's not doing is grabbing and showing my id as well as the others that are approved on my list. My driverID is 5, on my list is driverID 6, when I query the bulletins database, it should pull everything from driverID 5 and driverID 6, but it's only showing driverID 6. How do I get it to show my driverID as well? Here is my dababase structure -- -- Table structure for table `friends` -- CREATE TABLE `friends` ( `ID` int(11) NOT NULL auto_increment, `friendID` varchar(255) NOT NULL default '', `driverID` varchar(255) NOT NULL default '', `approved` varchar(10) NOT NULL default '', KEY `ID` (`ID`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=112 ; -- -- Dumping data for table `friends` -- INSERT INTO `friends` VALUES (88, '6', '5', 'y'); INSERT INTO `friends` VALUES (106, '5', '6', 'y'); And here is my code $server = "localhost"; // server to connect to. $database = "xxx"; // the name of the database. $db_user = "xxx"; // mysql username to access the database with. $db_pass = "xxx"; // mysql password to access the database with. $table = "bulletins"; // the table that this script will set up and use. // connect to the mysql server $link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error()); // Select all friends for this person $result = mysql_query("SELECT * FROM friends WHERE driverID AND friendID = 5") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { if ($row[approved]=='y') { //Select all bulletins for this person's friends $result1 = mysql_query("SELECT * FROM $table WHERE driverID = $row[friendID]") or die(mysql_error()); while($row1 = mysql_fetch_array( $result1 )) { print "$row1[driverID]<br>"; } } else { echo "No Friends"; } } Quote Link to comment https://forums.phpfreaks.com/topic/50132-help-selecting-variables-from-database/ Share on other sites More sharing options...
Trium918 Posted May 5, 2007 Share Posted May 5, 2007 First of all there should be no $ variable sign in <?php //Select all bulletins for this person's friends $result1 = mysql_query("SELECT * FROM $table WHERE driverID = $row[friendID]") ?> should be <?php //Select all bulletins for this person's friends $result1 = mysql_query("SELECT * FROM table WHERE driverID = '$row[friendID]'") ?> Quote Link to comment https://forums.phpfreaks.com/topic/50132-help-selecting-variables-from-database/#findComment-246155 Share on other sites More sharing options...
londonjustin Posted May 5, 2007 Share Posted May 5, 2007 I think there's a problem with this line: $result = mysql_query("SELECT * FROM friends WHERE driverID AND friendID = 5") I think this query is only looking for rows where the friendID is 5, and ignoring the driverID altogether, which is why you're just getting the one row returned - either that or it would return rows where both the driverID *and* the friendID are 5, which would return zero rows. What you might need is something along these lines: $driverID = 5; $result = mysql_query("SELECT * FROM friends WHERE driverID = '$driverID' OR friendID = '$driverID' ") That should return all rows where either the driverID is 5 or the friendID is 5. Quote Link to comment https://forums.phpfreaks.com/topic/50132-help-selecting-variables-from-database/#findComment-246159 Share on other sites More sharing options...
timmah1 Posted May 5, 2007 Author Share Posted May 5, 2007 That was it londonjustin!!! Thank you very much. And Trium918, $table is a pre-defined variable, as for this $table = "bulletins"; // the table that this script will set up and use. I have no database called 'table', so this wouldn't work $result1 = mysql_query("SELECT * FROM table WHERE driverID = $row[friendID]") But since the variable is already passed, this is what works $result1 = mysql_query("SELECT * FROM $table WHERE driverID = $row[friendID]") Thank you very much for the input, everything is working great now Quote Link to comment https://forums.phpfreaks.com/topic/50132-help-selecting-variables-from-database/#findComment-246202 Share on other sites More sharing options...
timmah1 Posted May 5, 2007 Author Share Posted May 5, 2007 That works, but now I have this problem I want to limit the query to 5, when i do this $result1 = mysql_query("SELECT * FROM bulletins WHERE driverID = '$friend_id' ORDER BY 'bulletinID' LIMIT 5") or die(mysql_error()); It shows 6 results instead of 5 results. Also, I'm trying to view them by bulletinID from newest to oldest, and it don't do it. Did I put this in wrong? Quote Link to comment https://forums.phpfreaks.com/topic/50132-help-selecting-variables-from-database/#findComment-246219 Share on other sites More sharing options...
MadTechie Posted May 5, 2007 Share Posted May 5, 2007 try $result1 = mysql_query("SELECT * FROM bulletins WHERE driverID = '$friend_id' ORDER BY 'bulletinID' DESC LIMIT 0, 5") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/50132-help-selecting-variables-from-database/#findComment-246223 Share on other sites More sharing options...
timmah1 Posted May 5, 2007 Author Share Posted May 5, 2007 That showed 6 results as well Quote Link to comment https://forums.phpfreaks.com/topic/50132-help-selecting-variables-from-database/#findComment-246231 Share on other sites More sharing options...
MadTechie Posted May 5, 2007 Share Posted May 5, 2007 can you post all your lastest code please, i'll review it in Code Tags aka # Quote Link to comment https://forums.phpfreaks.com/topic/50132-help-selecting-variables-from-database/#findComment-246250 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.