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!