I have a script that I intend to automate that copies a database table. I've been informed about replication but I have no idea of how to get it working properly, so I've decided to use this method instead.
To try and minimize security risks, our live site is going to make requests for the database table that our other site regularly uses. The request script, which I'll refer to as request.php, is as follows.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
//Idea is to use this to get response to provide table data
//What goes here?
</script>
</head>
<body>
<?php
//Define database handler
$dbWriter = new mysqli(database connection info);
if($mysqli->connect_errno){
$errorMessage = sprintf("'%s'", $mysqli->connect_error);
echo $errorMessage;
}
else{
$dbWriter->query("DELETE * FROM qb_inventory_status");
//Processing of data goes here
foreach($returnedItems as $item){
//Create query to insert individual records
$start = "INSERT INTO table(fieldA, fieldB...) VALUES (";
$end = sprintf("'%s', %d, %d, %f, %f)", fieldA, fieldB...);
$start .= $end;
//Database handler uses $start to insert record
$dbWriter->query($start);
}
$dbWriter->close();
}
?>
</body>
</html>
The response script (response.php) is as follows.
<?php
require_once($documentRoot."/classes/DBHandler.php");
$db_controller = new DBHandler();
$query = "SELECT * FROM table";
$result = $db_controller->select($query);
//This array is what should be returned so request.php can handle it
while($rowObject=mysql_fetch_object($rs)){
$rtnItems[]=$rowObject;
}
//Return $rtnItems here
?>
I know how to retrieve information from a database and put it in a format suitable for processing within the same file, but when it comes to a situation like this, I'm not sure how to pass it back to request.php. How can I make this work? I need to be able to write the returned array to the database. I've never done the latter process with Javascript before, so I was also wondering if it's possible to access the returned database with the PHP portion of request.php.