0
function f_sqlConnect($user, $pass, $db) {
$link = mysqli_connect('127.0.0.1', $user, $pass);

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysqli_select_db($db, $link);

if (!$db_selected) {
    die('Can\'t use ' . $db . ': ' . mysql_error());
}
}

I am getting errors on this and when I remove $link I get another error

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
K.F
  • 1
  • 1
    Reverse your arguments. The $link comes first. http://php.net/manual/en/mysqli.select-db.php But in MySQLi, you can pass `$db` as the fourth argument to `mysqli_connect()` so you may as well place it there as `mysqli_connect('127.0.0.1', $user, $pass, $db)` and avoid `mysqli_select_db()` entirely. – Michael Berkowski Oct 12 '15 at 17:12
  • Thank you the problem is I am trying to /*Cleans an array to protect against injection attacks.*/ function f_clean($array) { return array_map('mysqli_real_escape_string', $array); } /*Connects to and selects the specified database with the specified user.*/ function f_sqlConnect($user, $pass, $db) { $link = mysqli_connect('127.0.0.1', $user, $pass); if (!$link) { die('Could not connect: ' . mysql_error()); } $db_selected = mysqli_select_db($db, $link); if (!$db_selected) { die('Can\'t use ' . $db . ': ' . mysql_error()); } } – K.F Oct 12 '15 at 17:19
  • You are also mixing `mysql_error()` in the MySQLi API. The two are not compatible. You need to be using `mysqli_connect_error()` for the first one, and `mysqli_error($link)` for the one after selecting the DB. If you want to continue separating the db selection action, you must _reverse the arguments_ as `mysqli_select_db($link, $db);` – Michael Berkowski Oct 12 '15 at 17:22
  • Thank you Michael I have changed it now I am getting Parse error: syntax error, unexpected 'mysqli_connect_error' (T_STRING – K.F Oct 12 '15 at 17:26
  • That suggests you did not call it as a function: `die('Could not connect: ' . mysqli_connect_error());` And after setting the database `die('Can\'t use ' . $db . ': ' . mysqli_error($link));` – Michael Berkowski Oct 12 '15 at 17:29
  • Thank you very much I am pretty new at this I am trying to understand the not call to function. I apologized in advance. – K.F Oct 12 '15 at 17:34

0 Answers0