fishbaitfood Posted December 19, 2011 Share Posted December 19, 2011 Hi there, I have this query, which outputs the records properly in PhpMyAdmin. But when using this query inside my PHP code, the result is incomplete. Instead of retrieving the expected 15 results, I only get the first one twice. Also, I don't get any errors. <?php $get_files = "SELECT `id` FROM `files` WHERE `category` = 'W';"; include_once('connection.inc.php'); $con = mysql_connect(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD) or die ("Connection failed: " . mysql_error()); $dbname = "myDB"; $select_db = mysql_select_db($dbname, $con) or die ("Can't open database: " . mysql_error()); $result_files = mysql_query($get_files) or die ("Query failed: " . mysql_error()); $files = mysql_fetch_array($result_files); $W_files = "'".implode("', '", $files)."'"; // echo $W_files; outputs the first number twice, instead of the expected 15 different ones $get_checklist = "SELECT * FROM `checklist` WHERE `id` IN ($W_files) ORDER BY `id` ASC;"; $result_checklist = mysql_query($get_checklist) or die ("Query failed: " . mysql_error()); // .... code for displaying ?> Quote Link to comment https://forums.phpfreaks.com/topic/253478-mysql-query-not-working-with-php/ Share on other sites More sharing options...
ManiacDan Posted December 19, 2011 Share Posted December 19, 2011 You need to use loops to get all the rows. Right now you're just fetching the first result of the first query, and using it as the only ID in your second query. The manual has proper syntax. Quote Link to comment https://forums.phpfreaks.com/topic/253478-mysql-query-not-working-with-php/#findComment-1299333 Share on other sites More sharing options...
fishbaitfood Posted December 19, 2011 Author Share Posted December 19, 2011 ManiacDan, ofcourse! Totally forgot about it, because I already have a loop for displaying the results. Sorry for starting this topic! Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/253478-mysql-query-not-working-with-php/#findComment-1299334 Share on other sites More sharing options...
ManiacDan Posted December 19, 2011 Share Posted December 19, 2011 You should actually be doing this with one JOIN to join these two tables. Quote Link to comment https://forums.phpfreaks.com/topic/253478-mysql-query-not-working-with-php/#findComment-1299337 Share on other sites More sharing options...
fishbaitfood Posted December 19, 2011 Author Share Posted December 19, 2011 Hmm, yeah, I know, but I'm having trouble linking my two tables in MySQL. It's causing me a lot of errors. Quote Link to comment https://forums.phpfreaks.com/topic/253478-mysql-query-not-working-with-php/#findComment-1299343 Share on other sites More sharing options...
ManiacDan Posted December 19, 2011 Share Posted December 19, 2011 SELECT checklist.* from checklist JOIN files ON checklist.id = files.id WHERE files.category = 'W' Quote Link to comment https://forums.phpfreaks.com/topic/253478-mysql-query-not-working-with-php/#findComment-1299344 Share on other sites More sharing options...
fishbaitfood Posted December 19, 2011 Author Share Posted December 19, 2011 Oh yeah, linking my tables with foreign keys isn't necessary for this. I need to dig in some more where using foreign keys is handy. Thanks for the JOIN query! Saved me severel rows of code! Quote Link to comment https://forums.phpfreaks.com/topic/253478-mysql-query-not-working-with-php/#findComment-1299345 Share on other sites More sharing options...
ManiacDan Posted December 19, 2011 Share Posted December 19, 2011 More importantly, it saved you a round-trip to the database. 500 lines of PHP can execute in the time it takes to make a round-trip to the database to execute a query. Quote Link to comment https://forums.phpfreaks.com/topic/253478-mysql-query-not-working-with-php/#findComment-1299346 Share on other sites More sharing options...
fishbaitfood Posted December 19, 2011 Author Share Posted December 19, 2011 Indeed.. Thanks a bunch ManiacDan! Quote Link to comment https://forums.phpfreaks.com/topic/253478-mysql-query-not-working-with-php/#findComment-1299348 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.