Jump to content

Query is wrong


unemployment

Recommended Posts

$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

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

$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

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

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! :P

 

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

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! :P

 

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

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! :P

 

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

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! :P

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.