PHP或死亡以及if else条件特性

I have function that performs a mysql_query() and then does some other stuff. I want to be able to perform another mysql_query() only if the first one succeeds.

Here is the function

function myFunction($qtext)
{
  mysql_query($qtext) or die(mysql_error() . "
");
  //do some more stuff
  return true;
}

I'm calling the function and attempting to check if it failed with an if else conditional...

if(!myFunction($query_text))
{
  echo "first query failed";
}
else
{
  mysql_query($query_text1) or die (mysql_error() . "
");
}

This seems to work when the first query passes, but if the first query fails it goes to the or die and returns the mysql_error() text and the echo "first query failed"; line in the conditional is never reached.

Ideally id like to be able to alert the user with the mysql_error text but without or dieing, so I can run more code in the conditional.

Any help with explanations of behavior is greatly appreciated. Thanks.

p.s. I'm a beginner... I'm not sure if Im using the return true; properly in the function

You're always returning true in the function - you need to also return false if you're checking it in an if() statement.

function myFunction($qtext) {

    // run the query
    $sql = mysql_query($qtext);

    // see if there was a result (or whatever you're checking)
    if(mysql_num_rows($sql) > 0) {
        // do some more stuff
        return true;
    } else {
        return false;
    }
}

Also, you really should learn mysqli or POD instead of mysql, as mysql is depreciated. I also recommend you don't use die() unless you're testing. Build an error handling function instead - it's actually quite easy and will handle errors gracefully instead of abruptly killing the script and annoying your users. You also don't want to print error messages directly to your browser because it can compromise your site's security. :)

Just an FYI: I use a database class and run my queries like this. It's fast and clean.

if($db->get_results("SELECT email FROM users WHERE email='".$db->escape($email)."'")) {
    return true;
} else{
    return false;
}

To prevent "or die" from happening, replace it with return false:

function myFunction($qtext)
{
    $result = mysql_query($qtext);
    if ($result === false)
    {
        return false;
    }
    //do some more stuff
    return true;
}

that way your check later on will work and condition will fire. You don't have to use "or die", that is reserved for the cases where you want to halt all execution.

die() kills the script instantly. That function will never return any value if you call die() inside it, you will never be able to perform other queries. Only the destructors of instanced object are still ran after a die() call, and with several restrictions.

If you want to be able to continue doing stuff after a query fails, you must never call die(). Instead, just check if mysql_query() returned FALSE as Tim suggested. Note the === operator, its important to a proper error check here.

If you still want to print the error like die() would, use print() or echo instead.