dongmin1166 2019-04-22 15:33
浏览 137

尝试从SQL Server和MySQL同步表; 尝试使用数组中的where语句从MySQL中选择所有数据时查询错误

I'm trying to sync to identical tables from SQL Server to MySQL through PHP. However, I keep on running on the error "mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given". I know that this particular error lets me know that something is wrong with my query, but I've tried every fix I know (changing column names to a non-keyword, making sure names aren't mispelled, etc) and it's still not working. Here is the code:

<?php

$serverName = "<my_server_name>";
$connectionInfo_mssql = array("Database"=>"<db_name>", "CharacterSet"=>"UTF-8");

$conn_mssql = sqlsrv_connect($serverName, $connectionInfo_mssql);
$conn_mysql = new mysqli("localhost", "<user>", "<pass>", "<db_name>");

//SELECT FROM SQL SERVER DB
$mssql_array = array();
$ms_sql = "SELECT * FROM dumma";
$mssql_query = sqlsrv_query($conn_mssql , $ms_sql);

while($row = sqlsrv_fetch_array($mssql_query))
{

  $mssql_array[] = array('descript' => $row['descript'], 'codee' => $row['codee'], 'classification' => $row['classification'], 'id' => $row['id'], 'remark' => $row['remark']);

}

foreach($mssql_array as $key => $value)
{

   //SELECT FROM MySQL DB
   $my_sql = " SELECT * FROM dumma WHERE descript ='".$value['descript']."' AND codee = '".$value['codee']."' AND classification = '".$value['classification']."' AND id = '".$value['id']."' AND remark = '".$value['remark']."' ";

   if ($my_sql) {
   $mysql_query = mysqli_query($conn_mysql , $my_sql);
   $num_rows = mysqli_num_rows($mysql_query);
}

if (!$my_sql->query("SET a=1")) {
    printf("Errormessage: %s
", $my_sql->error);
}

   $mysql_query = mysqli_query($conn_mysql , $my_sql);
   $num_rows = mysqli_num_rows($mysql_query);

   if ($num_rows == 0)
   {
     //INSERT INTO MySQL DB
     $sql = "INSERT INTO dumma VALUES ('".$value['descript']."', '".$value['codee']."', '".$value['classification']."', '".$value['id']."', '".$value['remark']."')";
     $sql_query = mysqli_query($conn_mysql, $sql);
   }
}

echo "dumma Table MySQL updated!"."<br>";
echo "<a href='table_updater.php'>Go back to updater</a>";

?>

The error occurs in this line:

$my_sql = " SELECT * FROM dumma WHERE descript ='".$value['descript']."' AND codee = '".$value['codee']."' AND classification = '".$value['classification']."' AND id = '".$value['id']."' AND remark = '".$value['remark']."' ";
   $mysql_query = mysqli_query($conn_mysql , $my_sql);
   $num_rows = mysqli_num_rows($mysql_query);

Part of the dumma table and column data:

https://i.imgur.com/20cFugp.png https://i.imgur.com/ucebUao.png (sorry, i dont have enough rep to post it here directly)

For some reason, it's ONLY the "descript" column that's causing the problems. I've tried removing them all one-by-one and when I removed the "descript" column from the syntax I didn't get the error (unfortunately this causes huge problems because instead of syncing the two tables, it doubles up the contents of the MySQL table instead). Also, I've used this exact same code with other tables (only changing the variables) and they worked perfectly.

Any ideas are much appreciated!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥35 平滑拟合曲线该如何生成
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 自己瞎改改,结果现在又运行不了了
    • ¥15 链式存储应该如何解决
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站