gpt4 book ai didi

php - 无法使用 PDO 连接到 MySQL 服务器

转载 作者:可可西里 更新时间:2023-11-01 06:29:13 25 4
gpt4 key购买 nike

<分区>

我有一个 PHP 脚本,用于连接到 MySQL 数据库。通过 mysql_connect 的连接工作完美,但在尝试使用 PDO 时出现以下错误:

SQLSTATE[HY000] [2005] Unknown MySQL server host 'hostname' (3)

我用来连接的代码如下:

    <?php  
ini_set('display_errors', 1);
error_reporting(E_ALL);

$hostname_localhost ="hostname";
$database_localhost ="dbname";
$username_localhost ="user";
$password_localhost ="pass";
$user = $_GET['user'];
$pass = $_GET['pass'];

try{
$dbh = new PDO("mysql:host=$hostname_localhost;dbname=$database_localhost",$username_localhost,$password_localhost);
echo 'Connected to DB';
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $dbh->prepare("SELECT check_user_company(:user,:pass)");
$stmt = $dbh->bindParam(':user',$user,PDO::PARAM_STR, 16);
$stmt = $dbh->bindParam(':pass',$pass,PDO::PARAM_STR, 32);

$stmt->execute();

$result = $stmt->fetchAll();

foreach($result as $row)
{
echo $row['company_id'].'<br />';
}

$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>

提前致谢

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