怎么调用mysql数据库

怎么调用mysql数据库,第1张

首先在运行输入 cmd

这样便打开了命令提示符,如下:

打开了之后在此输入 mysql -h你的IP或者花生壳的账户 -uroot -p该数据库的密码

例如:mysql -h30.158.59.78 -uroot -pabc123

mysql -htom.xicp.net -uroot -pabc123

//整理了一下代码,改了一个地方:

<?php              

include_once('../inc/conn.php')

session_start()

$name=$_POST["username"]

$password=$_POST["password"]

$sql="SELECT * FROM users WHERE username='$name' and password ='$password' "

//echo $sql

$result = mysql_query($sql)

$row = mysql_fetch_array($result)

if($row){

  $uname=$row['username']

  $pwd=$row['password']

  $qxian=$row['quanxian']

 

  if($name== $uname && $password==$pwd){ //这一行第二个等号是双等号

    $_SESSION['name'] = $uname

    $_SESSION['islogin']=1

    if($qxian==1){

      echo "<script>alert('恭喜你,登陆成功!')location.href='../main.php'</script>"

    }else{

      echo "<script>alert('恭喜你,登陆成功!')location.href='../adressList.php'</script>"

    }

  }

}else {

  echo "<script>alert('登陆失败,请检查密码是否正确!')location.href='../index.php'</script>"

}

?>

1、用CAPI连接MySQL数据库有两个步骤:

1)初始化一个连接句柄

2)建立连接

所用到的函数如下:

MYSQL *mysql_init(MYSQL *connection) // 初始化连接句柄

//成功返回MySQL结构指针,失败返回NULL

MYSQL *mysql_real_connect(MYSQL *connection,

const char *server_host,

const char *sql_user_name,

const char *sql_password,

const char *db_name,

unsigned int port_number,

const char *unix_socket_name,

unsigned int flags) //建立连接

//成功返回MySQL结构指针,失败返回NULL

以下是完整实例:

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <mysql/mysql.h>

using namespace std

void mysql_err_function(MYSQL * connection)

int main()

{

//freopen("input.txt","r",stdin)

MYSQL * connection

connection = mysql_init(NULL)

if (!connection)

{

cout <<"mysql_init failed!" <<endl

exit(-1)

}

if (!mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0))

{

cout <<"Connection To MySQL failed!" <<endl

mysql_err_function(connection)

}

cout <<"Connection To MySQL Server is Success..." <<endl

string str

getline(cin,str)

int res = 0

int affected_count = 0

while (str != "close" &&str != "" &&!res)

{

res = mysql_query(connection,str.c_str())

affected_count += mysql_affected_rows(connection)

if (res)

{

if (mysql_errno(connection))

{

cout <<"Error " <<mysql_errno(connection) <<" : "

<<mysql_error(connection) <<'\n' <<endl

break

}

}

getline(cin,str)

}

cout <<"Have affected " <<affected_count <<" rows!" <<endl

mysql_close(connection)

cout <<"Connection To MySQL Server is closed..." <<endl

return 0

}

void mysql_err_function(MYSQL * connection)

{

if (mysql_errno(connection))

{

cout <<"Error " <<mysql_errno(connection) <<" : "

<<mysql_error(connection) <<endl

exit(-1)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6472038.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-23
下一篇2023-03-23

发表评论

登录后才能评论

评论列表(0条)

    保存