fetch problem - need help

I try to fetch some query from my mysql db and have trouble.
The connection is succssfully work to the DB.
Than when i try to run this code in phpmyadmin so the results are correct.
This is query i try to fetch:

[php]SELECT cat.name as category, scat.name as subcategory
FROM cat INNER JOIN scat
WHERE cat.id = scat.target[/php]

And this is the problem come after:

[php]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource[/php]

The error comes from the line where i fetch the varieble
This is all the code:

[php]$result = mysql_query(“SELECT cat.name as category, scat.name as subcategory
FROM cat INNER JOIN scat
WHERE cat.id = scat.target”);
$myrow = mysql_fetch_array($result);[/php]

The join is wrong. You have to tell it what fields to join the tables on
[php]
$result = mysql_query(“SELECT ca.name category, sc.name subcategory
FROM cat ca
INNER JOIN scat sc ON(ca.id = sc.id)
WHERE cat.id = scat.target”) or die(mysql_error());

if(mysql_num_rows($result) != 0) {
while($myrow = mysql_fetch_array($result)) {
// more code
}
} else {
$myrow = “No results”;
}[/php]

I try this one like you said:

[php]$result = mysql_query("
SELECT cat.name as category, scat.name as subcategory
FROM cat
INNER JOIN scat ON(cat.id = scat.target)
WHERE cat.id = scat.target
");
$myrow = mysql_fetch_array($reuslt);[/php]

Still same problem: " mysql_fetch_array(): supplied argument is not a valid MySQL result resource in"
The problem comes from line fetch array
in sql query in mysql php admin every thing works nice
Don’t know what the problem.

That’s not what i put up though and your join clause is the same as the where clause. typically the where clause would be something that matches the first table

The problem is i need to fetch category with sub category:
I have at the table:
table “categories”
id 1 name: cars

table “sub categories”
name: private, target= 1
name: van, target= 2

I need fetch for each category the sub one for the category and its not fetch
Have ideas maybe?

Are there any common fields in both tables?

and that cat has to be cat cat, otherwise cat.name means nothing and you really don’t need to have as in there, its an automatic thing.

Thanks i will try it

Thanks you very much richei
The problem resloved.
The problem was at hosting provider at the end ;(

What had to be changed on their end?

Sponsor our Newsletter | Privacy Policy | Terms of Service