我在我们网站的管理员端安装了一个 PHP 脚本,它应该允许我们运行 SQL 查询。但是,在我们的主机公司的 OPS 页面上通过 mysqladmin 运行它时,我没有得到相同的结果。
当我提交时:
SELECT orders_id FROM `orders_status_history` WHERE `comments` LIKE '%12345%'
我在 mysqladmin 中得到一个结果(我的测试记录)。所以它是成功的。
但是,当我通过 PHP 程序提交它时,我得到:
MySQL error 1064: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near '\'%12345%\'' at line 1
While executing:
SELECT orders_id FROM `orders_status_history` WHERE `comments` LIKE \'%12345%\'
我假设通过 PHP 提交时语法是不同的,但我一辈子都无法弄清楚它应该是什么。我尝试在单引号前加上斜线。我试过双引号。我花了几个小时在网上冲浪。我已经尝试了很多东西,我什至不能再把它们弄直了。我假设这很简单。谁能指出我正确的方向?
这是来自 php 程序的代码。用户将 SQL 查询粘贴到文本区域并点击 SEND 按钮。同样,完全相同的查询在 mysqladmin 中有效,但在使用此 PHP 程序时无效:
<?php
/*
$Id: sql_interface.php,v 1.00 2004/08/13 00:28:44 draven Exp $
*/
require('includes/application_top.php');
$text_heading = INITIAL_TITLE;
function sqlquery($query) {
$result = mysql_query($query);
global $query_result;
if (mysql_errno()) {
$query_result = "MySQL error ".mysql_errno().": ".mysql_error()."\n\nWhile executing:\n\n$query\n------------------------------------------------------------------------------------------\n\n";
} else {
$query_result = "Your query was successful!\nRows Affected: " . mysql_affected_rows();
}
return $result;
}
$action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');
if (isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'process')) {
sqlquery($HTTP_POST_VARS['query_entry']);
$text_heading = POST_QUERY_TITLE;
$tryagain = TRY_AGAIN_TEXT;
}
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo HEADING_TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr>
<td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
</table></td>
<!-- body_text //-->
<td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2"><?php echo tep_draw_form('sql_interface', 'sql_interface.php', 'post') . tep_draw_hidden_field('action', 'process'); ?>
<tr>
<td><table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td class="pageHeading" colspan="3"><?php echo HEADING_TITLE; ?></td>
</tr>
</table></td>
</tr>
<tr>
<td><table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr>
<td class="main" colspan="3"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
</tr>
<tr>
<td class="main" colspan="2"><?php echo '<b>' . $text_heading . ':</b>'; ?></td>
<td class="main" align="right" colspan="1"><i><?php echo $tryagain; ?></i></td>
</tr>
<tr>
<td class="main" colspan="3"><?php echo tep_draw_textarea_field('query_entry', '', 137, 30, $query_result, '', false); ?></td>
</tr>
<tr>
<td class="main" colspan="3"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
</tr>
<tr>
<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
<td colspan="2"align="right"><?php echo tep_image_submit('button_send.gif', IMAGE_BUTTON_EXECUTE_SQL) . tep_draw_separator('pixel_trans.gif', '10', '1'); ?></form></td>
</tr>
<tr>
<td class="smallText" colspan="3"> </td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table></td>
<!-- body_text_eof //-->
</tr>
</table>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>