如何通过变量的mysql_query [英] How to pass variable in mysql_query

查看:109
本文介绍了如何通过变量的mysql_query的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图执行MySQL查询传递变量。这里是我的code

I try to execute mysql query passing variable. Here is my code

char str[100] = "My String";

mysql_query(conn, printf("INSERT INTO table VALUES %s"), str);

我得到在编译过程中警告

I get that warning during compile process

warning: passing argument 2 of ‘mysql_query’ makes pointer from integer without a cast

我错过了什么?

推荐答案

您不能这样做。 的printf()返回打印的字符数。您有的mysql_query()调用之前创建字符串:

You cannot do that. printf() returns the number of characters printed. You have to create the string before calling mysql_query():

char statement[512], *my_str = "MyString";
snprintf(statement, 512, "INSERT INTO table VALUES ('%s')", str);
mysql_query(conn, statement);

此外,在创建这些查询字符串时要小心。不要使用类似函数的sprintf()如果你不能确定得到的字符串有多长。不要过度的内存段的边界写。

Also, be careful when creating those query strings. Don't use functions like sprintf() if you cannot be sure how long the resulting string is. Don't write over the boundaries of the memory segment.

这篇关于如何通过变量的mysql_query的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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