Mysqli trouble

+2 Gary Miller · November 17, 2014
Im pretty new to php and I've been using mysql_fetch_assoc because thats what Alex uses on the buckysroom php tutorials. I recently saw on that "This extension is deprecated as of PHP 5.5.0, and will be removed in the future. " So now I need to use mysqli_fetch_assoc but i don't understand how to set it up. Before all you had to do was mysql_fetch_assoc(mysql_query( $query). If somebody could explain how to do this with the mysqli functions that would be great. 

I have a function:

function show_coaches($school_id) {
$data = mysql_fetch_assoc(mysql_query("SELECT `first_name` FROM `users` WHERE `school_id` = '$school_id'")));
foreach($data as $value) {
echo $value;

Im trying to echo out all the first names of coaches that have the same school id.
Also this only echoes one coach but I have multiple coaches with the same id. How can i fix that?

Post a Reply


Oldest  Newest  Rating
0 Jonah Morrison · November 18, 2014
Is it supposed to be mysqli? Because you have out mysql.
0 Jonah Morrison · November 18, 2014
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);

    /* free result set */

/* close connection */

Something like this? This is right from the PHP manual on the PHP website.

0 Gary Miller · November 18, 2014
Thank you for your time and I looked on the php site i was just confused but this video helped out a lot.
I figured it out but do you know what the purpose of free result is? And is it necessary to close connection. If you don't know thats alright. Ill look it up.  
0 Jonah Morrison · November 18, 2014
Free results will free up memory. Shouldn't be an issue with smaller websites. But big result sets take up alot of memory. And same thing with closing the connection. Its just house keeping.

  • 1



Server-side, HTML embedded scripting language used to create dynamic Web pages.