Converting mysql_fetch_array results to multidimensional array

<?php // Connecting to Database $link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die(mysql_error()); echo 'Connected successfully'; mysql_select_db('my_database') or die('Connection failed'); // Query $query = 'SELECT * FROM users'; $result = mysql_query($query) or die('Query failed: ' . mysql_error()); // Converting results to multidimensional array for ($i = 0; $i < mysql_num_rows($result); $i++) { $multiarray[$i] = mysql_fetch_array($result); } /* Working with $multiarray * Now, I can see phone number of a user number 2 (for example) * Such as: */ echo $multiarray[2]['phone']; // Close connection mysql_free_result($result); mysql_close($link); ?>
Enjoy!

1 Response

mysql extension has been deprecated as of PHP 5.5.... and you do know that you can access the array by both key and index values correct? I don't see what your code accomplishes that a simple while($row = $sql_result_set->fetch_assoc()) {$array[] = $row;} doesn't do already?

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.