Friends, help me figure it out.
There is a database called NAME. It has 2 columns: name1 and name2 .
I need to output the value of the column name2 , provided that in this line name1 = $SuperName=$_REQUEST['name'] .
$_REQUEST['name'] is transferred correctly (I try to select a string and put it in $ data).

Next is my code:

 $Подключение к БД $SuperName=$_REQUEST['name']; $sql= mysql_query("SELECT * FROM NAME WHERE name1='$SuperName' ",$db); if($sql) { while($data = mysql_fetch_array($sql)) { echo $data['name2']; } } else { print mysql_error(); } 

The output writes an error that Name2 is not defined. Perhaps this question is unrealistically simple, but I cannot understand what is wrong. Honestly read similar questions. Dumb, I did not understand what is wrong :(

  • var_dump($data);die; In the inside of the while loop what does it write? - Naumov
  • 2
    replace mysql_fetch_array with mysql_fetch_assoc - EatMyDust
  • Thanks, EatMyDust! This solved the problem. If anyone encounters such a problem with a friend, then you need to output not by the column name, but by the array number, for example echo $ data [1]; - Al3x4kov
  • @ Al3x4kov, read this question / answer ru.stackoverflow.com/questions/393450/… and this ru.stackoverflow.com/questions/448720/… - Visman

1 answer 1

Replace mysql_fetch_array with mysql_fetch_assoc as in the first case you will receive a list (array with indices 0,1,2,3), and in the second case an associative array (array with keys name1, name2)