The following piece of code gets a page via curl, extracts some strings from it and should update some MySQL columns.
My problem is that for the following code I’m an error (see under output below).
When I copy/paste the query and place it in the SQL editor in phpmyadmin, it works perfectly. Also, if I replace the parameters $price and $stockid in my code with the actual numbers that are listed in the output, it also works. How is that even possible?
If feel like I’m missing something really stupid.
$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="";
}
Output:
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
Note: STOCK_ID is INT(11), STOCK_CURRENT_PRICE is DECIMAL(8,3)
Note2: I’m using the latest MySQL/PhpMyAdmin/PHP version.
UPDATE:
Editing the query to this:
$q2 = "UPDATE current_stocks SET STOCK_CURRENT_PRICE = '" . $price . "' WHERE STOCK_ID = '" . $stockid . "'";
mysql_query($q2) or die(mysql_error());
Removes the error message, but does not update the db.
If adding quotes doesn’t change anything, check for whitespace characters — maybe even inside
$price. Is there a hidden tab or return that is not visible in html?Try something like
$price = preg_replace("/'\s+'", '', $price);(not tested).