PHP MYSQL长查询在php中失败但在mysql中工作

时间:2017-08-23 22:46:22

标签: php mysql

好的,所以我写了一些代码来备份配置项。此脚本可以在其他所有内容中正常运行,但在获取此特定配置项的长查询时会失败。

出于理智目的,我将包括此部分的代码。

function writepool($pool, $device){
  $pooltext = implode('', $pool['list']);
  $sql = "INSERT into pools (deviceid, name, config) VALUES (
  '".$device."',
  '".$pool['pool']."',
  '".addslashes($pooltext)."'
  )";
  echo $sql;
  $addpool = insertdb($sql);
}

function insertdb($sql){
  include('/var/www/db_login.php');
  $conn = new mysqli($db_host, $db_username, $db_password, "F5");

  // check connection
  if ($conn->connect_error) {
    trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
  }

  if($conn->query($sql) === false) {
      trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
  } else {
      $last_inserted_id = $conn->insert_id;
      $affected_rows = $conn->affected_rows;
  }
      $result["lastid"] = $last_inserted_id;
      $result["affectedrow"] = $affected_rows;

      return $result;
      $conn->close();
}

我得到的错误信息如下

Fatal error: Wrong SQL: INSERT into pools (deviceid, name, config) VALUES ( '71', 'shopping.a.prod_pool_53601', 'ltm pool /Common/shopping.a.prod_pool_53601 { load-balancing-mode weighted-least-connections-node members { /Common/10.216.26.55:53601 { address 10.216.26.55 priority-group 1 } /Common/10.216.26.57:53601 { address 10.216.26.57 priority-group 1 } /Common/10.216.26.58:53601 { address 10.216.26.58 priority-group 1 } /Common/10.216.26.59:53601 { address 10.216.26.59 priority-group 1 } /Common/10.216.26.60:53601 { address 10.216.26.60 priority-group 1 } /Common/10.216.26.61:53601 { address 10.216.26.61 priority-group 1 } /Common/10.216.26.62:53601 { address 10.216.26.62 priority-group 1 } /Common/10.216.26.66:53601 { in /var/www/html/functions.php on line 2286

请注意查询非常长。这个特殊的配置项目是巨大的。我将它存储在MYSQL中的BLOB中。

如果我回显$ sql变量,我可以看到我的整个查询字符串。查询太长,无法放在这里。

'如果我将查询字符串复制到MYSQL,它就可以了。 如果我从我的echo复制查询字符串。并使用一个测试页面并将$ sql =放入我的失败脚本的字符串echo' d。有用。我希望我可以发布查询,但由于blob数据它太长了。

******更新*********

我做了tadman建议的事情,我转到准备好的声明。但是,现在我没有在表格中的blob中输入任何数据。

function writepool($pool, $device){
$pooltext = implode('', $pool['list']);
/*
$sql = "INSERT into pools (deviceid, name, config) VALUES (
'".$device."',
'".$pool['pool']."',
'".addslashes($pooltext)."'
)";
*/
#$addpool = insertdb($sql);

include('/var/www/db_login.php');
$mysqli = new mysqli($db_host, $db_username, $db_password, "F5");

// Check connection
if($mysqli === false){
    die("ERROR: Could not connect. " . $mysqli->connect_error);
}

// Prepare an insert statement
$sql = "INSERT into pools (deviceid, name, config) VALUES (?, ?, ?)";

if($stmt = $mysqli->prepare($sql)){
    // Bind variables to the prepared statement as parameters
    $stmt->bind_param("ssb", $db_device, $db_pool, $db_text);

    // Set parameters
    $db_device = $device;
    $db_pool = $pool['pool'];
    $db_text = addslashes($pooltext);

    // Attempt to execute the prepared statement
    if($stmt->execute()){
        #echo "Records inserted successfully.";
    } else{
        echo "ERROR: Could not execute query: $sql. " . $mysqli->error;
    }
} else{
    echo "ERROR: Could not prepare query: $sql. " . $mysqli->error;
}

// Close statement
$stmt->close();

// Close connection
$mysqli->close();
}

$ db_text是系统文件生成的配置数据的一部分。我将它存储为blob,因为它很大(1600多行)。

0 个答案:

没有答案