+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: SQL errors and PHP

  1. #1
    fguy64's Avatar
    fguy64 is offline x10 Sophmore fguy64 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    218

    SQL errors and PHP

    OK, I am trying to get a handle on what kind of mySQL errors will feed info back to php. I know connection errors feed back to php and back to browsers, but what about the following...
    Code:
    {
        mysql_query( "INSERT INTO users VALUES ("
                    +"'" +$id +"',"
                    +"'" +$pwd +"',"
                    +"'" +$email +"')" );
    
        header( "Location: menu.htm" );
    }
    the code block executes because I get redirected to menu.htm. But my users table doesn't get updated. I tried just removing the header() function, but then I get a blank page. I'ne looked the INSERT statement over pretty closely, I don't see anything wrong, but I'm just a rookie.

    So the question is what can I make out of the fact that no error information gets returned to the browser, yet clearly the mysql_query is not working.

    thanks.

  2. #2
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: SQL errors and PHP

    try a try-catch block:
    PHP Code:
    function exception_error_handler($errno$errstr$errfile$errline ) {
    throw new 
    ErrorException($errstr0$errno$errfile$errline);
    }
    set_error_handler("exception_error_handler");

    try {
         
    mysql_query"INSERT INTO users VALUES ("
                    
    +"'" +$id +"',"
                    
    +"'" +$pwd +"',"
                    
    +"'" +$email +"')" );
    } catch (
    Exception $e) {
        exit(
    'Caught exception: ',  $e->getMessage());
    }

    header"Location: menu.htm" ); 
    The code provided above is not meant to be on a production server. If you intend to use it on a production server, use this instead:
    PHP Code:
    function exception_error_handler($errno$errstr$errfile$errline ) {
    throw new 
    ErrorException($errstr0$errno$errfile$errline);
    }
    set_error_handler("exception_error_handler");

    try {
         
    mysql_query"INSERT INTO users VALUES ("
                    
    +"'" +$id +"',"
                    
    +"'" +$pwd +"',"
                    
    +"'" +$email +"')" );
    } catch (
    Exception $e) {
        
    //write more advance data to an error log.
        
    exit('There was an unexpected error.');
    }

    header"Location: menu.htm" ); 
    IMPORTANT: Using this method (exceptions) will force you to use try-catch blocks to check for errors instead of the regular way.
    Last edited by xav0989; 06-26-2009 at 01:44 PM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  3. #3
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: SQL errors and PHP

    You can also try this.

    First make sure your table “users” has only 3 fields. If your table has more than 3 fields, you need to specify where to insert the data.

    Then you can use:

    PHP Code:
    mysql_query"INSERT INTO users VALUES (
                    '
    $id',
                    '
    $pwd',
                    '
    $email')" ); 

  4. #4
    fguy64's Avatar
    fguy64 is offline x10 Sophmore fguy64 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    218

    Re: SQL errors and PHP

    Quote Originally Posted by gomarc View Post
    You can also try this.

    First make sure your table “users” has only 3 fields. If your table has more than 3 fields, you need to specify where to insert the data.

    Then you can use:

    PHP Code:
    mysql_query"INSERT INTO users VALUES (
                    '
    $id',
                    '
    $pwd',
                    '
    $email')" ); 
    ok goMarc I'll try, but...

    Based on the following reference, how would mySQL know that what is within the single quotes is variable names, and not just text data?

    http://www.w3schools.com/php/php_mysql_insert.asp

    p.s. thanks xav0989, I hadn'tthought about try catch.

  5. #5
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: SQL errors and PHP

    At the end of the page you are making reference, check how it handles the PHP $_POST variables.

    PHP Code:

    $sql
    ="INSERT INTO Persons (FirstName, LastName, Age)
    VALUES
    ('
    $_POST[firstname]','$_POST[lastname]','$_POST[age]')"

  6. #6
    fguy64's Avatar
    fguy64 is offline x10 Sophmore fguy64 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    218

    Re: SQL errors and PHP

    ok gomarc, when I can I'll put all this stuff through the paces, and see how it all shakes out.

    It's still not clear to me how....

    mysql_query( "INSERT INTO users VALUES ( 'Smith', 'xyz', 'smith@aol.com')" );

    will insert the stuff as text data, but your suggestion below will insert the contents of the variables. I guess even within the quotes, $ is a special character, which presumably would have to be escaped if I wanted the VARCHAR data to include $


    mysql_query( "INSERT INTO users VALUES (
    '$id',
    '$pwd',
    '$email')" );

    Anyways, with time I'm sure it'll become clear. Thanks for the help.
    Last edited by fguy64; 06-26-2009 at 03:15 PM.

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: SQL errors and PHP

    Quote Originally Posted by fguy64 View Post
    Based on the following reference, how would mySQL know that what is within the single quotes is variable names, and not just text data?
    Within double quoted strings (and heredocs), variables are interpolated. Single quotes within double quotes are not special, they're just characters.

    For collecting more information about failures, don't forget about driver specific error functions (eg mysql_error(), mysqli_error()). You can use error_log() to log extended error messages for your eyes only. Also remember that many SQL query functions (including mysql_query and mysqli_query) return FALSE if the query failed.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  8. #8
    fguy64's Avatar
    fguy64 is offline x10 Sophmore fguy64 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    218

    Re: SQL errors and PHP

    Quote Originally Posted by misson View Post
    Within double quoted strings (and heredocs), variables are interpolated. Single quotes within double quotes are not special, they're just characters.

    ...
    That may or may not be true, but I doesn't really answer the question.

    Within the double quotes is a mySQL statement.
    within that mySQL statement is the sequence VALUES('Tom', 'Jones')
    within the single quotes is text data.

    now, if I were to replace Tom and Jones with $first and $last e.g.

    VALUES('$first', '$last') and expect $first and $last to be treated as something other than text data, i.e. something other than a string of characters beginning with $, then to say single quotes within double quotes are just characters is not really illuminating.

    Let me put this a different way. If the sequence VALUES('$first', '$last') actually results in two VARCHAR variables containing Tom and Jones, then how would i write that sequence so that my VARCHAR variables contain $first and $last as text strings themselves?

  9. #9
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: SQL errors and PHP

    Quote Originally Posted by fguy64 View Post
    ...

    If the sequence VALUES('$first', '$last') actually results in two VARCHAR variables containing Tom and Jones, then how would i write that sequence so that my VARCHAR variables contain $first and $last as text strings themselves?
    PHP Code:
    $first  '$first';
    $last  '$last'

  10. #10
    fguy64's Avatar
    fguy64 is offline x10 Sophmore fguy64 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    218

    Re: SQL errors and PHP

    ok then it would seem to follow that php is somewhat different than other languages, at least the ones I am familiar with, in the way that it treats string literals and string variables.

    I appreciate the time taken. I think I need to take two steps back an go back to school on my php basics. And then put all this stuff to the test. I'll return to this thread later.

    all the best, and thanks again.

+ Reply to Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. PHP Errors
    By JAL Virtual in forum Free Hosting
    Replies: 10
    Last Post: 01-26-2009, 11:50 AM
  2. Replies: 0
    Last Post: 12-24-2008, 03:59 PM
  3. Php errors
    By Wizet in forum Free Hosting
    Replies: 1
    Last Post: 09-04-2008, 08:48 PM
  4. PHP intermediate and errors are still there
    By BritAngel in forum Free Hosting
    Replies: 4
    Last Post: 08-24-2008, 09:08 PM
  5. php errors galore
    By DMG Online in forum Scripts & 3rd Party Apps
    Replies: 9
    Last Post: 05-17-2008, 06:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers