Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6336659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:06:00+00:00 2026-05-24T19:06:00+00:00

To make my life a bit easier, I created a function DoQuery: // DoQuery

  • 0

To make my life a bit easier, I created a function DoQuery:

// DoQuery
function DoQuery($sql)
{
    global $cm_db_host,$cm_db_user,$cm_db_pass,$cm_db_name;
    $con = mysql_connect($cm_db_host,$cm_db_user,$cm_db_pass);
    if(!$con) return 1001;
    $db = mysql_select_db($cm_db_name);
    if(!$db) return 1002;
    $res = mysql_query($sql);
    if(!$res) return 1003;
    return $res;
    mysql_close();
}

The function works perfectly – it seems..

Here is how I use it:

 $res = DoQuery("UPDATE table SET column1='value 1',column2='value 2' WHERE id=1;");

 switch($res){
    case true:
      echo 'Response[success]ENDCMTAG';
      break;
    case 1001:
      die('Response[dberror1001]ENDCMTAG\r\n'.
      'MySQLError['.mysql_error().']ENDCMTAG\r\n');
      break;
    case 1002:
      die('Response[dberror1002]ENDCMTAG\r\n'.
      'MySQLError['.mysql_error().']ENDCMTAG\r\n');
      break;
    case 1003:
      die('Response[dberror1003]ENDCMTAG\r\n'.
      'MySQLError['.mysql_error().']ENDCMTAG\r\n');
      break;  
 } 

Now, when I purposely screw up the SQL statement, so case 1003 should be used, the case true is what is being executed.

According to the PHP manual (yes, I did look it up! 😉 ) a MySQL INSERT, UPDATE …. will either return true or false. In my DoQuery function, whenever a MySQL function returns false, I output specific error codes instead, so I know where it went wrong. If no error occurs, $res should be true, and then the case true should be executed, right?

The problem is that it always outputs true, even if I change the SQL query to rubbish.

If I put the case true at the bottom of the switch, the case 1001 is always executed when everything goes OK – I know because I check the mysql_error() field, and its empty.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:06:01+00:00Added an answer on May 24, 2026 at 7:06 pm

    You are returning the value 1003, which evaluates to a “truthy” value when compared against a boolean. It will always evaluate to TRUE.

    // Essentially it is this:
    if (1003) {
       // I'm TRUE!!!
    }
    

    You cannot use a strict comparison === either, since in the true case, you are returning a MySQL result resource, not the boolean TRUE.

    If you rearrange your switch() so that the valid resource case is at the bottom, your error cases will be correctly evaluated first.

     switch($res){
        case 1001:
          die('Response[dberror1001]ENDCMTAG\r\n'.
          'MySQLError['.mysql_error().']ENDCMTAG\r\n');
          break;
        case 1002:
          die('Response[dberror1002]ENDCMTAG\r\n'.
          'MySQLError['.mysql_error().']ENDCMTAG\r\n');
          break;
        case 1003:
          die('Response[dberror1003]ENDCMTAG\r\n'.
          'MySQLError['.mysql_error().']ENDCMTAG\r\n');
          break;  
    
        // Valid result is the default case.
        default:
          echo 'Response[success]ENDCMTAG';
          break;
     }
    

    However, this is a little weird and could cause problems if your function ever returns a different error value (if it is changed,) but the switch isn’t updated. Instead, you should check for a true/false return on the value first and then pass it to the switch to evaluate error codes:

    // Check if we have an Integer value. If we do, its an error code!
    // If not, it's a result, or true!
    if (is_int($res)) {
     switch($res){
        case 1001:
          die('Response[dberror1001]ENDCMTAG\r\n'.
          'MySQLError['.mysql_error().']ENDCMTAG\r\n');
          break;
        case 1002:
          die('Response[dberror1002]ENDCMTAG\r\n'.
          'MySQLError['.mysql_error().']ENDCMTAG\r\n');
          break;
        case 1003:
          die('Response[dberror1003]ENDCMTAG\r\n'.
          'MySQLError['.mysql_error().']ENDCMTAG\r\n');
          break;  
     }
     // $res was valid. Success!
     else {
       // handle your valid results
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build some objects to make my life a bit easier, but
It would make life so much easier!
I'm thinking about frameworks. I know they make life a lot easier, but as
I have a small RIA that I built as a learning/make-my-life-easier project that uses
Autocomplete inputs make life easier. However, I am working on a project where the
This may be a really long stretch but would make life a fair bit
What are your favorite Git configuration settings which make your life easy while working
I can't for the life of me find a way to make this work.
Suppose this is my URL route: (r'^test/?$','hello.life.views.test'), How do I make it so that
To make things easier when switching between machines (my workstation at the office and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.