-6

Possible Duplicate:
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in php
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

Im working with two MYSQL databases trying to gahter data from both of them to insert in to field for submission to another database. When i go and use the mysql_fetch_assoc() for the query it gives me an error. I did some research and I cannot find anything that really pertaied to my issue. I was wondering if i could get some help.

<?php
$connect = mysql_connect("localhost","***********","*********") or die ("Couldn't Connect"); //host,username,password
mysql_select_db("virtua15_jo151") or die ("Could not find database");


$query = mysql_query("SELECT jos_comprofiler.firstname, jos_comprofiler.lastname, jos_users.username, jos_comprofiler.cb_country, jos_users.email FROM jos_users LEFT JOIN jos_comprofiler ON jos_users.id=jos_comprofiler.id WHERE jos_users.id = " . mysql_real_escape_string($id));


if ($row = mysql_fetch_assoc($query))
{
    $firstname = $row['firstname']; //This is whatever the field is called in your table
    $lastname = $row['lastname'];
    $loginname = $row['jos_users.username'];
    $country = $row['cb_country'];
    $email = $row['email'];

}

?>

below this is all HTML I can include that if need be,

Community
  • 1
  • 1
David
  • 389
  • 5
  • 22
  • 1
    Your sql is wrong. Now this mistake can be anything.... You have to check table name, column name thoroughly. I advice you, in phpmyadmin, write just an initial portion of the query, see if it runs or not.... Then add 1 joint statement, if it runs, join another. In this way, you can spot the error even if you don't have clue where the error is at 1st. – itachi Mar 14 '12 at 06:48
  • 3
    Please, stop writing new code with `mysql_*`, and learn to use [PDO](http://php.net/pdo). – tereško Mar 14 '12 at 06:51
  • Whats that supposed to mean? Why can't I write my code how I want. Secondly I would like to know why I lost 4 reputation points because of this post. I'm here to ask questions on how to do things in regards to PHP and other things. I don't appreciate being penalized for asking questions that I don't know the answer to AFTER I did my research both here and on the internet and claimed that I did so in my posting. – David Mar 14 '12 at 08:50
  • It's not about "not writing the code you want". It's about asking question for some of your code: (1) your SQL is wrong (2) your code is ugly (`or die` my god, my god!!) (3) if you ask a question with bad SQL, with ugly code, and without trying to understand good suggestions (**PDO is the right and safe way to go**) then sorry we won't help you. – Olivier Pons Mar 14 '12 at 09:24
  • Please another suggestion: check for samples of mysql_xx: `while(list($firstname, $lastname, $loginname, $country, $email)=mysql_fetch_array($sql)) {}` is easier to both read and maintain. – Olivier Pons Mar 14 '12 at 09:26
  • David, [Google has nearly 7 million results for that error message](https://www.google.com/search?ix=seb&sourceid=chrome&client=ubuntu&channel=cs&ie=UTF-8&q=mysql_fetch_assoc()%3A+supplied+argument+is+not+a+valid+MySQL+result+resource). [StackOverflow has 5,000+](http://stackoverflow.com/search?q=mysql_fetch_assoc%28%29%3A+supplied+argument+is+not+a+valid+MySQL+result+resource). There's no way you researched this issue before posting as the entire Related column are duplicates as well.. Stackoverflow pointed this out when you entered that title... – Mike B Mar 14 '12 at 13:02

1 Answers1

0

Something is wrong with query check with print mysql_error();

Check query. quote properly values assigned in query.

$query = mysql_query("SELECT jos_comprofiler.firstname, jos_comprofiler.lastname,
         jos_users.username, jos_comprofiler.cb_country, jos_users.email 
         FROM jos_users LEFT JOIN jos_comprofiler ON jos_users.id=jos_comprofiler.id 
         WHERE jos_users.id ='".mysql_real_escape_string($id)."'");
  • 1
    `mysql_query` always returns a `resource`, even if no results were found. If it returns `false`, it means there was an error in the query. In that case, `mysql_num_rows` will complain about exactly the same thing. – deceze Mar 14 '12 at 06:42
  • Agree. Updated my answer –  Mar 14 '12 at 06:52