gpt4 book ai didi

php - 无法连接php和mysql

转载 作者:行者123 更新时间:2023-11-30 21:49:36 25 4
gpt4 key购买 nike

当我用 postman 尝试 php 脚本时,我得到这个错误:Unable to connect

凭证是正确的。

这是我的代码:

dbDetails.php

<?php
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_DATABASE','prova');

$con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE) or die('Unable to Connect');

上传.php:

<?php
require_once 'dbDetails.php';

//response array
$response = array();


if($_SERVER['REQUEST_METHOD']=='POST'){

if(isset($_POST['url']) and isset($_POST['name'])){

$con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or die('Unable to Connect...');

//getting name from the request
$url = $_POST['url'];
$name = $_POST['name'];

try{
$sql = "INSERT INTO `pdfs` (`id`, `url`, `name`) VALUES (NULL, '$url', '$name');";

if(mysqli_query($con,$sql)){

//filling response array with values
$response['error'] = false;
$response['url'] = $url;
$response['name'] = $name;
}
//if some error occurred
}catch(Exception $e){
$response['error']=true;
$response['message']=$e->getMessage();
}
//closing the connection
mysqli_close($con);
}else{
$response['error']=true;
$response['message']='Please choose a file';
}

//displaying the response
echo json_encode($response);
}

有人可以帮助我吗?

谢谢!

最佳答案

请按如下方式使用:-

$con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

关于php - 无法连接php和mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47830708/

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