I deduce the data from the database as follows:

$result = mysql_query('SELECT * FROM `users` WHERE id = '.$uid.''); while ( $users = mysql_fetch_array( $result ) ) { echo = ''.$users['first_name'].''; } 

I can use the $users['first_name'] variable only inside while {}

How to make it so that you can use it in any part of the page by writing

 <?=$users["first_name"]?> 

ps: yes, noob

pps: answer preferably deployed

  • one
    depricated This extension is deprecated since PHP 5.5.0 and will be removed in the future. Use MySQLi or PDO_MySQL instead. See also the MySQL instruction: API selection and the corresponding FAQ for more details. Alternatives to this function: mysqli_connect () PDO :: __ construct () - zb '

1 answer 1

Initialize (create / prepare) an array in advance (outside the while block).

Then fill this array with data from your sample (inside the while block).

Here also the global array with the necessary data.

  • Is it possible with a specific example? - KAFT
  • Specifically for your case, an example // Initialize the array $ array ['first_name'] = array (); $ result = mysql_query ('SELECT * FROM users WHERE id ='. $ uid. ''); while ($ users = mysql_fetch_array ($ result)) {// Fill $ array ['first_name'] $ array ['first_name'] [] = $ users ['first_name']; } it turned out an array $ array ['first_name'], which contains the values ​​of the field first_name from each row of the sample - Games
  • I try to display the value <? = $ Array ['first_name']?> - displays "Array" - KAFT
  • That's right, this is an array of data. I showed an example when many values ​​from the base will be selected and filled into an array. <? = $ array ['first_name'] [0]?> - (this is what should be output) the first element of the array (in your case the array will contain only one element) - Igrik
  • one
    If the result of the sample is one line, then you can equate to simple variables. But if the result of the sample is more than one row, then an array is needed. - Games