How to stop the script after an error

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

SimonEast
New php-forum User
New php-forum User
Posts: 3
Joined: Sun Dec 22, 2002 6:05 pm

How to stop the script after an error

Postby SimonEast » Sun Dec 22, 2002 6:09 pm

Is there a way I can tell the script to stop when an error is produced? I'm pretty sure ASP automatically stops unless you tell it not to. I have a series of SQL queries and I don't want them to continue if the first didn't complete.

I suppose I could test for an error after every query, but is there a simpler way??

Simon.

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Postby Joan Garnet » Mon Dec 23, 2002 5:29 am

You can use this exit()

Code: Select all

<?php
$filename = '/path/to/data-file';
$file = fopen ($filename, 'r')
    or exit("unable to open file ($filename)");
?>

or you can use trigger_error() -->

Code: Select all

mysql_connect("localhost", "user", "database", "pass") or trigger_error("Could not connect to MySQL database", E_USER_ERROR);

or you can use die() -->

Code: Select all

mysql_connect("localhost", "user", "database", "pass")or die("Connect Error: ".mysql_error());

Importatnt!
When you use die() after an open <html> tag, don't forget to use die like this : die("blabla\n\n</body>\n\n</html>") ; This will close the body and html tags.

bye
:D


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron