gpt4 book ai didi

php - php数据库连接错误

转载 作者:行者123 更新时间:2023-11-30 00:54:35 25 4
gpt4 key购买 nike

我收到错误:未选择数据库这是代码:

    <?php
$host = "localhost";
$user = "root";
$password = "";
$database_name = "Student";

mysql_connect($host, $user, $password);
mysql_select_db($database_name);
?>

此代码适用于 php-connect.php 类和表单:

    <!DOCTYPE html>
<html>
<body>

<?php
//load database connection
include("php-connect.php");
$id = "1";
$name = "Elena";
$city = "Lahore";

//Command to insert into table
$query = "INSERT INTO studentdata (id,name,city) VALUES ('$id','$name','$city')";

//run the query to insert the person.
$result = mysql_query($query) OR die(mysql_error());

//let them know the person has been added.
echo "Data successfully inserted into the database table ... ";
?>

</body>
</html>

这是表单的代码..我已经尝试了很多方法来修复此错误,但它不起作用。我的数据库有问题吗?

最佳答案

试试这个...

<?php
$host = "localhost";
$user = "root";
$password = "";
$database_name = "Student";

$mLink = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db($database_name , $mLink);

另一个文件

<?php

require 'php-connect.php';


//...... etc..

但是,请先阅读以下内容:

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_select_db() PDO::__construct() (part of dsn)

http://br.php.net/mysql_select_db

关于php - php数据库连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693756/

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