MySQL Forums
Forum List  »  PHP

Re: html,mysql,php copy data from one db to another
Posted by: brian liddle
Date: June 15, 2012 10:25AM

so i have now updated my code a bit and added in what you said to do in your instructions. im now getting output but its not what its supposed to be. it shows a table with correct dimesions however it shows variable names and not their values. im not sure whats happening because quotes are all in the correct places.

Code:

<html>
<body>

<?php

//Part 1: get data to copy
$con = mysql_connect("IPADDRESS","USERNAME","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("DATABASE", $con);

$MainResult = mysql_query("SELECT column1
,column2
,column3
,column4
,column5
,column6
,column7
,column8
,column9
,column10
,column11
,column12
,column13
,column14
,column15
,column16
,column17
FROM TABLE");

mysql_close($con);

// Done with part 1: get data to copy

//Part 2: Insert and Update with new data
$con = mysql_connect("IPADDRESS","USERNAME","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("DATABASE", $con);


while ($row = mysql_fetch_row($MainResult))
{
$sql="INSERT INTO TABLE VALUES ('".mysql_real_escape_string($row[0])."',
".$row[1].",
'".mysql_real_escape_string($row[2])."',
".$row[3].",
'".mysql_real_escape_string($row[4])."',
'".mysql_real_escape_string($row[5])."',
'".mysql_real_escape_string($row[6])."',
'".mysql_real_escape_string($row[7])."',
'".mysql_real_escape_string($row[8])."',
'".mysql_real_escape_string($row[9])."',
'".mysql_real_escape_string($row[10])."',
'".mysql_real_escape_string($row[11])."',
'".mysql_real_escape_string($row[12])."',
'".mysql_real_escape_string($row[13])."',
'".mysql_real_escape_string($row[14])."',
'".mysql_real_escape_string($row[15])."',
'".mysql_real_escape_string($row[16])."',
NULL)
ON DUPLICATE KEY UPDATE TABLE SET COLUMN1='".mysql_real_escape_string($row[0])."',
COLUMN2=".$row[1].",
COLUMN3='".mysql_real_escape_string($row[2])."',
COLUMN4=".$row[3].",
COLUMN5='".mysql_real_escape_string($row[4])."',
COLUMN6='".mysql_real_escape_string($row[5])."',
COLUMN7='".mysql_real_escape_string($row[6])."',
COLUMN8='".mysql_real_escape_string($row[7])."',
COLUMN9='".mysql_real_escape_string($row[8])."',
COLUMN10='".mysql_real_escape_string($row[9])."',
COLUMN11='".mysql_real_escape_string($row[10])."',
COLUMN12='".mysql_real_escape_string($row[11])."',
COLUMN13='".mysql_real_escape_string($row[12])."',
COLUMN14='".mysql_real_escape_string($row[13])."',
COLUMN15='".mysql_real_escape_string($row[14])."',
COLUMN16='".mysql_real_escape_string($row[15])."',
COLUMN17='".mysql_real_escape_string($row[16])."'";
echo $sql."<br>";
mysql_query($sql)
}
//Done with part 2: Insert and Update with new data

//Part 3: Print Data from Updated DB
$MirrorResult = mysql_query("SELECT * FROM TABLE");

$fields_num = mysql_num_fields($MirrorResult);
echo '<table border="1"><tr>';
$fields = mysql_query("DESCRIBE TABLE;");
while ($row = mysql_fetch_row($fields))
{
echo "<th>".$row[0]."</th>";
}
echo "</tr>";
while($row = mysql_fetch_row($MirrorResult))
{
echo "<tr>"."<td>".$row[0]."</td>".
"<td>".$row[1]."</td>".
"<td>".$row[2]."</td>".
"<td>".$row[3]."</td>".
"<td>".$row[4]."</td>".
"<td>".$row[5]."</td>".
"<td>".$row[6]."</td>".
"<td>".$row[7]."</td>".
"<td>".$row[8]."</td>".
"<td>".$row[9]."</td>".
"<td>".$row[10]."</td>".
"<td>".$row[11]."</td>".
"<td>".$row[12]."</td>".
"<td>".$row[13]."</td>".
"<td>".$row[14]."</td>".
"<td>".$row[15]."</td>".
"<td>".$row[16]."</td>".
"<td>".$row[17]."</td>"."</tr>\n";
}
echo "</table>";

mysql_free_result($MainResult);
mysql_free_result($MirrorResult);

mysql_close($con);
//Done with part 3: Print Data from Updated DB
?>

</body>
</html>

OUTPUT:
"; mysql_query($sql) } //Done with part 2: Insert and Update with new data //Part 3: Print Data from Updated DB $MirrorResult = mysql_query("SELECT * FROM JTD"); $fields_num = mysql_num_fields($MirrorResult); echo '
'; $fields = mysql_query("DESCRIBE JTD;"); while ($row = mysql_fetch_row($fields)) { echo "
"; } echo "
"; while($row = mysql_fetch_row($MirrorResult)) { echo "
"."
". "
". "
". "
". "
". "
". "
". "
". "
". "
". "
". "
". "
". "
". "
". "
". "
". "
"."
\n"; } echo "
_______________________________________________________________________________________________________________________________________________________________________________________________
| ".$row[0]."| | | | | | and so on blank |
|_______________________________________________________________________________________________________________________________________________________________________________________________|
| ".$row[0]."|".$row[1]."|".$row[2]."| and so on sequentially to 17 |
|_______________________________________________________________________________________________________________________________________________________________________________________________|
"; mysql_free_result($MainResult); mysql_free_result($MirrorResult); mysql_close($con); //Done with part 3: Print Data from Updated DB ?>


(I tried to make the table legible)

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.