MySQL Insert into multiple databases - issue the whole code

2.4k views Asked by At

I wouldn't want to talk too much, here is my code (I know it is not PDO, but it will once it can work). Almost everything working!!!

Now I am doing the logs and I am stuck! - UPDATE thanks to Michael some of it has fixed!

$statresult=mysql_query($statsql, $actconn) or die(mysql_error());

Optimize function too. This is not working with Multiple DB some reason...

The permission doesn't want to be right I have looked hours and I can't figure it out. A part from that everything works beautifully.

The code reads some rows from the real DB and moves them to the backupDB. But I want a cron job and email alert so I did an easy log (into a table) but it doesn't want to work... I can't even see any error log. Well: PHP Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/sites/stcticketing.org/public_html/back/asu1.php on line 34

<?php
// open db
$dbhost = 'localhost';
$actdbuser = 'user1';
$actdbpass = 'pass';

$bckdbuser = 'user2';
$bckdbpass = 'pass';

$actconn = mysql_connect($dbhost, $actdbuser, $actdbpass) or die ('act Error connecting to mysql');
$bckconn = mysql_connect($dbhost, $bckdbuser, $bckdbpass) or die (' back Error connecting to mysql');

$actdbname = '`web151-tevenyal`';
mysql_select_db($actdbname, $actconn);

$bckdbname = '`web151-bckproba`';
mysql_select_db($bckdbname, $bckconn);

//end opendb


//functions
function test($sqls, $states){
 if ($sqls=1){
   $resflag=1;
 }
 else {
   $resflag=0;
 }
 $statsql="INSERT INTO `web151-tevenyal`.`tex_bcklog` (`what`,`how`,`when`) VALUES ('$states',$resflag,CURDATE());";
 echo "<p>".$sqls."<br/>".$statsql."</p>"; 
 $statresult=mysql_query($statsql, $actconn) or die(mysql_error());
 echo "<p>".$states." - ".$resflag."</p>";
 $emaltext=$emailtext.$sqls;
}

function db_rows($db,$ord, $connectdb){
 $dbquery="SELECT azon FROM $db ORDER BY azon $ord LIMIT 1";
 $dbresult=mysql_query($dbquery, $connectdb);
 $row = mysql_fetch_array($dbresult);
 $dbrow = $row['azon'];
 return $dbrow;
}
// end of functions

//config information...
$acttable  = 'adat';
$today = date("yW_Hi");
$newdb = $bckdbname.".test_".$today;
test($permsql, "grant");
test(1, "backupstart");

//creating log table for backup results
$backtablesql="CREATE TABLE IF NOT EXISTS `tex_bcklog` ( `azon` int(11) NOT NULL AUTO_INCREMENT,`what` varchar(255) CHARACTER SET utf8 NOT NULL,  `how` varchar(255) CHARACTER SET utf8 NOT NULL,  `when` date NOT NULL,   PRIMARY KEY (`azon`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;" ;
//$backtableresult=mysql_query($backtablesql);



$firstact= db_rows($actdbname.".".$acttable,"asc", $actconn);
$lastact= db_rows($actdbname.".".$acttable,"desc", $actconn);
$upto=$firstact+25000;


if ($lastact-$firstact>50000) {

 //create a new table 
 $permsql="GRANT SELECT ON `$actdbuser`.* TO `$bckdbuser`@'%' ;";
 test($permsql, "grant");
 $perm = mysql_query($permsql, $bckconn) or die(mysql_error());

 $newdbsql="CREATE TABLE $newdb LIKE $actdbname.`$acttable`";
 test($newresult, "createdb");
 $newresult = mysql_query($newdbsql, $bckconn) or die(mysql_error());


 // copy all the data
 $query = "INSERT INTO $newdb SELECT * FROM $actdbname.$acttable WHERE $acttable.azon < $upto";
 test($query, "copyrows");
 $result = mysql_query($query) or die(mysql_error());

 // so what has happened...
 $delquery = "DELETE FROM $actdbname.$acttable WHERE $actdbname.$acttable.azon < $upto";
 test($delquery, "deleterows");
 $delresult = mysql_query($delquery, $actconn);

 // then tidy up everything:)
 $res = mysql_query('SHOW TABLE STATUS WHERE Data_free / Data_length > 0.1 AND Data_free >  102400', $actconn);
 while($optrow = mysql_fetch_assoc($res)) {
   mysql_query('OPTIMIZE TABLE ' . $optrow['Name']);
  }
 }
 else {
  test(0, "nothing");
 }

 // send an email to confirm what's happened - thanks:)

 // close db 
 mysql_close($actconn);
 mysql_close($bckconn);
 ?>

Please fix it first and then comment any PDO unless the solution itself:) Any help would be greatly appreciated!

1

There are 1 answers

2
Michael Berkowski On BEST ANSWER

Inside the function that is failing (it is done correctly in the other function db_rows()), your database resource link variables are out of scope. Pass them to the functions as parameters:

For example, pass one in as $connection here:

function test($sqls, $states, $connection){
 if ($sqls=1){
   $resflag=1;
 }
 else {
   $resflag=0;
 }
 $statsql="INSERT INTO `web151-tevenyal`.`tex_bcklog` (`what`,`how`,`when`) VALUES ('$states',$resflag,CURDATE());";
 echo "<p>".$sqls."<br/>".$statsql."</p>"; 
 $statresult=mysql_query($statsql, $connection) or die(mysql_error());
 echo "<p>".$states." - ".$resflag."</p>";
 $emaltext=$emailtext.$sqls;
}

Then call the function with the correct connection as in:

test($permsql, "grant", $actconn);