MySQL Forums
Forum List  »  PHP

Re: trying to insert values into my table for first time
Posted by: Peter Brawley
Date: March 19, 2021 11:20PM

> I am guessing it is an issue with the php install.

?! The issue is most often what the programmer has written.

A simple example of polling for errors ...

<?php

$conn = mysqli_connect( ... ) 
        or exit( mysqli_connect_error() ;
$sql = "select * from test where id > ?";
$ival = 3;
$ctypes = "i";   // mysqli_stmt_bind_param() wants this param by ref
$stmt = mysqli_stmt_init( $conn );
if( !mysqli_stmt_prepare($stmt,$sql) ) {
  exit( $conn->error );
} 
elseif( !mysqli_stmt_bind_param($stmt,$ctypes,$ival) ) {
  exit( $conn->error );
} 
elseif( !mysqli_stmt_execute($stmt) ) {
  exit( $conn->error );
}   
elseif( $res=mysqli_stmt_get_result($stmt) ) {
  while( $row=mysqli_fetch_row($res) ) {
    print_r($row);
  }
}
else {
  exit( $conn->error );
}

?>

Once you've played around with this snippet, weeing what happens when you create deliberate errors &c, read about and try out other error handling methods eg try/catch.



Edited 1 time(s). Last edit at 03/20/2021 10:00AM by Peter Brawley.

Options: ReplyQuote




Sorry, only registered users may post in this forum.

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.