MySQL Forums
Forum List  »  PHP

Re: PHP/MySQL Reading Database Field Help!
Posted by: Peter Brawley
Date: July 22, 2019 04:49PM

Why write code to hide errors from yourself?

if (mysqli_connect_error()) { 
  echo "Failed to connect to the database!"; 
} 
...

$stream = mysqli_query($conn,$sql); 
echo "<br>"; 
echo "stream = " $stream; 
$stream1 = mysqli_fetch_field($stream); 
echo "<br>"; 
echo "streamID = " $stream1;

In development, in testing, and in production, all code that can elicit errors needs error trapping, minimally something like ...

$debug = TRUE;
$err = "";
ini_set( "log_errors", 1 );
ini_set( "error_log", "path_to_your_error_file" );  
ini_set( "display_errors", $debug );

$conn = mysqli_connect(...);
if( mysqli_connect_error() ) { 
  $err = mysqli_connect_error(); 
  if( $debug ) exit( "Error: $err" );
  logerr( $err );  
  exit( "Failed to connect to the database." ); 
} 

...

$stream = mysqli_query($conn,$sql); 
if( !$stream ( {
  $err = mysqli_error( $conn ); 
  logerr( $err );  
  if( $debug ) echo "Error: $err<br>" );
  ...
}

...

$stream1 = mysqli_fetch_field($stream); 
if( !$stream ( {
  ... etc ...
}

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.