gpt4 book ai didi

PHP连接mysql服务器数据库

转载 作者:行者123 更新时间:2023-11-29 23:16:20 25 4
gpt4 key购买 nike

我只想从浏览器测试数据库连接。但我得到的是空页。我没有收到错误消息。

<?php

$con = mysqli_connect('http://ec2-54-67-69-153.us-west-1.compute.amazonaws.com/', 'root', 'root') or die(mysqli_error($con));

if ($con) {
echo "success";
} else {
echo "fail";
}

mysqli_close($con);

?>

最佳答案

**Here is a handy function that connects you to a Database, using *Object Oriented* style:**
function MySQLi_quickConnect()
{
$host = 'somewebsite.db.120327161.hostedresource.com'; //or 'http://localhost'
$username = '<YOUR USERNAME>';
$password = '<YOUR PASSWORD>';
$database = '<YOUR DATABASES NAME>';

$db = new MySQLi($host,$username,$password,$database);
$error_message = $db->connect_error;
if($error_message != NULL){die("Error:" . $error_message . "<br>" . "Occured in function
MySQLi_quickConnect");}
return $db;
}

**A simple example on how you would query the Database:**
$db = MySQLi_quickConnect(); //this is your new Database object
$sql = "<YOUR SQL STATEMENT>";
$stmt = $db->query($sql);
if(!$stmt){die('MyError : ('. $db->errno .') '. $db->error);} //kill script, show errors

关于PHP连接mysql服务器数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27765303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com