mysqli链接数据库

时间:2022-12-28 23:45:08

<?php


$uid = $_GET['uid'];

$host = 'localhost';
$database = 'test';
$username = 'root';
$password = '123456789';
$selectName = 'harry';//要查找的用户名,一般是用户输入的信息
$insertName = 'testname';

$connection = mysqli_connect($host, $username, $password,$database);//连接到数据库
if (!$connection) {
    die("could not connect to the database.\n" . mysqli_content_error());//诊断连接错误
}

$query = "select * from tp_users where uid =" . $uid;//构建查询语句
$result = mysqli_query($connection,$query);//执行查询

if (!$result) {
    die("could not to the database\n" . mysqli_content_error());
}

while ($row = mysqli_fetch_row($result)) {
    print_r($row);
}

//添加记录
// $insertSql = "insert into tp_users(name, age) values('$insertName', 18)";
// $result = mysqli_query($connection,$insertSql);
// echo $result . "\n";

//更新记录
// $updateSql = "update user set uid = 3 where name=2";
// $result = mysqli_query($connection,$updateSql);
// echo $result . "\n";

//删除记录
// $deleteSql = "delete from user where uid = 3";
// $result = mysqli_query($connection,$deleteSql);
// echo $result . "\n";

mysqli_close($connection);//关闭连接

?>