MySQL插入多个数据库 - 发出整个代码

时间:2012-08-05 21:30:31

标签: php mysql mysql-backup

我不想说太多,这是我的代码(我知道它不是PDO,但一旦它可以工作)。几乎一切都在运作!!!

现在我正在做日志而且我被困住了! - 更新感谢迈克尔的一些已修复!

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

也优化功能。这不适用于多个DB某些原因......

许可不想是正确的我看了几个小时,我无法理解。一切都很美妙。

代码从真实数据库中读取一些行并将它们移动到backupDB。但我想要一个cron作业和电子邮件警报,所以我做了一个简单的日志(进入一个表),但它不想工作...我甚至看不到任何错误日志。好吧:PHP警告:mysql_query():提供的参数不是第34行/home/sites/stcticketing.org/public_html/back/asu1.php中有效的MySQL-Link资源

<?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);
 ?>

请先修复它然后评论任何PDO,除非解决方案本身:)任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

在失败的函数内部(在其他函数db_rows()中正确完成),您的数据库资源链接变量超出范围。将它们作为参数传递给函数:

例如,在$connection传递一个:

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;
}

然后使用正确的连接调用函数,如:

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