0

mysql_connect will give me an error report. mysqli_connect gives me a blank page.

This does gives me an error report

$mysql_con = mysql_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_table); 
if(!$mysql_con) {
   die("Some error occurred during connection " . mysql_error($mysql_con));
}

This does not give me an error report:

$mysql_con = mysqli_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_table); 
if(!$mysql_con) {
   die("Some error occurred during connection " . mysqli_error($mysql_con));
}

Nor does this

$mysql_con = mysql_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_table) 
or die("Some error occurred during connection " . mysqli_error($mysql_con));

I have tried both and same thing: Blank white page

error_reporting(E_ALL);
error_reporting(-1);

When I say blank white page I mean nothing at all is displayed. None of my HTML or css or anything. It completely stops the script. mysql_connect seems to function and loads the page fully even with an error.

When do do this locally (localhost), mysqli_connect works just fine and gives an error when needed. The DB im connecting to is the same one, the only difference is in the above script Im letting it execute from a remote server to access my db on my local system. Even if it cant access it for whatever reason, why would it give me a blank screen?


Edit

Its magically working and giving me errors properly now using the following code.

$con = mysqli_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_db);
if(mysqli_connect_errno()) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Paul Duncan
  • 302
  • 1
  • 5
  • 19
  • 1
    Is `display_errors` in `php.ini` set to "on"? That could also have an added effect. Try adding `ini_set('display_errors', 1);` while making sure `display_errors` in `php.ini` is set to "on". – Funk Forty Niner Jun 19 '14 at 20:09
  • 1
    FYI: Fourth parameter is the database name ***not*** a table name. Reference: [`mysqli_connect()`](http://www.php.net/manual/en/mysqli.construct.php)... – War10ck Jun 19 '14 at 20:15
  • @ Fred -ii- wouldn't they be set to on if `mysql_connect` spits out an error? Or do they have separate configurations? Ill check this when I can. @War10ck yes I know, I'm trying to get errors to display though. – Paul Duncan Jun 19 '14 at 20:31
  • @PaulDuncan I couldn't 100% sure about that. MySQL and PHP both have their own way of showing errors. I saw a question last week where it was a similar problem and now, for the life of me, I can't remember what the OP did to rectify the problem. I think it had something to do with the output buffer being set too low. – Funk Forty Niner Jun 19 '14 at 20:41
  • Im unable to get it checked on the remote server till later. Right now, im wondering why `error_reporting(E_ALL);` has no effect on any of this? I have it above all the code. Whats the point of it it I cant force errors to display with it? Or does it not take priority over server configs :/ – Paul Duncan Jun 19 '14 at 20:41
  • Does this answer your question? [Should we ever check for mysqli\_connect() errors manually?](https://stackoverflow.com/questions/58808332/should-we-ever-check-for-mysqli-connect-errors-manually) – Dharman Jun 24 '20 at 21:06

1 Answers1

2

Instead of appending mysqli_error to the die statement, check for an error after the connection like so:

$mysql_con = mysqli_connect($mysql_host, $mysql_user, $mysql_pass, $mysql_table); 
if($mysql_con->connect_error) {
   die($mysql_con->connect_error);
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Kodlee Yin
  • 1,089
  • 5
  • 10
  • `::` is only used to access static properties. You need to use `->` to access `connect_error`. – gen_Eric Jun 19 '14 at 20:09
  • Using this with `mysqli_connect` results in no error even when I give it the wrong password. The page loads (no blank screen) but no error report. Same with with `mysql_connect`. – Paul Duncan Jun 19 '14 at 20:29
  • Im thinking the remote server has errors set to not display, as suggested by Fred -ii-. However Ill keep this error display method in mind thanks – Paul Duncan Jun 19 '14 at 20:34
  • Sadly for whatever reason this cant even be used right now, as Im manually having to display an echo as an error message `if($mysql_con->connect_error) {echo "UGH";}` and it will not display `UGH`. Using `if(!$mysql_con)` will give me a manual error message however. *toss hands in air* – Paul Duncan Jun 19 '14 at 20:45