简体   繁体   English

使用php连接到数据库

[英]connecting to a database using php

I am very new to PHP so this is my first attempt to connect to a mysql database through a php file and I am getting this message. 我对PHP非常陌生,因此这是我第一次尝试通过php文件连接到mysql数据库,并且收到此消息。 I dont know how much anyone can help me with this or if at least someone can guide me to a right direction 我不知道有多少人可以帮助我,或者至少有人可以引导我朝正确的方向发展

Can not use : soum_email1: 无法使用:soum_email1:

And my php looks like this 和我的PHP看起来像这样

<?php
define('DB_NAME', 'soum_email1');
define('DB_USER', 'soum_email');
define('DB_PASSWORD', 'Qe232f9');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!link){
    die('could not connect:' . mysql_error());
}

$db_selct = mysql_select_db(DB_NAME, $link);

if(!$db_selected){
    die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
echo 'connection sucessful';
?>

You are assigning the mysql_select_db() function $db_selct , but then checking $db_selected (which with the code you've posted is always falsey. 您正在分配mysql_select_db()函数$db_selct ,但随后检查$db_selected (使用已发布的代码始终是错误的。

Also, link should be $link (on line 9). 另外, link应该是$link (第9行)。

Your code should be: 您的代码应为:

define('DB_NAME', 'soum_email1');
define('DB_USER', 'soum_email');
define('DB_PASSWORD', 'Qe232f9');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$link){
    die('could not connect:' . mysql_error());
}

$db_selct = mysql_select_db(DB_NAME, $link);

if(!$db_selct){
    die('Can not use : ' . DB_NAME . ':' .mysql_error());
}
echo 'connection sucessful';

You should note though that the mysql_* family of functions are now deprecated, and you should consider using MySQLi or PDO . 您应该注意,尽管现在不建议使用mysql_*系列函数,并且应该考虑使用MySQLiPDO

First of all: Please use the pdo or mysqli database like Quentin wrote in the first comment. 首先:请使用Quentin在第一条评论中写的pdo或mysqli数据库。

Further you should name your variables right, 此外,您应该正确命名变量,

$db_selct = mysql_select_db(DB_NAME, $link);

and

 if(!$db_selected){
    die('Can not use : ' . DB_NAME . ':' .mysql_error());
}

have different variable names. 具有不同的变量名。

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

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