unemployment Posted January 3, 2011 Share Posted January 3, 2011 $q = "SELECT `users.id`, `users.username`, `users.firstname`, `users.lastname`, `users.accounttype`, `companies.companyid`, `companies.companyname`, `companies.companyoccupation`, `companies.country`, `companies.state`, `companies.city`, `companies.industry` FROM `users`, `companies` WHERE `users.id` = `companies.companyid`"; Error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /home/www-data/mysite.com/fresh.php on line 25 Link to comment https://forums.phpfreaks.com/topic/223269-query-is-wrong/ Share on other sites More sharing options...
the182guy Posted January 3, 2011 Share Posted January 3, 2011 It's not the query, it's your code - you must be passing the query into mysql_fetch_assoc(), you should be passing the query into mysql_query() and the result of that to mysql_fetch_assoc(). $q = "SELECT `users.id`, `users.username`, `users.firstname`, `users.lastname`, `users.accounttype`, `companies.companyid`, `companies.companyname`, `companies.companyoccupation`, `companies.country`, `companies.state`, `companies.city`, `companies.industry` FROM `users`, `companies` WHERE `users.id` = `companies.companyid`"; $result = mysql_query($q); if($res && mysql_num_rows($result) > 0) { while($row = mysql_fetch_assoc($result)) { echo $row['firstname']; } } Link to comment https://forums.phpfreaks.com/topic/223269-query-is-wrong/#findComment-1154222 Share on other sites More sharing options...
MMDE Posted January 3, 2011 Share Posted January 3, 2011 $q = "SELECT `users.id`, `users.username`, `users.firstname`, `users.lastname`, `users.accounttype`, `companies.companyid`, `companies.companyname`, `companies.companyoccupation`, `companies.country`, `companies.state`, `companies.city`, `companies.industry` FROM `users`, `companies` WHERE `users.id` = `companies.companyid`"; Error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /home/www-data/mysite.com/fresh.php on line 25 please post full script! You should: $result=mysql_query($q) or die(mysql_error()); $resultrow=mysql_fetch_assoc($result); Link to comment https://forums.phpfreaks.com/topic/223269-query-is-wrong/#findComment-1154223 Share on other sites More sharing options...
unemployment Posted January 3, 2011 Author Share Posted January 3, 2011 I added in your suggestion but I still get this.. Unknown column 'users.id' in 'field list' I have a users table with the field id. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/223269-query-is-wrong/#findComment-1154235 Share on other sites More sharing options...
unemployment Posted January 3, 2011 Author Share Posted January 3, 2011 Essentially I have this... $q = "SELECT `users.id`, `users.username`, `users.firstname`, `users.lastname`, `users.accounttype`, `companies.companyid`, `companies.companyname`, `companies.companyoccupation`, `companies.country`, `companies.state`, `companies.city`, `companies.industry` FROM `users`, `companies` WHERE `users.id` = `companies.companyid`"; $r = mysql_query($q) or die(mysql_error()); while($r = mysql_fetch_assoc($q)) Link to comment https://forums.phpfreaks.com/topic/223269-query-is-wrong/#findComment-1154237 Share on other sites More sharing options...
MMDE Posted January 3, 2011 Share Posted January 3, 2011 try this: $q = 'SELECT users.id, users.username, users.firstname, users.lastname, users.accounttype, companies.companyid, companies.companyname, companies.companyoccupation, companies.country, companies.state, companies.city, companies.industry FROM users LEFT JOIN companies ON users.id=companies.companyid'; also, if that is all the fields, you could just use * instead of all the column names! EDIT: sorry, I did a typo, but edited it! Link to comment https://forums.phpfreaks.com/topic/223269-query-is-wrong/#findComment-1154240 Share on other sites More sharing options...
unemployment Posted January 3, 2011 Author Share Posted January 3, 2011 try this: $q = 'SELECT users.id, users.username, users.firstname, users.lastname, users.accounttype, companies.companyid, companies.companyname, companies.companyoccupation, companies.country, companies.state, companies.city, companies.industry FROM users LEFT JOIN companies ON users.id=companies.companyid'; also, if that is all the fields, you could just use * instead of all the column names! EDIT: sorry, I did a typo, but edited it! I tried your suggestion. Code: $q = 'SELECT users.id, users.username, users.firstname, users.lastname, users.accounttype, companies.companyid, companies.companyname, companies.companyoccupation, companies.country, companies.state, companies.city, companies.industry FROM users LEFT JOIN companies ON users.id=companies.companyid'; $r = mysql_query($q) or die(mysql_error()); while($r = mysql_fetch_assoc($q)) { but I still get this error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /home/www-data/mysite.com/fresh.php on line 27 Link to comment https://forums.phpfreaks.com/topic/223269-query-is-wrong/#findComment-1154247 Share on other sites More sharing options...
MMDE Posted January 3, 2011 Share Posted January 3, 2011 try this: $q = 'SELECT users.id, users.username, users.firstname, users.lastname, users.accounttype, companies.companyid, companies.companyname, companies.companyoccupation, companies.country, companies.state, companies.city, companies.industry FROM users LEFT JOIN companies ON users.id=companies.companyid'; also, if that is all the fields, you could just use * instead of all the column names! EDIT: sorry, I did a typo, but edited it! I tried your suggestion. Code: $q = 'SELECT users.id, users.username, users.firstname, users.lastname, users.accounttype, companies.companyid, companies.companyname, companies.companyoccupation, companies.country, companies.state, companies.city, companies.industry FROM users LEFT JOIN companies ON users.id=companies.companyid'; $r = mysql_query($q) or die(mysql_error()); while($r = mysql_fetch_assoc($q)) { but I still get this error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /home/www-data/mysite.com/fresh.php on line 27 that's because you're doing it wrong... try this instead: $query='SELECT * FROM users LEFT JOIN companies ON users.id=companies.companyid'; $result=mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result)>0){ while($row=mysql_fetch_assoc($result)){ print_r($row); $table[]=$row; } } print_r($table); Look at the source of the page after running this... I wrote a bit more, but your problem is that you did this: while($r = mysql_fetch_assoc($q)) should have been like this: while($row = mysql_fetch_assoc($r)) Link to comment https://forums.phpfreaks.com/topic/223269-query-is-wrong/#findComment-1154250 Share on other sites More sharing options...
unemployment Posted January 3, 2011 Author Share Posted January 3, 2011 try this: $q = 'SELECT users.id, users.username, users.firstname, users.lastname, users.accounttype, companies.companyid, companies.companyname, companies.companyoccupation, companies.country, companies.state, companies.city, companies.industry FROM users LEFT JOIN companies ON users.id=companies.companyid'; also, if that is all the fields, you could just use * instead of all the column names! EDIT: sorry, I did a typo, but edited it! I tried your suggestion. Code: $q = 'SELECT users.id, users.username, users.firstname, users.lastname, users.accounttype, companies.companyid, companies.companyname, companies.companyoccupation, companies.country, companies.state, companies.city, companies.industry FROM users LEFT JOIN companies ON users.id=companies.companyid'; $r = mysql_query($q) or die(mysql_error()); while($r = mysql_fetch_assoc($q)) { but I still get this error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /home/www-data/mysite.com/fresh.php on line 27 that's because you're doing it wrong... try this instead: $query='SELECT * FROM users LEFT JOIN companies ON users.id=companies.companyid'; $result=mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result)>0){ while($row=mysql_fetch_assoc($result)){ print_r($row); $table[]=$row; } } print_r($table); Look at the source of the page after running this... I wrote a bit more, but your problem is that you did this: while($r = mysql_fetch_assoc($q)) should have been like this: while($row = mysql_fetch_assoc($r)) Thank you! You fixed my error. Link to comment https://forums.phpfreaks.com/topic/223269-query-is-wrong/#findComment-1154253 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.