简体   繁体   中英

Having problems connecting to database PHP

define('DB_HOST', 'localhost');
define('DB_DBNAME', '*');
define('DB_USER', '*');
define('DB_PASSWORD', '*');
//conection:
$link = mysqli_connect("DB_HOST","DB_USER","DB_PASSWORD","DB_DBNAME") or die("Error " . mysqli_error($link));

This is my code, and I am getting this errors:

PHP Warning: mysqli_connect(): (HY000/2005): Unknown MySQL server host 'DB_HOST'
PHP Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given

I am a beginner in PHP so I don't have much experience in working with databases. I don't know what to do, but the defines are right.

使用常量时不要引用它们:

$link = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DBNAME)

Your DB_NAME will have an actual name also

Identifiers are converted to Unicode internally. They may contain these characters: Permitted characters in unquoted identifiers: ASCII: [0-9,az,AZ$_] (basic Latin letters, digits 0-9, dollar, underscore) Extended: U+0080 .. U+FFFF

Replace

$link = mysqli_connect("DB_HOST","DB_USER","DB_PASSWORD","DB_DBNAME") 
or die("Error " . mysqli_error($link));

With

$link = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DBNAME) 
or die("Error connecting to database");

You cannot use mysqli_error() in the die of mysqli_connect because $link will be false in case when connection cannot be established to database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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