INSERT fails, 'cos connection was made in an included file.

Discussion in 'PHP' started by Hade, Oct 19, 2007.

  1. #1
    I have a script which works fine on most web servers.

    On GoDaddy and another, it won't work. Strangely, SQL INSERTs don't work on this page.
    The Exact same SQL INSERTs work fine when the connection to the database is made on the same page.
    In my script, the connection is made in an included file.

    Why could this be? I've debugged and the connection code is being run before the INSERT.

    For example, this works:

    $dbConnection = mysql_pconnect(DATABASE_HOST, DATABASE_USER, DATABASE_PASS) or trigger_error(mysql_error(),E_USER_ERROR); 
    mysql_select_db(DATABASE_NAME, $dbConnection);
    
    $insert_question_query_sql = "insert into qa_questions (questionid, content) values ('123', 'abc')";
    				mysql_query($insert_question_query_sql) or die ('Error Inserting Data into the Database!') ;
    PHP:

    But when the main file includes a config.php file then the file with the SQL in, it won't work. I've tried adding this to the top:

    ini_set('display_errors','1');
    ini_set('display_startup_errors','1');
    error_reporting (E_ALL); 
    PHP:
    But it tells me nothing.
    Any ideas?
     
    Hade, Oct 19, 2007 IP
  2. theOtherOne

    theOtherOne Well-Known Member

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #2
    You could change the code to this:

    
     mysql_query($insert_question_query_sql) or die ('Error Inserting Data into the Database!<br />Message from DB: '.mysql_error()) ;
    
    PHP:
    Then you can see whether MySQL raises an error...
     
    theOtherOne, Oct 19, 2007 IP
    Hade likes this.
  3. Shimonenator

    Shimonenator Peon

    Messages:
    28
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What error are you getting? How do you include?
     
    Shimonenator, Oct 19, 2007 IP
    Hade likes this.
  4. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    #4
    Thanks for the help.
    Here: the MySQL error:

    I'm using include('config.php');

    My page (index.php) includes application_top.php.
    application_top.php includes config.php (which hold dbase settings)
    application_top.php then connects to the DB.

    index.php then includes import.php which in turn includes import_data.php.
    import_data.php runs the SQL.

    I tried echoing out as the database is connected to: success.
    I also echoed out dbase settings as the SQL is being performed: success.

    There isn't a problem with my including. Besides, this script works on 95% of web hosts.
     
    Hade, Oct 19, 2007 IP
  5. theOtherOne

    theOtherOne Well-Known Member

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Well... would be strange, but does it work like this?

    
    mysql_query($insert_question_query_sql, $dbConnection) or die ('Error Inserting Data into the Database!<br />Message from DB: '.mysql_error()) ;
    
    PHP:
     
    theOtherOne, Oct 19, 2007 IP
  6. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    #6
    Then it gives this...

     
    Hade, Oct 19, 2007 IP
  7. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #7
    have you tried using mysql_connect instead of a persistent connection?

    Are you sure your connection string is correct?

    What type of account are you using?

    All shared hosting plans do not allow making remote connections to servers not owned by GoDaddy. So if you're trying to connect to a db server other than the ones provided by GoDaddy, it's not going to work.

    I've never had a problem with database servers provided by GoDaddy.
     
    KalvinB, Oct 19, 2007 IP
    Hade likes this.
  8. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    #8
    Yep, still won't work

    Yep, select statements work fine. Even one on the same page. All on the same database connection.

    It's a paid shared account

    It's Godaddy's database. As I said, the select statements are ok.
     
    Hade, Oct 19, 2007 IP
  9. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    #9
    Excellent support email from GoDaddy. I'm happy with this:

    I'll work on restructuring now :D
     
    Hade, Oct 19, 2007 IP
  10. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #10
    good to know if I ever do run into an issue.
     
    KalvinB, Oct 19, 2007 IP