troubleshooting Question

MySQL SQLSTATE 00000 ?????

Avatar of Exceter
ExceterFlag for United States of America asked on
PHP
17 Comments1 Solution2904 ViewsLast Modified:
I'm thinking there must be something wrong with my error handling code because it is claiming there is an error and giving SQLSTATE 00000 as the error. As I understand things, SQLSTATE 00000 means that there were no errors. I'm using the functions to interact a MySQL database,

      function establish_mysqli_connection( $schema )
      {
            $connection_ip_address       = "127.0.0.1";
            $connection_username      = "root";
            $connection_password      = "";
      
            $mysqli = new mysqli( $connection_ip_address, $connection_username, $connection_password, $schema );

            if( mysqli_connect_errno() )
            {
                  printf( "<div style = \"text-align: left;\"><span style = \"color: red;\">Connection to MySQL server failed:<br />%s</span></div>", mysqli_connect_error() );
                  exit();
            }
            
            return $mysqli;
      }

      function execute_real_query( $schema, $sql )
      {
            $mysqli = establish_mysqli_connection( $schema );
            $mysqli->autocommit( FALSE );
            $mysqli->real_query( $sql );
            
            if( 0 != strcmp( $mysqli->sqlstate, "00000" ) )
            {
                  $mysqli->rollback();

                    echo "<div style = \"text-align: left;\"><span style = \"color: red;\">The MySQL server has encountered an error:<br />$mysqli->error<br /><br />Error - SQLSTATE: " . $mysqli->sqlstate  . "</span></div>";
                    echo "<br />The error occured in function: <b>execute_real_query</b><br />" . $_SERVER['SCRIPT_NAME'] . " <b>On Line: " . __LINE__ . "</b><br />" . $sql;
                  exit();
            }

            $mysqli->commit();
            $ins_id = $mysqli->insert_id;
            $mysqli->close();
            
            return $ins_id;
      }

      function execute_query( $schema, $sql )
      {
            $mysqli = establish_mysqli_connection( $schema );
            $mysqli->autocommit( FALSE );
            $result = $mysqli->query( $sql );
            
            if( 0 != strcmp( $mysqli->sqlstate, "00000" ) )
            {
                  $mysqli->rollback();
                  
                    echo "<div style = \"text-align: left;\"><span style = \"color: red;\">The MySQL server has encountered an error:<br />" . $mysqli->error. "<br />Error - SQLSTATE: " . $mysqli->sqlstate  . "</span></div>";
                    echo "The error occured in function: <b>execute_query</b><br />" . $_SERVER['SCRIPT_NAME'] . " <b>On Line: " . __LINE__ . "</b><br /><br />" . $sql;
                    exit();
            }

            $mysqli->commit();
            $mysqli->close();
            
            return $result;
      }

This is the output,
The MySQL server has encountered an error:


Error - SQLSTATE: 00000

The error occured in function: execute_real_query
/century/shipment_to_vendor_mod.php On Line: 107
INSERT INTO shipment_serial VALUES ( '', '7', '0' );

This is the invoking code,

$schema="myschema";

...

      if( 0 == strcmp( "Add", $_POST['add'] ) )
      {
            if( 0 == strcmp( "", $_POST['serial_number'] ) )
            {
                  $msg = "<span style = \"color: red; font-size: 24; font-weight: bold;\">Serial Number cannot be blank</span>";
            }

            else if( 0 == strcmp( "", $_POST['product_id'] ) )
            {
                  $msg = "<span style = \"color: red; font-size: 24; font-weight: bold;\">Please select a product ID</span>";
            }

            else
            {
                  $sql = "SELECT id FROM serial_numbers WHERE serial_number = '$_POST[serial_number]';";
                  $result = execute_query( $schema, $sql );

                  if( $result->num_rows == 0 )
                  {
                        mysqli_free_result( $result );
                        $sql = "INSERT INTO serial_numbers VALUES ( '', '$_POST[serial_number]', $_POST[product_id], 0 );";
                        $ins_id = execute_real_query( $schema, $sql );
                  }

                  else
                  {
                        $row = $result->fetch_object();
                        $ins_id = $row->id;
                  }                        

                  $sql = "SELECT * FROM shipment_serial WHERE ship_id = '$_REQUEST[id]' AND serial_id = '$ins_id';";
                  $result = execute_query( $schema, $sql );

                  if( 0 == $result->num_rows )
                  {
                        $sql = "INSERT INTO shipment_serial VALUES ( '', '$_REQUEST[id]', '$ins_id' );";
                        execute_real_query( $schema, $sql );
                        
                        header("Location: http://www.mywebsite.com/century/shipment_to_vendor_mod.php?id=$_REQUEST[id]");
                        exit();
                  }

                  else
                  {
                        $msg = "<span style = \"color: red; font-size: 24; font-weight: bold;\">Serial Number is already associated with this shipment</span>";
                  }

                  mysqli_free_result( $result );
            }
      }

      else if( 0 == strcmp( "Save", $_POST['save'] ) )
      {
            if( $_POST['ship_to'] == $_POST['ship_from'] )
            {
                  $msg = "<span style = \"color: red; font-size: 24; font-weight: bold;\">The TO and FROM fields cannot be equal</span>";
            }

            else if( "" == $_POST['reference_number'] )
            {
                  $msg = "<span style = \"color: red; font-size: 24; font-weight: bold;\">Shipment Number cannot be empty</span>";
            }

            else if( "" == $_POST['ship_desc'] )
            {
                  $msg = "<span style = \"color: red; font-size: 24; font-weight: bold;\">Description cannot be empty</span>";
            }

            else
            {
                  $sql = "
                        UPDATE shipments

                        SET       ship_to = '$_POST[ship_to]',
                              ship_desc = '$_POST[ship_desc]',
                              reference_number = '$_POST[reference_number]',
                              verified = $_POST[verified],
                              verified_by = $_COOKIE[auth_id],
                              verified_date = CURDATE(),
                              ship_type = 2

                        WHERE id = $_POST[id];";

                  execute_query( $schema, $sql );
                  mysqli_free_result( $result );

                  header("Location: http://www.mywebsite.com/century/shipment_to_vendor_mod.php?id=$_REQUEST[id]");
                  exit();
            }
      }

      $sql = "      SELECT *
                  FROM shipments
                  WHERE id = '$_REQUEST[id]'
                  AND ship_type = 2;";

      $result = execute_query( $schema, $sql );

      if( 0 == $result->num_rows )
      {
            mysqli_free_result( $result );
            header("Location: http://www.mywebsite.com/century/main.php");
            exit();
      }

      $row = $result->fetch_object();

      $tmp_arr_date = explode( " ", $row->ship_date );

      $_POST['reference_number'] = $row->reference_number;
      $_POST['ship_to'] = $row->ship_to;
      $_POST['ship_desc'] = $row->ship_desc;
      $_POST['id'] = $_REQUEST['id'];

      $tmp_arr_date = explode( " ", $row->ship_date );
      $_POST['verified_date'] = $row->verified_date;
      $_POST['verified'] = $row->verified;
      $_POST['verified_by'] = $row->verified_by;            

      $_POST['packed_by'] = $row->packed_by;
      $_POST['pack_date'] = $row->packed_date;

      mysqli_free_result( $result );

Note: I obfuscated the URLs

I'm hoping there's a simple explanation for this. I don't even care if I've made a mind numbingly stupid mistake. Actually, I hope I did. I just need it to work. If it keeps doing this to me I'm going to go home and attack it tomarrow with a clearer(less tired) head.

Cheers!
Exceter
ASKER CERTIFIED SOLUTION
TeRReF

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 17 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros