【问题标题】:Insert mysql data from remote mysql table using php使用php从远程mysql表中插入mysql数据
【发布时间】:2014-11-18 22:47:07
【问题描述】:

使用 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();
?>

【问题讨论】:

    标签: php mysql database mysqli


    【解决方案1】:

    你确定你可以远程连接到那个 mysql 吗?

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

    如果它是开放的,您可能需要将您的 ip 列入白名单

    【讨论】:

    • 这可能不是问题,因为当 mysqli 无法连接时,您会收到类似 PHP Warning: mysqli::mysqli() 的警告消息。如果没有数据库连接,prepare 也会发出警告。
    • 不幸的是,这不是问题所在。我可以远程连接。
    【解决方案2】:

    我不断收到“在非对象上调用成员函数 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;    
    } 
    

    【讨论】:

    • 它没有在 localDB 或 remoteDB 返回错误消息
    • @user1667601 “它”是什么意思?
    • 除了问题中提到的错误之外,php 脚本没有返回任何特定错误
    • @user1667601 $remoteDB-&gt;error 一个还是mysqli_connect_error() 一个?
    • 在开始呼叫new mysqli之前放置mysqli_report(MYSQLI_REPORT_ALL);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2014-06-16
    相关资源
    最近更新 更多