0

I want to transfer data from a MySQL database, select the column related to the language of values from my goal.

MySQL table's name is catalogs Mysql column names are id, name_en, name_de, name_ru, content_en, content_de, content_ru, image

language short code comes from _GET request

For example, the URL looks like the following catalogs.php?id=1&lan=en

My PHP Code Looks like the following

$get_id = $_GET['id'];
$lan = $_GET['lan'];

$sql = $mysqli -> query("SELECT id, name_$lan, content_$lan, image FROM catalogs WHERE id='$get_id'");
while($row = $sql->fetch_array()){
   echo $row['name'];
 //nothing display
}
mega6382
  • 9,211
  • 17
  • 48
  • 69
axiinet
  • 11
  • 2
  • Because your '$get_id' is not evaluated correctly, try id=$get_id whithout the quotes – Loïc Di Benedetto Sep 18 '17 at 14:12
  • 1
    That doesn't scale very well; instead you should use one `name` and one `content` column and use for example your language code or an integer in an additional column to set the language of the record. – jeroen Sep 18 '17 at 14:12
  • 3
    Your code is dangerous and not secure. It is vulnerable to SQL injection, read up here and consider using PDO with bound parameters https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – delboy1978uk Sep 18 '17 at 14:12
  • 2
    Agreeing with last two comments. 1) Your schema is bad if you have to resort to variable field names. 2) This is a major SQL injection risk. Fixing number 1 (adding language as it's own field and having multiple records for each catalog id) will allow you to solve number 2 with parameterized mysqli/PDO logic. It might also fix the problem you are asking about. – JNevill Sep 18 '17 at 14:15
  • Thank you @delboy1978uk abbreviated code and I'm just trying to understand the logic – axiinet Sep 18 '17 at 14:50
  • @LoïcDiBenedetto my code is true no problem – axiinet Sep 18 '17 at 14:54
  • `var_dump($row)` and tell us what it gives you – delboy1978uk Sep 19 '17 at 10:40

0 Answers0