将Mysql转换为Mysqli

I was told to stop using MySQL and now learning MySQLi. However I ran into a problem using fetch array and don't know what I did wrong.

//Connect to db
include "mysqli_connect.php";

// Construct our join query
$sql = "SELECT userID, username, lastlogin FROM users";

//Crate results
$result = mysqli_query($link, $sql);

// Print out the contents of each row into a table 
$row = mysqli_fetch_array($result, MYSQLI_BOTH);

// Free result set
mysqli_free_result($result);

// Close connection
mysqli_close($link);


Messages I see:
  • [Connection Successful] Host info: db413417616.db.1and1.com via TCP/IP
  • Warning: mysqli_query() [function.mysqli-query]: Couldn't fetch mysqli in /homepages/9/d413002686/htdocs/maintenance/testsession.php on line 9
  • Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /homepages/9/d413002686/htdocs/maintenance/testsession.php on line 12
  • Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in /homepages/9/d413002686/htdocs/maintenance/testsession.php on line 15
  • Warning: mysqli_close() [function.mysqli-close]: Couldn't fetch mysqli in /homepages/9/d413002686/htdocs/maintenance/testsession.php on line 18 ()

I used this to for mysqli_connect.php: http://www.php.net/manual/en/mysqli.construct.php

<?php
// $link (host, username, password, database)
$link = mysqli_connect('host', 'username', 'password', 'database');

//If connection is successful, otherwise show error message. 
if (!$link) {
    die('[Connect Fail] Error: (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}
// Echo success message
echo '[Connection Successful] Host info: ' . mysqli_get_host_info($link) . "
";

// Close the link
mysqli_close($link);
?>

This line in your include file should be removed:

// Close the link
mysqli_close($link);

You're closing $link, a.k.a. your connection to the database, and then trying to use it in your query. You only want to close it when you're all done with it.