下面的代码通过curl获取一个页面,从中提取一些字符串,应该更新一些MySQL列。
我的问题是,对于下面的代码,我是一个错误(参见下面的output )。
当我复制/粘贴查询并将其放在phpmyadmin的SQL编辑器中时,它工作得很好。此外,如果我将代码中的参数$price和$stockid替换为输出中列出的实际数字,它也能工作。这怎么可能?
如果我觉得我错过了什么很愚蠢的东西。
$q = mysql_query("SELECT STOCK_TRADE_NAME,STOCK_ID FROM current_stocks WHERE STOCK_COUNTRY_ID = 7 LIMIT 1,9");
while ($row = mysql_fetch_array($q)) {
$stockid = $row['STOCK_ID'];
$url = "http://www.some.url.com/?stock_name=" . $row['STOCK_TRADE_NAME'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$dom = new simple_html_dom;
$dom->load($output);
foreach($dom->find('span.amount') as $e) {
$price = str_replace(',','',$e->outertext);
}
foreach($dom->find('tr.even') as $f) {
if (strstr($f->outertext,'<td class="name">Open</td>')) {
$exp = explode('<td class="value">',$f->outertext);
$open = str_replace('</td>','',$exp[1]);
}
}
echo $stockid . " " . $price . "<br>";
mysql_query("UPDATE current_stocks SET STOCK_CURRENT_PRICE = $price WHERE STOCK_ID = $stockid") or die(mysql_error());
$ch="";
$dom="";
}输出
345 11.300
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 '11.300 WHERE STOCK_ID = 345' at line 1注: STOCK_ID为INT(11),STOCK_CURRENT_PRICE为十进制(8,3)
Note2:我正在使用最新的MySQL/PhpMyAdmin/PHP版本。
更新
将查询编辑为:
$q2 = "UPDATE current_stocks SET STOCK_CURRENT_PRICE = '" . $price . "' WHERE STOCK_ID = '" . $stockid . "'";
mysql_query($q2) or die(mysql_error());移除错误消息,但不更新数据库。
发布于 2011-06-12 15:10:13
如果添加引号不会改变任何事情,请检查空格字符--甚至在$price内部。是否有在html中不可见的隐藏选项卡或返回?
尝试像$price = preg_replace("/'\s+'", '', $price);这样的东西(没有测试)。
发布于 2011-06-12 15:03:32
尝试用引号封装$price:'$price';
发布于 2011-06-12 15:06:18
快速修复:将''添加到查询中:
mysql_query("UPDATE current_stocks SET STOCK_CURRENT_PRICE = '$price' WHERE STOCK_ID = '$stockid'") or die(mysql_error());更好的方法是:使用参数化查询,以便将内容和逻辑分隔开来:您不会遇到这个问题,并且免费获得可能的注射修复:)
https://stackoverflow.com/questions/6322612
复制相似问题