简体   繁体   English

无法在 PHP 中连接到 MySQL 数据库

[英]Can't connect to MySQL database in PHP

I'm following a guide from a book and all the code looks fine, but I can't connect to my database.我正在遵循一本书的指南,所有代码看起来都很好,但我无法连接到我的数据库。 It's pretty annoying, I've got all privileges granted and what not.这很烦人,我已经获得了所有特权,但没有。 The connect file is set on for sure.连接文件是确定设置的。

Everything works up to the connection phase but I'm completely miffed, tried everyone, tried creating a new user and all that jazz.一切都在连接阶段进行,但我完全生气了,尝试了每个人,尝试创建一个新用户和所有爵士乐。 Nothing appears to be wrong with the code.代码似乎没有任何问题。 I tried using 127.0.0.1 instead of localhost.我尝试使用 127.0.0.1 而不是 localhost。 Is something a-miss?有什么不对吗?

<?php

DEFINE ('DB_USER', 'sam');
DEFINE ('DB_PASSWORD', 'whatever');
DEFINE ('DB_HOST', '127.0.0.1');
DEFINE ('DB_NAME', 'beginner');

$dbcon = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die ('Could not connect to MySQL:' .mysql_connect_error());
mysqli_set_charset($dbcon, 'utf8'); ?>

That's my external, but here's the initiating code:那是我的外部,但这是启动代码:

<?php 
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $errors = array();
    if (empty($_POST['fname'])) {
    $errors[] = 'You did not enter a first name.';
    }
else { $fn = trim($_POST['fname']);
    }
if (empty($_POST['lname'])) {
    $errors[] = 'You did not enter a last name.';
    }
else { $ln = trim($_POST['lname']);
    }

if (empty($_POST['email'])) {
    $errors[] = 'You did not enter your e-mail address.';
    }
else {$e = trim($_POST['email']);
    }

    if(!empty($_POST['psword1'])) {
    if ($_POST['psword1'] !=$_POST['psword2']) {
    $errors[] = 'Your passwords were not the same';
    }
else {$p = trim($_POST['psword1']);
    }
    }
    else { $errors[] = 'You did not enter your password.';
    }

if (empty($errors)) {
    require ('mysql_connect.php');

    $q = "INSERT INTO users (user_id, fname, lname, email, psword, registration_date)
    VALUES (' ', '$fn', '$ln', '$e', SHA1('$p'), NOW() )";
    $result = @mysqli_query ($dbcon, $q);
    if ($result) {
    header ('location: ../register-thanks.php');
    exit();
    }

    else {echo '<h2>Error!</h2>
    <p class="error">You could not be registered due to a system error. We apologise for any inconvenience.</p>';
    echo '<p>'.mysqli_error($dbcon); '<br><br>Query:'.$q.'</p>';}

    include ('footer.php');
    exit();
    }

    else {
        echo '<h2>Error!</h2>
        <p class="error">The following error(s) occurred:<br>';
        foreach ($errors as $msg) {
            echo " - $msg<br>\n";
            }
            echo '</p><h3>Please try again</h3><p><br></p>';
            }
            }
            ?>

It requires a re-direct.它需要重新定向。 don't feel it's necessary but here it is:不觉得有必要,但这里是:

<!doctype html>
<html lang=en>
<head>
<title>Registration thank you page</title>
<meta charset=utf-8>
<link rel="stylesheet" type="text/css" href="includes.css">
</head>
<body>
<div id="container">
<?php include ("header.php"); ?>
<?php include ("nav.php"); ?>
<?php include ("info-col.php"); ?>
<div id="content">
<p>The thank you page content. The thank you page content. The thank you page content.<br> 􏰀
The thank you page.The thank you page content. The thank you page content.<br>The thank 􏰀 
you page content. The thank you page. 
The thank you page content.<br>The thank you page 􏰀 content. The thank you page content. The thank you page content</p>
</div>
</div>
<?php include("footer.php"); ?>
</body>
</html>

Try removing the '@' sign, as user Jimbo says in his comment.尝试删除“@”符号,正如用户Jimbo在评论中所说。 That should stop suppressing errors.这应该停止抑制错误。 After that, enable PHP's error reporting.之后,启用PHP的错误报告。

Example:例子:

<?php

error_reporting( E_ALL );
ini_set( 'display_errors', 1 );

define( 'DB_USER', 'sam' );
define( 'DB_PASSWORD', 'whatever' );
define( 'DB_HOST', '127.0.0.1' );
define( 'DB_NAME', 'beginner' );

$dbcon = mysqli_connect( DB_HOST, DB_USER, DB_PASSWORD, DB_NAME )
    OR die ('Could not connect to MySQL:' . mysql_connect_error() );
mysqli_set_charset( $dbcon, 'utf8' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM