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



我不想说太多,这是我的代码(我知道它不是PDO,但一旦它能工作,它就会工作)。几乎所有东西都在工作!!!

现在我在做日志,我被卡住了!-更新感谢迈克尔,其中一些已经修复!

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

优化功能。由于某种原因,这不适用于Multiple DB

许可不想是正确的,我已经看了几个小时了,我想不通。其中的一部分,一切都很完美。

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

<?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,除非解决方案本身:)如有任何帮助,我们将不胜感激!

在失败的函数内(在其他函数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);

相关内容

  • 没有找到相关文章

最新更新