Display different results if no entry is present

I’ve got a database query that gets symbols from a database and if they’re not there adds the word ‘NO’ to the result.

What I want to do is change it so that if it’s not there is adds a value to at column but I can’t work out how to do it.

This is what I’ve got

 $query="select * from symbols where visible=1 order by name";
  $result=dbselect( $query,"db" );
  if( is_array( $result ) ) {
    foreach( $result as $key=>$value ) {
      $symbols[$value['id']]['name']=$value['name'];
      $symbols[$value['id']]['character']=$value['character'];
      $symbols[$value['id']]['color']=$value['color'];
      $symbols[$value['id']]['image1']=$value['image'];
      $symbols[$value['id']]['image2']=$value['image2'];
      $symbols[$value['id']]['image']=$value['image2'];
      $symbols[$value['id']]['text']="NOT ".$value['name'];
      $symbols[$value['id']]['color2']="#4e4e4e"; // This is what I want to add
    }
  }
  $query="select symbols.id from symbols,symbols_map where symbol_id=symbols.id and visible=1 and id=".$id." order by name";
  $result=dbselect( $query,"db" );
  if( is_array( $result ) ) {
    foreach( $result as $key=>$value ) {
      $symbols[$value['id']]['image']=$symbols[$value['id']]['image1'];
      $symbols[$value['id']]['text']=$symbols[$value['id']]['name'];
      $symbols[$value['id']]['color']=$symbols[$value['id']]['corolla']; // This works but the complete other way round - it added the color if it's there instead.
    }
  }

with PDO you can get the $symbol array without an extra loop by using the PDO::FETCH_GROUP option.

the second array can be done quite similarly if you use a LEFT/RIGHT JOIN instead of a CROSS JOIN (and the help of the IFNULL() function).

// little bit shortened––too lazy to type everything
$stmt = $pdo->query('SELECT id, name, "#4e4e4e" AS color FROM symbols');
$symbols = $stmt->fetchAll(PDO::FETCH_GROUP);

Sorry to be dumb but is PDO a type of database? It’s a MySQL database, does that make a difference?

No, PDO is a different method of accessing the MySQL (or other type of) database. So you have the old-style mysql() calls, the mysqli() calls, or PDO calls - just different functions to get to the same data.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.