Pom Canys
Pom Canys

Reputation: 379

Getting information from a second MySQL table

In my code I would like to use a title attribute as you see in my code title="'. $title .'". For now $title ist not returning anything but it should return a value from the database. The title values are stored into another MySQL table (product_titles).

My goal would be to get the field value of the column "names" from the table "products". If the value of the field is the same as utf8_encode($row_Bereich2->name) then it should return the field value of the column "product_title".

if(!$result = $db->query("SELECT name FROM produkteguide_kategorien_alle_bereiche WHERE bereich = 2")){
         die('Error');
    }

    while($row_Bereich2 = $result->fetch_object()){

         $Select_Bereich2 .= '<input type="checkbox" id="'. utf8_encode($row_Bereich2->name) .'"><label for="'. utf8_encode($row_Bereich2->name) .'">'. utf8_encode($row_Bereich2->name) .'</label> <div title="'. $title .'" class="info_box">i</div><br>';
    }

How would I do this?

Upvotes: 0

Views: 41

Answers (2)

Joffrey Carle
Joffrey Carle

Reputation: 108

I suppose you are using PDO (I hope so, otherwise, use it).

Well, in your connection string, when you get connected to the DB with PDO you shall precise that you are using utf-8:

mysql:host=$host;dbname=$db;charset=utf8

By doing this, you will be able to avoid this kind of thing:

utf8_encode($row_Bereich2->name)

And to get an error directly from the database, you may have a look over here.

Upvotes: 0

Jiri Tousek
Jiri Tousek

Reputation: 12440

LEFT JOIN is what you're looking for.

SELECT kat.name, prod.product_title
FROM produkteguide_kategorien_alle_bereiche kat
    LEFT JOIN products prod ON (kat.name = prod.name)
    WHERE ...

Upvotes: 2

Related Questions