如何从MySQL Query(多级数组)中获取变量?

时间:2022-09-25 16:30:11

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 =/.

但它不起作用(产生空白)。我确定我做错了什么,但我不知道我正在寻找的正式条款是什么,所以谷歌没有帮助= /。

3 个解决方案

#1


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.

代码未运行,因为您为mysql_fetch_array提供了有效的资源。 $ row也是一个单维数组。

$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'];

 }

#2


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.

好吧,我投票给PDO,但我很确定你可以跳过'c',因为$ row将引用字段名。 c仅由DBMS评估,以将字段与propper表相关联。

#3


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.

你应该在你的$ sql(sql查询)上调用mysql_query,然后再调用mysql_fetch_array来调用调用的结果。无论如何,你几乎总是应该调用mysql_error来检查错误。

#1


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.

代码未运行,因为您为mysql_fetch_array提供了有效的资源。 $ row也是一个单维数组。

$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'];

 }

#2


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.

好吧,我投票给PDO,但我很确定你可以跳过'c',因为$ row将引用字段名。 c仅由DBMS评估,以将字段与propper表相关联。

#3


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.

你应该在你的$ sql(sql查询)上调用mysql_query,然后再调用mysql_fetch_array来调用调用的结果。无论如何,你几乎总是应该调用mysql_error来检查错误。