You are here:  » inserting into a database


inserting into a database

Submitted by griffinsbridge on Tue, 2007-10-30 16:24 in

Oh the headache!

I'm trying to add the contents of a feed into a database table. Ive read every post in these forums about inserting into a db but none seem to cover my problem.

my code:

<?php
  require("MagicParser.php");
    require_once('Config.php');
  function myRecordHandler($record)
  {
print('<pre>');
    print_r($record);
    print $record["RESULTSET"];
    print $record["STATUS"];
    print $record["TRANS_DATE_TIME"];
    print $record["LINK_ID"];
    print $record["TRANSACTION_VALUE"];
    print $record["AFFILIATE_EARNS"];
    print $record["CURRENCY"];
    print $record["PROGRAMME_NAME"];)
    print $record["PP_ENABLED"];
    print $record["MD5_TRANS_ID"];
$trans_date = date("Y-m-d", strtotime($record["TRANS_DATE_TIME"]));
$result = mysql_query("INSERT INTO my_db_table (trans_id, name, retailer, amount, trans_date, status, approve_date) VALUES ('','".$record["LINK_ID"]."','".$record["PROGRAMME_NAME"]."','".$record["AFFILIATE_EARNS"],"','".$trans_date."','','')") or die (mysql_error());
print('Added to DB');
  }
  MagicParser_parse("MYFILE.xml","myRecordHandler","xml|RESULT/BODY/RESULTSET/");
  mysql_close($dbn);
?>

error:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/root/public_html/dir/newTest.php

which, as Im sure everyone knows, means the query isnt connecting to the DB.

Ive tried alsorts mate. Added $dbn (mysql variable) as a global in myRecordHandler, include, require both in and out of the function. Ive even tried to force a different error within the INSERT query, but it never gets that far.

for some reason, it's just not connecting to the DB within myRecordHandler.

Am I going about this all the wrong way?

I thought I may have to run a foreach loop within myRecordHandler, then maybe deal with the database insert outside, but not sure.

Any ideas David?

Submitted by support on Tue, 2007-10-30 17:12

Hi,

You need to confirm that your database connection is working regardless of your parse code. What I would do is comment out the line with MagicParser_parse(), and then add a line to do a trial INSERT to give yourself a controlled environment in which to debug your MySQL problem. For example:

<?php
  
require("MagicParser.php");
  require_once(
'Config.php');
  function 
myRecordHandler($record)
  {
    print(
'<pre>');
    
print_r($record);
    print 
$record["RESULTSET"];
    print 
$record["STATUS"];
    print 
$record["TRANS_DATE_TIME"];
    print 
$record["LINK_ID"];
    print 
$record["TRANSACTION_VALUE"];
    print 
$record["AFFILIATE_EARNS"];
    print 
$record["CURRENCY"];
    print 
$record["PROGRAMME_NAME"];)
    print 
$record["PP_ENABLED"];
    print 
$record["MD5_TRANS_ID"];
    
$trans_date date("Y-m-d"strtotime($record["TRANS_DATE_TIME"]));
    
$result mysql_query("INSERT INTO my_db_table (trans_id, name, retailer, amount, trans_date, status, approve_date) VALUES ('','".$record["LINK_ID"]."','".$record["PROGRAMME_NAME"]."','".$record["AFFILIATE_EARNS"],"','".$trans_date."','','')") or die (mysql_error());
    print(
'Added to DB');
  }
  
// MagicParser_parse("MYFILE.xml","myRecordHandler","xml|RESULT/BODY/RESULTSET/");
  
$result mysql_query("INSERT INTO my_db_table (trans_id, name, retailer, amount, trans_date, status, approve_date) VALUES ('','123456','Test','1.23','2007-10-30','','')") or die (mysql_error());
  
mysql_close($dbn);
?>

(make sure the SQL is valid of course!) If you are still not able to INSERT, this means that the database connection has failed, so check your code within Config.php file, assuming that this file contains code to make the database connection.

If Config.php doesn't have any code to connect to the database, then that is the problem, and you would need to add mysql_connect() code at the top of your script...

Hope this helps,
Cheers,
David.

Submitted by griffinsbridge on Tue, 2007-10-30 17:21

Hi David

found it
".$record["AFFILIATE_EARNS"],"
Commas eh!!

I lead myself to beleive that it wasn't connecting due to the constraints of the function, when infact, it wasn't connecting because Im a bloody muppet!!

Thanks for your time mate, much appreciated.