0

I've looked through StackOverflow for an answer to this and have so far come up empty handed. I know some have had a similar issue, but so far none of the responses to the issues have worked.

So I work on two sites, both of which are on the same host with the same PHP Admin set up (different accounts and domains, sites are not affiliated). One of them uses MySqli perfectly, without issues, while the other either gives a database selection failure, localhost access denied with password as "NO" or a blank page when I attempt to replace the MySql with MySqli.

The deprecated code:

$host = "LOCALHOST";
$usr = "USER";
$pwd = "PASSWORD";
$dbname = "DATABASE";
$connection = @mysql_connect($host,$usr,$pwd);

if(!$connection){ 
    die("No connection.");  
}
mysql_select_db($dbname,$connection);

The MySqli I am attempting (identical to the site that MySqli works perfectly on):

$host = "LOCALHOST";
$usr = "USER";
$pwd = "PASSWORD";
$dbname = "DATABASE";

$connection = mysqli_connect($host,$usr,$pwd);
if (!$connection) {
    die("No connection.");
}

$db_select = mysqli_select_db($connection, $dbname);
if (!$db_select) {
    die("Database selection failed: " . mysqli_error($connection));
}
Phil
  • 157,677
  • 23
  • 242
  • 245
  • 1
    Your database is on localhost, on server or are you trying to connect to another place ? fist you typed `localhost` then `host` – Edgarth Aug 30 '18 at 06:30
  • @Edgarth Local host, whoops. Let me edit that real quick. –  Aug 30 '18 at 06:33
  • What does phpinfo(); says? Is mysqli enabled on the website with blank page? Also you can set error_reporting(E_ALL); to see all errors. – AnTrakS Aug 30 '18 at 06:40
  • Please connect using dbname and port name which you have used Like mysqli_connect('host','user','pass','dbname','8889'); //8889 - whatever port you are using – Mehul Patel Aug 30 '18 at 06:49
  • A blank page almost certainly means you're not seeing the errors that are occurring. See [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php). Also add `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script for better MySQLi related errors – Phil Aug 30 '18 at 07:01
  • No, from what I can tell, error reporting does not work. I do not get any. It seems to be failing at Database selection. If I remove the error catching it will then state that the localhost has been denied access. I have not touched the database's name or any of the variables at the first 4 lines. –  Aug 30 '18 at 23:16
  • Phpinfo(); returns version 5+ –  Aug 30 '18 at 23:19

1 Answers1

0

So the PHP error codes did not work at all and it had nothing to do with the local host or port, so I first checked to make sure mysqli_connect was on. Next, I ran a simple test where I echoed a random word before each mysqli command.

echo 'Passed.';

global $c_mysqli;
$conn = new mysqli($host, $usr, $pwd, $dbname);

echo '<br>Passed global mysqli.';

I found that my second pass was not working, and then proceeded to do an error message. An error showed up then. However, nothing I did fixed the issue... then I realized that one of my files, the header file, is the meat and potatoes and if it were to fail then everything else would fail too.

Sure enough, one of my lines of code contained a mysql connection and not mysqli. Long story short, double check and triple check to make sure that all

$statement = mysql_query("STATEMENT");
$query = mysql_fetch_array($statement);

Appear as

$statement = $conn->query("STATEMENT");
$query = mysqli_fetch_array($statement);