使用php从远程mysql表插入mysql数据

时间:2014-11-18 22:47:08

标签: php mysql database mysqli

使用php,我试图访问远程mysql表并将其导入本地服务器。但是,我无法弄清楚如何让导入工作。我一直在“非对象上调用成员函数bind_param()”错误。

<?php
$localDB = new mysqli("", "", "", "");  //filled in for actual but ignored for question
$remoteDB = new mysqli("", "", "", ""); //filled in for actual but ignored for question
$pat = "0";

$getQuery = "SELECT eventname, da, mont, yea, autoinc, pic, codez, emai, pricof, excl FROM `eventannoun` WHERE da > ?";
$getStmt = $remoteDB->prepare($getQuery);
$getStmt->bind_param('i', $pat); //error here
$getStmt->execute();
$getStmt->bind_result($message, $da, $mont, $yea, $autoinc, $pic, $codez, $emai, $pricof, $excl);

$putQuery = "INSERT INTO `eventannoun` (eventname, da, mont, yea, autoinc, pic, codez, emai, pricof, excl) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$putStmt = $localDB->prepare($putQuery);
$putStmt->bind_param('siiiiisssi',$message, $da, $mont, $yea, $autoinc, $pic, $codez, $emai, $pricof, $excl);

while ($getStmt->fetch()) {
  $putStmt->execute();
  $putStmt->fetch();
}
$getStmt->close();
$putStmt->close();
?>

2 个答案:

答案 0 :(得分:0)

  

我不断收到“对非对象调用成员函数bind_param()”错误。

如果出现错误,

mysqli::prepare会返回false

false不是对象,因此您收到错误:

call to method function on bind_param() on a non-object

您可以通过检查mysqli::error属性来获取错误消息。文件摘录如下:

  

返回最近可以成功或失败的MySQLi函数调用的最后一条错误消息。

// at the top of your script place this.
mysqli_report(MYSQLI_REPORT_ALL);

/* Do all your connection stuff */

$getQuery = ""; // query goes here
// your problem is actually on this line
$getStmt = $remoteDB->prepare($getQuery); 
if (!$stmt) {
    $err = $remoteDB->error; 
    echo $err; // this will tell you why your query is invalid
    // do something with $err
}

prepare失败的最典型原因是格式错误或无效的查询,但在不知道客户架构或约束的情况下,我无法确定您的特定问题是什么。

另一个潜在问题是您无法连接到远程数据库。但我怀疑这是问题,因为你会看到警告信息。以下是一些有助于实现这种可能性的代码。

$remoteDB = new mysqli("","","","");
if (mysqli_connect_errno() !== 0) {
    print_r( mysqli_connect_error() );
    die;    
} 

答案 1 :(得分:0)

您确定可以远程连接到该mysql吗?

检查服务器上的3306端口是否已打开(使用http://www.yougetsignal.com/tools/open-ports/等工具)

如果它已打开,您可能需要将您的IP列入白名单