mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, "localhost", "root",
"111111", "zl", 0, NULL, 0)) {
printf("Connection success\n"); res = mysql_query(&my_connection, "SELECT username, password FROM user WHERE username = ‘userName’");//问题在这里
希望这里的userName是个变量,它可以根据客户端传输的用户名的不同而不同。但是现在这么写查询语句结果不正确。请问,应该如何书写呢?谢谢啦!~
if (mysql_real_connect(&my_connection, "localhost", "root",
"111111", "zl", 0, NULL, 0)) {
printf("Connection success\n"); res = mysql_query(&my_connection, "SELECT username, password FROM user WHERE username = ‘userName’");//问题在这里
希望这里的userName是个变量,它可以根据客户端传输的用户名的不同而不同。但是现在这么写查询语句结果不正确。请问,应该如何书写呢?谢谢啦!~
$conn = mysql_connect("192.168.1.133", "root", "22582258") or
die("Could not connect: " . mysql_error()); $program_char = "utf8"; $conn.mysql_select_db("test");
//$conn.mysql_query('SET @@character_set_results = "'.$program_char.'"');
mysql_set_charset($program_char,$conn);
$charset = mysql_client_encoding($conn);
printf ("current character set is %s <br>", $charset);
$result = mysql_query("SELECT id, task_no,pack_path FROM tb_workplan where id = 1 ",$conn);
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
printf ("ID: %s <br> task_no: %s <br> pack_path :%s <BR>", $row["id"], $row[1], $row["pack_path"]);
}
$conn.mysql_free_result($result);
$conn.mysql_close();楼主可以参考上面的内容。
"SELECT username, password FROM user WHERE username ='"+
userName+"'")
userName+"'"
赋值一个变量试试
也就是关于c语言中mysql的select语句变量查询问题程序如下:
#include <stdlib.h>
#include <stdio.h>
#include "mysql/mysql.h"
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
char *userName="z";//!!!
void display_row();
int main(int argc, char *argv[]) {
int res;
mysql_init(my_connection);
if (mysql_real_connect(my_connection, "localhost", "root",
"111111", "zl", 0, NULL, 0)) {
printf("Connection success\n");
res = mysql_query(my_connection, "SELECT username,password FROM user where='userName'");//!!!
if (res) {
printf("SELECT error: %s\n", mysql_error(my_connection));
} else {
res_ptr = mysql_use_result(my_connection);
if (res_ptr) {
while ((sqlrow = mysql_fetch_row(res_ptr))) {
printf("Fetched data...\n");
display_row();
}
if (mysql_errno(my_connection)) {
printf("Retrive error: %s\n", mysql_error(my_connection));
}
}
mysql_free_result(res_ptr);
}
mysql_close(my_connection);
} else {
fprintf(stderr, "Connection failed\n");
if (mysql_errno(my_connection)) {
fprintf(stderr, "Connection error %d: %s\n",
mysql_errno(my_connection), mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
void display_row() {
unsigned int field_count;
field_count = 0;
while (field_count < mysql_field_count(my_connection)) {
printf("%s ", sqlrow[field_count]);
field_count++;
}
printf("\n");
}运行之后程序说明可以连接成功,但是却无关于z的用户名输出,请问大家这是为什么呢?应该如何修改?
SELECT username,password FROM user where='userName'userName是变量吧,你要将userName->z
变量="SELECT username, password FROM user WHERE username ='"+
userName+"'"
修改为你的语言格式
#include <stdio.h>
#include "mysql/mysql.h"
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
char *userName="z";//!!!
void display_row();
int main(int argc, char *argv[]) {
int res;
mysql_init(my_connection);
if (mysql_real_connect(my_connection, "localhost", "root",
"111111", "zl", 0, NULL, 0)) {
printf("Connection success\n");
char *sSQL;
sprintf(sSQL,"SELECT username,password FROM user where='%s'",userName);
res = mysql_query(my_connection, sSQL); // changed by ACMAIN
if (res) {
printf("SELECT error: %s\n", mysql_error(my_connection));
} else {
res_ptr = mysql_use_result(my_connection);
if (res_ptr) {
while ((sqlrow = mysql_fetch_row(res_ptr))) {
printf("Fetched data...\n");
display_row();
}
if (mysql_errno(my_connection)) {
printf("Retrive error: %s\n", mysql_error(my_connection));
}
}
mysql_free_result(res_ptr);
}
mysql_close(my_connection);
} else {
fprintf(stderr, "Connection failed\n");
if (mysql_errno(my_connection)) {
fprintf(stderr, "Connection error %d: %s\n",
mysql_errno(my_connection), mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
void display_row() {
unsigned int field_count;
field_count = 0;
while (field_count < mysql_field_count(my_connection)) {
printf("%s ", sqlrow[field_count]);
field_count++;
}
printf("\n");
}
还是不行啊
printf("SELECT error: %s\n", mysql_error(my_connection));
} else {
res_ptr = mysql_use_result(my_connection);
if (res_ptr) {
if ((sqlrow = mysql_fetch_row(res_ptr)))
printf("not record Fetched ...\n");
else {
printf("Fetched data...\n");
while ((sqlrow = mysql_fetch_row(res_ptr))) {
printf("Fetched data...\n");
display_row();
}
}
if (mysql_errno(my_connection)) {
printf("Retrive error: %s\n", mysql_error(my_connection));
}
}
mysql_free_result(res_ptr);
}
将永远不被执行啊 现在就是想知道如果查询的数据在数据不存在的时候
靠什么判断数据库中不存在这个值?
是mysql_use_result()的返回值还是mysql_fetch_row()的?