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 Quote Link to comment 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']; } } Quote Link to comment 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); Quote Link to comment 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? Quote Link to comment 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)) Quote Link to comment 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! Quote Link to comment 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 Quote Link to comment 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)) Quote Link to comment 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. Quote Link to comment 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.