찾다
php教程php手册c连接mysql的实例

mysql

今天给新来的同事做mysql培训,用到的一个例子:
连接数据库test 用户名root密码abc
在里面有一个username的表,describe username结果如下
mysql> describe username;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| username | varchar(32) | | PRI | | |
+----------+-------------+------+-----+---------+-------+

mysql> select * from username;
+------+----------+
| id | username |
+------+----------+
| 1 | sfs |
+------+----------+
1 row in set (0.00 sec)


本文目的查找执行select id from username这个语句并打印结果
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MYSQL mysql;
main()
{
   char host[32]="localhost";
   char user[32]="root";
   char passwd[32]="abc";
   char dbname[32]="test";   
   
   if( mysql_init(&mysql) == NULL )
   {
      syslog(LOG_USER|LOG_INFO,"inital mysql handle errorn");
      return 1;
   }
   if (mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)
   {
      syslog(LOG_USER|LOG_INFO, "Failed to connect to database: Error: %sn",mysql_error(&mysql));
      return 1;
   }
   else syslog(LOG_USER|LOG_INFO, "connect to database: n");
   find_ps();   
   db_close();
   return 0;
}
int db_close()
{
        mysql_close(&mysql);
         return 0;
}


int  find_ps ()
{
   MYSQL_ROW m_row;
   MYSQL_RES *m_res;
   char sql[1024],username[32];
   int res=1;
   int *id;
   sprintf(sql,"select id from username");
   if(mysql_query(&mysql,sql) != 0)
   {
      syslog(LOG_USER|LOG_INFO, "select ps_info Error: %sn",mysql_error(&mysql));
      return res;
   }
   m_res = mysql_store_result(&mysql);
   if(m_res==NULL)
   {
      syslog(LOG_USER|LOG_INFO, "select username Error: %sn",mysql_error(&mysql));
      res = 3;
      return res;
   }
   if(m_row = mysql_fetch_row(m_res))
   {
      printf("m_row=%dn",atoi(m_row[0]));
      res = 0;      
   }
   mysql_free_result(m_res);
   return res;
}

编译命令
gcc -g connect_db.c -L/usr/lib/mysql -lmysqlclient -lz
注意:
如果/tmp/ccTGmMS21.o: In function `main':
/tmp/ccTGmMS21.o(.text+0x11): undefined reference to `mysql_init'
那么参数增加-L/usr/lib/mysql -lmysqlclient
如果
usr/lib/mysql/libmysqlclient.a(my_compress.o): In function `my_uncompress':
my_compress.o(.text+0xaa): undefined reference to `uncompress'
那么增加-lz参数



성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.