了解如何在 Adodb 中绑定参数和插入数据
有人告诉我使用绑定参数,以便我可以将包含引号的文本插入到我的数据库中。但是,当谈到如何执行此操作时,我很困惑,这些命令对我来说似乎很混乱。
那么,如果我有一个包含 html 的 php 字符串,我如何使用绑定参数将其插入到我的数据库中?
我想插入它,我该怎么做?
$str = '<div id="test"><a href="#">Test string in db</a></div> string content';
我被告知要使用类似的东西:
$rs = $db->Execute('select * from table where val=?', array('10'));
I was told to use bind parameters so that I could insert text into my db that had quotes in it. But, I am pretty confused when it comes to how to do this, the commands seem confusing to me.
So, if I had a php string, that contained html, how would I insert this into my DB using bind parameters?
I wanted to INSERT it, how would I do this?
$str = '<div id="test"><a href="#">Test string in db</a></div> string content';
I was told to use something like:
$rs = $db->Execute('select * from table where val=?', array('10'));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经有一段时间没有使用 ADODB 了,但我相信这应该可行,不是吗?
I haven't used ADODB for a while but I believe this should work, no?
SQL 中的 ? 用作绑定到语句的值的占位符。
执行时,ADO 正在执行(给定您的示例)
您应该能够大致如下构建插入 SQL:
传入您的值(以正确的顺序)将呈现适当的查询。
The ?'s in the SQL serve as placeholders for values that are bound to the statement.
When executed, ADO is executing (given your example)
You should be able to construct your insert SQL roughly as:
Passing in your values (in the correct order) will render the appropriate query.
使用 mysql_real_escape_string 应该也可以这样做,它会自动转义引号,然后您可以将数据插入数据库,请考虑以下示例:
现在您可以安全地使用
$str_escaped
变量将数据插入数据库。此外,它对于防止 SQL 注入攻击也很有用。Using mysql_real_escape_string should do the trick too, it escapes the quotes automatically after which you can insert data into the database, consider this example:
Now you can safely use the
$str_escaped
variable to insert data into the database. Furthermore, it is useful in preventing SQL injection attacks.改编自CodeIgniter框架:
那么你可以有一个函数:
Adapted from the CodeIgniter framework:
Then you could have a function: