配置文件不与mysql数据库连接

时间:2016-05-02 05:42:30

标签: php mysql

我正在使用MAMP,这里的问题是配置文件不能连接数据库,

<?php

define("HOST","localhost");
define("USER", "root");
define("PASS", "root");
define("DB", "inserttest");

$con = mysqli_connect(HOST,USER,PASS,DB);
?>

...

<?php

include 'config.php';

$firstname = $_POST['firstname'];
$fathername = $_POST['fathername'];
$grandfathername = $_POST['grandfathername'];
$familyname = $_POST['familyname'];


$sql = mysqli_query($con,"INSERT INTO subventions (firstname,fathername,grandfathername,familyname) VALUES ('$firstname','$fathername','$grandfathername','$familyname')");

if ($sql == true) {

echo '<b> تم الإرسال </b>';
} else {
    echo '<b> فشل الإتصال </b>';
}  

mysqli_close($con);
?>

............................................... ................

3 个答案:

答案 0 :(得分:0)

您可以检查现有文件路径。 再试一次。

<?php
//include(foldername/filename);
include('config.php') or die('File path error');

答案 1 :(得分:0)

您可以使用某些功能来检查连接已建立的天气

你可以这样做

<?php

   define("HOST","localhost");
   define("USER", "root");
   define("PASS", "root");
   define("DB", "inserttest");

   $con = mysqli_connect(HOST,USER,PASS,DB);
   if(mysqli_connect_errno())
   {
      die("Some Error Occured While COnnection to database :".mysqli_connect_error());
   }
?>

希望这会有所帮助

答案 2 :(得分:0)

  

试试这个

 
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "inserttest";

// Create connection 
$conn = new mysqli($servername, $username,
$password, $dbname); 
// Check connection 
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error); } ?> 
     


include('config.php');


$firstname = $_POST['firstname'];
$fathername = $_POST['fathername'];
$grandfathername = $_POST['grandfathername'];
$familyname = $_POST['familyname'];


$sql = "INSERT INTO subventions
(firstname,fathername,grandfathername,familyname) VALUES
('$firstname','$fathername','$grandfathername','$familyname')";


if ($conn->query($sql) === TRUE) {
    echo "New record created successfully"; } else {
    echo "Error: " . $sql . "
" . $conn->error; } ?>