1

I have the following:

$sql = "SELECT c.category_name
         , c.category_name_url 
    FROM blog_categories AS c 
      JOIN blog_articles AS a
        ON a.category_name = c.category_name
    WHERE c.category_status = 'online'
    GROUP BY c.category_name
    ";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)) {

$category_name = $row['c']['category_name'];
$category_name_url = $row['c']['category_name_url'];

}

But it's not working (generating blanks). I'm sure I'm doing something wrong, but I don't know what the formal terms of what I'm looking for is so Google is no help =/.

2

3 Answers 3

3

code was not running because you dint supply a valid resource for the mysql_fetch_array. also $row will be a single dimensional array.

$sql = mysql_query("SELECT c.category_name
     , c.category_name_url 
FROM blog_categories AS c 
  JOIN blog_articles AS a
    ON a.category_name = c.category_name
WHERE c.category_status = 'online'
GROUP BY c.category_name
");

 while($row = mysql_fetch_array($sql)) {

 $category_name = $row['category_name'];
 $category_name_url = $row['category_name_url'];

 }
1
$category_name = $row['category_name'];

Well, I vote for PDO, but I am pretty sure you can skip the 'c', as $row will refer to the fieldname. The c is just evaluated by the DBMS to associate the field with the propper table.

1
  • Also by using mysql_fetch_array() you can use indices. Ex: $row[0] for category_name, $row[1] for category_name_url etc. Commented Jul 14, 2011 at 13:09
0

You should call mysql_query on your $sql (the sql query), you then call mysql_fetch_array againts the result of the call. Anyway, you should almost always call mysql_error to check for errors.

1
  • Ah, actually I did do that (I just didn't add that line in there) - edited for clarification :) Commented Jul 14, 2011 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.