简体   繁体   中英

MySQL join results different in PHP?

I have a query:

SELECT * FROM categorys 
LEFT JOIN category_info ON categorys.cat_id=category_info.cat_id 
WHERE `cat_name` = 'aname'
ORDER BY `cat_order`

When I run this in phpMyAdmin I get an cat_id back regardless of if there is a match in the second table.

However, when I run this query in my PHP code I get a blank cat_id back, as shown by this print_r():

Array ( [cat_id] => [cat_name] => baths [type] => main [cat_order] => 99 [cat_img] => [display] => 1 [deleted] => 0 [desc_id] => [desc] => [text] => )

Why would there be a different result when the query is exactly the same?

EDIT: My PHP code:

$getcatidsql = "SELECT * FROM categorys 
LEFT JOIN category_info ON categorys.cat_id=category_info.cat_id 
WHERE `cat_name` = '{$cname}'
ORDER BY `cat_order";   
$getcatidresult = $db->query( $getcatidsql );
$catdata = $db->fetchRow( $getcatidresult );

function query() {
  $this->query_total++;
  if (func_num_args() == 1) {
    $sql = func_get_arg(0);
  } else {
    $args = func_get_args();
    for ($i=1;$i<count($args);$i++) if (!is_numeric($args[$i])) $args[$i] = '"'.mysql_real_escape_string($args[$i]).'"';
    $sql = vsprintf(array_shift($args),$args);
  }
  if ($result = mysql_query($sql,$this->db_connection)) {
    return $result;
  } else {
    $this->dberror( $this->db_connection, $sql );
  }
}

function fetchRow($result,$type=MYSQL_ASSOC)
{
    if($result)
        $row = mysql_fetch_array($result,$type);
    return $row;
}

I think you must not use select * (also because of same column names in both tables) , but select exactly fields for your needs

  select table_name.field, table_name.field2, other_table_name.field1 

.. and you'll get right results from both php code and phpmyadmin

I think it is because 'cat_id' is the same field name in both tables => the same index in result array. Try to modify query like : " Select categorys.cat_id AS category from categorys ..... "

You're SELECTing * from categorys and category_info . They both have a cat_id column. So you'll get two cat_id columns back.

The question is: what does the SQL library/driver you're using do when it encounters two columns with the same name in your SELECT list?

Looks like it's probably overwriting the first one it comes across with the second one... What happens if you use an explicit SELECT list, specifying categorys.cat_id and not bringing in category_info.cat_id ?

When you state

When I run this in phpMyAdmin I get an cat_id back

Do you mean that you get a VALUE for cat_id or do you get it as NULL? Because that is what you get with php, a NULL value for cat_id (as you see in print_r it still appears there).

If you don't want to get lines from categorys where there is no existing line in category_info for that category, then you have to use RIGHT JOIN instead of LEFT JOIN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM