Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 01-12-2008, 03:29 PM "Or Die" function
Mattmaul1992's Avatar
Ultra Talker

Posts: 486
Name: Matt
Trades: -1
Ok.. So I've been developing an SQL app with PHP. It's basically just for those poor people who can't get access to a MySQL database or are looking for an SQL system with less complication for faster results for applications such as chat systems (Basically the speed difference is how long it takes for a query to run compared to editing a file... HUGE difference).
My problem is I've been developing the error reporting and it works pretty much exactly like MySQLs error reporting. It's just a function in which a value is returned that contains the error. The only problem is getting that error to appear.
In MySQL to connect and have the error show if one is encountered you might do it like this -
PHP Code:
$connection mysql_connect('host''user''pass') or die ('Unable to connect to database because: ' mysql_error()); 
This is how I want it to work for my app. The only problem is there's a problem with the "or die" part of the function. I figured or die means if the function returns false then stop script execution and show this message but it dies even when the function returns true. So does anyone know EXACTLY how or die works so I can make this work?
O also this is how the functions I'm using look like -
Call to function -
PHP Code:
require ('./functions.php');
$txtsql = new txtSQLfunctions;
$txtsql->connect('/mmmsoftware/projects/textSQL/db_1''Matt''pass') or die ($txtsql->latest_error()); 
Function being called -
PHP Code:
    // Database connection handler
    
function connect($db_location$db_user$db_pass)
    {
    
// Clean up info given...
    
$db_user strtolower($db_user);
    
$db_loc_last_char substr($db_locationstrlen($db_location) - 1strlen($db_location));
        if (
$db_loc_last_char != '/' && $db_loc_last_char != '\\')
        {
        
$db_location .= '/';
        }
        if (
substr($db_location07) != 'http://' && substr($db_location01) != '/' && substr($db_location02) != './')
        {
        
$db_location 'http://' $db_location;
        }
        
// Already connected to another DB?
        
if (isset($this->connection_info['db_location']) && $this->connection_info['db_location'] != $db_location)
        {
        
$this->latest_error "<b>TextSQL <i><font color='red'>beta</font></i> Error:</b> Already connected to another database. Please close first connection before opening a new one. Support for multiple connections will come in a later version.";
        }
        if (
$this->connection_info['db_location'] == $db_location && $this->connection_info['db_user'] == $db_user && $this->connection_info['db_pass'] == $db_pass)
        {
        
// Lol dude you're already connected to that db. Just re-use past connection info.
        
$this->latest_error .= "<br />NO FATAL ERRORS REPORTED";
        return (
true);
        }
        else
        {
            if (
is_file($db_location 'txtsql_db_info.php.inc'))
            {
            require(
$db_location 'txtsql_db_info.php.inc');
                if (isset(
$USERS[$db_user]))
                {
                
$pass_perm explode('-|*|-'$USERS[$db_user]);
                    if (
$pass_perm[0] == $db_pass)
                    {
                    
// Connection info valid!!!
                    
                    
}
                    else
                    {
                    
// Invalid password
                    
$this->latest_error "<b>TextSQL <i><font color='red'>beta</font></i> Error:</b> Invalid password for user given. Connection failed.";
                    
$this->connection_info = array();
                    return (
false);    
                    }
                }
                else
                {
                
$this->latest_error "<b>TextSQL <i><font color='red'>beta</font></i> Error:</b> The user you attempted to use does not exist.";
                
$this->connection_info = array();
                return (
false);    
                }
            }
            else
            {
            
$this->latest_error "<b>TextSQL <i><font color='red'>beta</font></i> Error:</b> Database files do not exist in specified directory or the databases info file was destroyed.";
            
$this->connection_info = array();
            return (
false);    
            }
        }
    } 
Error function -
PHP Code:
    function latest_error()
    {
    return (
"test " $this->latest_error);
    } 
Thanks a lot .
__________________
PHP Code:
$talkupation++; 

Please login or register to view this content. Registration is FREE
- Free IPB forum hosting (releasing today!!!), no ads, free modifications

Last edited by Mattmaul1992; 01-12-2008 at 03:33 PM..
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-13-2008, 10:25 AM Re: "Or Die" function
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
Trades: 1
i would sugest to not use die instead do something like
Code:
require ('./functions.php');
$txtsql = new txtSQLfunctions;
$txtsql->connect('/mmmsoftware/projects/textSQL/db_1', 'Matt', 'pass');  
if ($txtsql->error===true){
   echo "The page has encountered this SQL error : ",$txtsql->latest_error();
}
__________________
If you like my posts ... TK is appreciated:)

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 01-13-2008, 12:12 PM Re: "Or Die" function
Mattmaul1992's Avatar
Ultra Talker

Posts: 486
Name: Matt
Trades: -1
Yer I was going to do that but I've just been set on or die because it's less code if anyone ever ends up using this script and it sort of makes me mad when things like this don't work and I have no way to fix them lol. Thanks anyways.
__________________
PHP Code:
$talkupation++; 

Please login or register to view this content. Registration is FREE
- Free IPB forum hosting (releasing today!!!), no ads, free modifications
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 01-13-2008, 12:30 PM Re: "Or Die" function
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Just my opinion here, and please don't take offense, but the or die stuff bothers me. It just smacks of the BSOD -- stops everything and displays a usually un-useful messge to the end user. I think scripts should log the error and redirect the user to a nice error page.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 01-13-2008, 07:27 PM Re: "Or Die" function
Mattmaul1992's Avatar
Ultra Talker

Posts: 486
Name: Matt
Trades: -1
Well.. Logging really isn't up to me. I do however plan on setting up an array developers can use to log all warnings and errors but once again that's really something people that develop with this need to do themselves. I guess it would be a good idea to log all errors in the server DB but I'm not sure everyone would want that. Also I personally think redirecting to a nice error page is a waste of time.. I want to display the error. Straight and to the point. No point making a whole new page to display an error message.
__________________
PHP Code:
$talkupation++; 

Please login or register to view this content. Registration is FREE
- Free IPB forum hosting (releasing today!!!), no ads, free modifications
Mattmaul1992 is offline
Reply With Quote
View Public Profile
 
Old 01-14-2008, 11:38 PM Re: "Or Die" function
phpknowhow's Avatar
Skilled Talker

Posts: 83
Name: Colin
Location: USA
Trades: 0
Here is my take on your problem:

Since the function returns false on an error why not just do this:
PHP Code:
require ('./functions.php');
$txtsql = new txtSQLfunctions;
if(!
$txtsql->connect('/mmmsoftware/projects/textSQL/db_1''Matt''pass')) {
echo 
$txtsql->latest_error(); die(); } 
or

PHP Code:
require ('./functions.php');
$txtsql = new txtSQLfunctions;
try {

if(!
$txtsql->connect('/mmmsoftware/projects/textSQL/db_1''Matt''pass'))
throw new 
Exception($txtsql->latest_error());

} catch(
Exception $e) {
echo 
$e->getMessage(); die();

What I did with my MySQL database, is I created a custom Exception that uses error_log to send the error to my email or log it in a file. I prefer using try / catch.

Hope that helped.
__________________

Please login or register to view this content. Registration is FREE
| Freelance PHP solutions for small to midsized projects |
Please login or register to view this content. Registration is FREE
phpknowhow is offline
Reply With Quote
View Public Profile Visit phpknowhow's homepage!
 
Reply     « Reply to "Or Die" function
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24442 seconds with 12 queries