Jump to content

Transferring records from Access to MySQL via PHP


Eiolon

Recommended Posts

When users click the refresh button, I want the records from the Access database to move to the MySQL database.

 

It semi-working except I am only getting the first record from the Access database transferred over.  Any ideas on how to get the other records?

 

<?php

$conn=odbc_connect('pcres','','');
$sql="SELECT PC_Id, Stop_Time FROM usage";
$rs=odbc_exec($conn,$sql); 

require_once('includes/mysql.php');

$pc = odbc_result($rs,"PC_Id");
$stop = odbc_result($rs,"Stop_Time");

$query_users = "SELECT pc, stop FROM pcres ORDER BY stop ASC";
$users = mysql_query($query_users) OR die ('Cannot retrieve a list of current users.');

if (isset($_POST['refresh'])) {

$delete_old = "DELETE FROM pcres";
$result_old = mysql_query($delete_old) OR die ('Could not delete old data.');

if ($delete_old) {

$insert = "INSERT INTO pcres (pc, stop) VALUES ('$pc','$stop')";
$result = mysql_query($insert) OR die ('Could not insert into database.');

if ($insert) {
header('Location: index.php');
exit; }

}

}

?>

<form id="add" name="add" method="post" action=""><input type="submit" name="refresh" id="button" value="REFRESH" /></form>

<table class="data" border="1">
  <tr>
    <th><div align="left">PC</div></th>
<th><div align="left">Stop Time</div></th>
  </tr>
  <?php do { ?>
  <tr onmouseover="this.bgColor='#EBFAFF';" onmouseout="this.bgColor='#FFFFFF';">
    <td><?php echo $row_users['pc']; ?></td>
<td><?php echo $row_users['stop']; ?></td>
  </tr>
  <?php } while ($row_users = mysql_fetch_assoc($users)); ?>
</table>

You need to loop the data: http://php.net/manual/en/function.odbc-fetch-array.php

 

while( $myRow = odbc_fetch_array( $rs ) )
{
  $accessRows[] = $myRow;
} 

 

Then you will have an array of the results and can use foreach to loop through them and do what you will with them (or you can add that logic inside the while.)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.