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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
    • ¥15 相敏解调 matlab
    • ¥15 求lingo代码和思路
    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应