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
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
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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.