Re: New years resolution: To get serious with my programming! Anyone wanna help? :)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Jan 8, 2008, at 1:23 PM, Jochem Maas wrote:

Jason Pruim schreef:
Hi Everyone,

Happy New Year a week late! :)

I am trying to get more serious with my programming, I feel fairly
confident in my basic abilities except for one... Error checking. That's
what I'm trying to get figured out :)

I have a script, that I am using to connect to my database, read,
insert, delete or edit the records in there.

most of the time the script works perfectly, but on the occassion it
doesn't like when jupiters third moon aligns with uranus, I want the
user to be notified to take their head out of their ass... :)

What I have tried is this:

$querytest = "INSERT INTO current VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
if ($stmt = mysqli_prepare($link, $querytest)) {


   mysqli_stmt_bind_param($stmt, 'ssssssssss', $FName, $LName, $Add1,
$Add2, $City, $State, $Zip, $XCode, $Record, $Reason);

it's possible that the binding fails. check the return value of
mysqli_stmt_bind_param() and if an error status is returned log the error
and don't try to execute.

   //Add the record
   mysqli_stmt_execute($stmt);

again check the return value of the function you called (you beginning
to see a pattern here with regard to error checking? ;-)

you only need to print out (or log) an error if one actually occurred.
additionally if mysqli_stmt_execute() returns an error code you can output a nice userfriendly message log the cryptic mysql error message, etc somewhere.

e.g.

if (!mysqli_stmt_execute($stmt)) {
	echo "SOMETHING BAD HAPPENED!";
	error_log(mysqli_stmt_errno($stmt));
}

hope you get the idea.

Yeah I think I get the idea... Now I just need to figure out to do it for sure :) this is what I have come up with so far:

$check = mysqli_stmt_error($stmt);
echo "<BR>$checkdate<BR>";
if($check ==""){
	echo "Success! Redirect to first page";
	printf("%d Row Inserted.\n", mysqli_stmt_affected_rows($stmt));
	header("Location: index.php");
	}
	else
	{
	echo "
Sorry for the inconvience, but something appears to be not working correctly. <BR> Please take a quick moment to send an e-mail to: <a href=\"mailto:webmaster@xxxxxxxxxx \">webmaster@xxxxxxxxxx</A> to report the error.<BR>";
	echo "
		Please include:<BR>
		What browser you are using.<br>
		any text displayed on this page.<br>";
	printf("Error: %d.\n", mysqli_stmt_error($stmt));
	echo $checkdate;
	
}

//Close the statement
if(!$stmt){

	mysqli_stmt_close($stmt);
}

I'm sure I could do it better, and I'll be studying to figure out how very shortly!

Now I just need to make the query fail, but still connect to the database to test it :)




   printf("Error: %d.\n", mysqli_stmt_errno($stmt));
   printf("%d Row Inserted.\n", mysqli_stmt_affected_rows($stmt));

you might want to output the actual error message (often more useful than a number) and also output the values you we're trying to submit to the DB.

lastly consider logging to a file (e.g. error_log()) and log enough so that you build up a store of error data that you can use to help you track problems
that are apparently cropping up occasionally



}

//Close the statement
mysqli_stmt_close($stmt);

you should only close the statement if it was actually prepared okay in the first place

at it's most simple:

if ($stmt)
	mysqli_stmt_close($stmt);



that was pulled off of the php.net site (For the most part) and adapted
slightly to meet my needs, and obviously I edited too much of it :)

If anyone has any ideas I would appreciate it. Even RTFM as long as $M
is defined :)



--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@xxxxxxxxxx




--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@xxxxxxxxxx

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux