Hello - My problem is as follows (I am trying to migrate from a mssql
server to a mysql server)
<?
## I have already made pconnect statements to both the mssql and
mysql servers.
## I have summarized what I am trying to achieve below (actual,
full code available below):
$data = mssql_result(mssql_query("SELECT field1 FROM table1
WHERE ID='1'"),0,0);
echo $data;
mysql_query("INSERT INTO table2 VALUES('$data')");
?>
The data in the MSSQL table is: ��
IF I call this PHP code from my browser (IE6) it results in the
following output: �� and it also places into the MySQL database the
value: ��
However, if I call this PHP code from the command line, using "php.exe
test.php" it also results in the following output: �� but it places into
the MySQL database: ��
Does anyone have any idea why this is happening?
I am sure it is a character set issue, but for the life of me I cannot
work out why the command-line does not run the exact same script in the
same way as a browser would?
Regards,
Emile Axelrad, <mailto:[EMAIL PROTECTED]>
[EMAIL PROTECTED]
-- Reposted in response to: -
Ouch.
First, you're mixing and matching mssql and mysql functions. Second,
there is no function named mysql_insert(). Third, mssql_result()
requires three arguments, you only provided two. Fourth, if you mean to
be calling mysql_result(), I've never seen folks call it with a
mysql_query()
function in it, people usually call the query function separately first
and put the resource identifier into a variable. Fifth, all of this
could be thrown off by your retyping the example rather than
copying/pasting or
importing the acutal script that's giving you the error. Sixth, you
posted the same question twice.
--Dan
--
<html><head><style>
BODY,td {font:9pt Verdana, Arial, sans-serif;color:red}
.black {color:black}
</style></head>
<body>
<?
set_time_limit(3530);
#mssql_pconnect("SQL-1", "xxx", "xxx");
mssql_pconnect("SQL-1", "xxx", "xxx");
mssql_select_db ("xxx");
mysql_pconnect("localhost", "xxx", "xxx");
function AddToErrorDisplay ($text) {
global $errordisplay;
echo $text;
$errordisplay .= $text;
return true;
}
function CreateTable($tablename) {
$_temp = mssql_query("exec sp_columns @table_name =
'$tablename'");
while ($row = mssql_fetch_assoc($_temp)) {
$tablestructure[] = $row;
}
##
## Build fieldlist and create table structure
##
$createquery = "CREATE TABLE `acclinetest`.`" .
strtolower(ereg_replace("[[:space:]+]","",$tablename)) . "_import`
(\r\n";
foreach ($tablestructure as $rowid=>$row) {
if ($rowid > 0) $fieldlist .= ",";
if ($row["TYPE_NAME"] == "ntext") {
$fieldlist .= "SUBSTRING([" .
$row["COLUMN_NAME"] . "],1,DATALENGTH([" . $row["COLUMN_NAME"] . "])) As
[" . $row["COLUMN_NAME"] . ']';
} else {
$fieldlist .= '[' . $row["COLUMN_NAME"] . ']';
}
switch ($row["COLUMN_NAME"]) {
#case 'group':
case 'option':
$createquery .= "`" .
$row["COLUMN_NAME"] . "column` ";
break;
default:
$createquery .= "`" .
$row["COLUMN_NAME"] . "` ";
break;
}
switch ($row["TYPE_NAME"]) {
case 'nvarchar':
if (($row["LENGTH"]/2) > 255) {
$createquery .= 'MEDIUMTEXT';
} else {
$createquery .= 'VARCHAR(' .
($row["LENGTH"]/2) . ')';
}
break;
case 'varchar':
if ($row["LENGTH"] > 255) {
$createquery .= 'MEDIUMTEXT';
} else {
$createquery .= 'VARCHAR(' .
$row["LENGTH"] . ')';
}
break;
case 'nchar':
case 'char':
$createquery .= 'VARCHAR(' . $row["LENGTH"] .
')';
break;
case 'text':
case 'ntext':
$createquery .= 'MEDIUMTEXT';
break;
case 'varbinary':
$createquery .= 'BLOB';
break;
case 'image':
$createquery .= 'MEDIUMBLOB';
break;
case 'smallint':
$createquery .= 'SMALLINT(' .
($row["PRECISION"]+1) . ')';
break;
case 'tinyint':
$createquery .= 'TINYINT(' .
($row["PRECISION"]) . ')';
break;
case 'int':
$createquery .= 'INT(' . ($row["PRECISION"]+1)
. ')';
break;
case 'float':
$createquery .= 'FLOAT(8)';
break;
case 'int identity': # AUTO_INCREMENT field
$createquery .= 'INT(' . ($row["PRECISION"]+1)
. ')';
break;
case 'bit':
$createquery .= 'TINYINT(4)';
break;
case 'real':
$createquery .= 'FLOAT(4)';
break;
case 'money':
$createquery .= 'DECIMAL(19,4)';
break;
case 'smalldatetime':
case 'datetime':
$createquery .= 'VARCHAR(40)';
break;
default:
AddToErrorDisplay("<br><b>UKNOWN FIELD TYPE: "
. $tablename . ' --> ' . $row["COLUMN_NAME"] . ' --> ' .
$row["TYPE_NAME"] . "</b>");
AddToErrorDisplay("<br><span
style='display:none'>" . print_r($row) . "</span>");
break;
}
$createquery .= ' ' . ($row["NULLABLE"] ? "NULL" : "NOT
NULL");
if ($rowid+1 < sizeof($tablestructure)) $createquery .=
",";
$createquery .= "\r\n";
# print_r($row);
}
$createquery .= "\r\n)";
#echo $fieldlist;
#echo $createquery;
mysql_query($createquery);
AddToErrorDisplay(" \r\n:: <span class=black><b>[" .
date("H:i:s") . "] " . $tablename . "</b></span><u>" . mysql_error() .
"</u>");
flush();
return $fieldlist;
}
function InsertData($tablename,$fieldlist) {
##
## Retrieve data and build/run reinsert queries
##
$_temp = mssql_query("SELECT $fieldlist FROM [$tablename]");
while ($row = mssql_fetch_row($_temp)) {
#print_r($row);
$i=0;
$insertquery = " (";
foreach ($row as $fieldvalue) {
$insertquery .= "'" . addslashes($fieldvalue) .
"'";
if (++$i < sizeof($row)) $insertquery .= ",";
}
$insertquery .= ")";
## Every x rows insert data using SQL query...
$dataquery .= ($j % 75 > 0 ? ',' : '') . $insertquery;
if (++$j % 75 == 0) {
if ($dataquery) {
mysql_query("INSERT INTO " .
strtolower(ereg_replace("[[:space:]+]","",$tablename)) . "_import
VALUES" . $dataquery);
AddToErrorDisplay(mysql_error());
# AddToErrorDisplay("<br>INSERT INTO " .
strtolower($tablename) . "_import" . $dataquery);
unset($dataquery);
}
}
# echo $insertquery . "<br>\r\n";
}
# Finish off any remaining queries before ending.
if ($dataquery) {
mysql_query("INSERT INTO " .
strtolower(ereg_replace("[[:space:]+]","",$tablename)) . "_import
VALUES" . $dataquery);
AddToErrorDisplay(mysql_error());
}
AddToErrorDisplay("<span class=black> (inserted $j rows)</span>");
}
#mysql_query("DROP DATABASE acclinetest");
#mysql_query("CREATE DATABASE acclinetest");
mysql_select_db("acclinetest");
$_temp = mssql_query("exec sp_tables");
while ($row = mssql_fetch_assoc($_temp)) {
if ($row["TABLE_TYPE"] == "TABLE") $tables[] = $row;
}
foreach ($tables as $tabledata) {
mysql_query("DROP TABLE IF EXISTS " .
strtolower(ereg_replace("[[:space:]+]","",$tabledata["TABLE_NAME"])) .
"_import");
$fieldlist = CreateTable($tabledata["TABLE_NAME"]);
InsertData($tabledata["TABLE_NAME"],$fieldlist);
}
foreach ($tables as $tabledata) {
mysql_query("DROP TABLE IF EXISTS " .
strtolower(ereg_replace("[[:space:]+]","",$tabledata["TABLE_NAME"])));
mysql_query("ALTER TABLE " .
strtolower(ereg_replace("[[:space:]+]","",$tabledata["TABLE_NAME"])) .
"_import RENAME AS " .
strtolower(ereg_replace("[[:space:]+]","",$tabledata["TABLE_NAME"])));
mysql_query("DROP TABLE IF EXISTS " .
strtolower(ereg_replace("[[:space:]+]","",$tabledata["TABLE_NAME"])) .
"_import");
}
?>
</body></html>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php