John Doe John Doe - 2 months ago 15
MySQL Question

mysqli_select_db() expects exactly 2 parameters, 1 given

Attempting to solve this issue. I'm trying to restore a extremely old version of Wordpress, back when it was known as B2, into a working state. After making a bunch of edits to the code in a attempt to get it working, I'm now getting this:


Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /home/Redacted/public_html/b2-include/b2functions.php on line 31


Here's the segment of the code that's having issues:

<?php

/* functions... */

function get_currentuserinfo() { // a bit like get_userdata(), on steroids
global $HTTP_COOKIE_VARS,$user_login,$userdata,$user_level,$user_ID,$user_nickname,$user_email,$user_url,$user_pass_md5;
// *** retrieving user's data from cookies and db - no spoofing
$user_login = $HTTP_COOKIE_VARS["cafeloguser"];
$userdata = get_userdatabylogin($user_login);
$user_level = $userdata["user_level"];
$user_ID=$userdata["ID"];
$user_nickname=$userdata["user_nickname"];
$user_email=$userdata["user_email"];
$user_url=$userdata["user_url"];
$user_pass_md5=md5($userdata["user_pass"]);
$pref_usequicktags=$userdata["pref_usequicktags"];
$pref_postnavigator=$userdata["pref_postnavigator"];
$pref_showinactiveusers=$userdata["pref_showinactiveusers"];
$pref_textarearows=$userdata["pref_textarearows"];
$pref_confirm=$userdata["pref_confirm"];
$pref_usespellchecker=$userdata["pref_usespellchecker"];
// *** /retrieving
}


function dbconnect() {
global $connexion, $server, $loginsql, $passsql, $base;

$connexion = mysqli_connect($server,$loginsql,$passsql) or die("Couldn't connect! So sad :( <p>You should look into this!</p>");

$connexionbase = mysqli_select_db($base) or die("Couldn't connect! So sad :( <p>You should look into this!</p>");

return(($connexion && $connexionbase));
}


I am getting the error when I load the site at all. I can provide more code if necessary.

Answer

Posting as a community wiki. I don't want rep from this and there shouldn't.

$connexionbase = mysqli_select_db($base)

Just as the error states. You need to pass the db connection as the first argument:

$connexionbase = mysqli_select_db($connexion, $base)

Reference:

Example from the manual:

bool mysqli_select_db ( mysqli $link , string $dbname )

Sidenote:

return(($connexion && $connexionbase));

TBH, I've never seen this type of syntax for a return. Far as I know, you can return only once or using an array.

Pulled from this answer http://stackoverflow.com/a/3815243/1415724

You can only return one value. But you can use an array that itself contains the other two values: return array($uid, $sid);

Instead of going through all that trouble, just use the 4 parameters:

$connexion = mysqli_connect($server,$loginsql,$passsql, $base)

as per the manual:

then return with and if it's really needed.

return $connexion;

Plus, why are you intending to use MD5 to store passwords with? That hashing function is no longer considered safe to use.

You're better off using password_hash().

This is the 21st century after all.

and $HTTP_COOKIE_VARS, that's deprecated.

I've no idea why you're using that code or where you got it from.

Comments