从curl返回的字符串未插入MySQL [英] String returned from curl not inserting in mySQL

查看:90
本文介绍了从curl返回的字符串未插入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用curl获取字符串:

I am getting a string using curl:

$ch = curl_init();    
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL,"https://$URL");  
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$result = curl_exec($ch);    
curl_close ($ch);

然后我用该字符串更新mySQL中的表:

and then I am updating a table in mySQL with that string:

$query = "UPDATE $table SET code='$result' WHERE id=$id";
$result = mysql_query($query)or die("Update query failed : " . mysql_error());

问题是更新后该列为空白。如果我回显$ result,我会在浏览器中看到该字符串,那么我知道它是从curl到达的,如果我自己键入字符串(而不是使用curl $ result),那么它也会很好地插入。我已经尝试过所有可以想到的字符串清洗组合:

problem is the column is blank after the update. If I echo $result I see the string in the browser so I know it is arriving from curl and if I type the string in myself (instead of using the curl $result) then it inserts fine as well. I've tried every combo of string cleaning I can think of:

mysql_real_escape_string(htmlentities($result));

但仍然没有骰子。

btw,返回的字符串看起来像这样:ABC * DEF * 12345ABC

btw, the string it returns looks like this: ABC*DEF*12345ABC

解决方案

按照给出的建议进行操作卷曲的结果$ res的头部有一个\n,直到我回显$ query时我才看到。我可以使用str_replace()快速修复它,以删除\n。

Following the advice given it turned out the curl $result had a \n at the head which I was not able to see until I echo'd the $query. I was able to quickly fix it with a str_replace() to remove the \n.

推荐答案

我会稍微修改一下代码到下面添加一些调试:

I would modify the code slightly to the following to add some debug:

$query = "UPDATE $table SET code='$result' WHERE id=$id";
echo "query='{$query}'<br>\n";

$result = mysql_query($query)or die("Update query failed : " . mysql_error());

echo "rows=". mysql_affected_rows() ."<br>\n";

请确保查询看起来正确,并尝试从MySQL控制台或phpmyadmin,workbench,等,并检查以这种方式运行时是否出现任何错误。从控制台运行它时,请非常仔细地查看是否在更新时生成任何警告。

Make sure that the query looks correct and attempt to run it from the MySQL console or phpmyadmin, workbench, etc and check to see if you get any errors when running it that way. When you run it from the console look very carefully to see if any warnings are generated on update.

这篇关于从curl返回的字符串未插入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆