deprecated: mysql_connect():_Deprecated: mysql_connect()

这篇博客介绍了如何在PHP中处理数据库连接和查询,从旧的MySQL扩展到新的MySQL Improved Extension(mysqli)和PDO。作者展示了如何在`connection.php`文件中设置数据库连接参数,并在`example.php`文件中执行查询。讨论了两种方法的适用场景,指出PDO可以处理多种类型的数据库。最后提醒不要忽略错误警告,因为它们有助于及时发现潜在问题。
摘要由CSDN通过智能技术生成

This warning is displayed because a new extension has appeared.

It suppouse that you still can use the old one but in some cases it´s impossible.

I show you how I do the connection with database. You need just change the values of the variables.

My connection file: connection.php

$host='IP or Server Name (usually "localhost") ';

$user='Database user';

$password='Database password';

$db='Database name';

//PHP 5.4 o earlier (DEPRECATED)

$con = mysql_connect($host,$user,$password) or exit("Connection Error");

$connection = mysql_select_db($db, $con);

//PHP 5.5 (New method)

$connection = mysqli_connect($host,$user,$password,$db);

?>

The extension changes too when performing a query.

Query File: "example.php"

//First I call for the connection

require("connection.php");

// ... Here code if you need do something ...

$query = "Here the query you are going to perform";

//QUERY PHP 5.4 o earlier (DEPRECATED)

$result = mysql_query ($query) or exit("The query could not be performed");

//QUERY PHP 5.5 (NEW EXTENSION)

$result = mysqli_query ($query) or exit("The query could not be performed");

?>

This way is using MySQL Improved Extension, but you can use PDO (PHP Data Objects).

First method can be used only with MySQL databases, but PDO can manage different types of databases.

I'm going to put an example but it´s necessary to say that I only use the first one, so please correct me if there is any error.

My PDO connection file: "PDOconnection.php"

$hostDb='mysql:host= "Here IP or Server Name";dbname="Database name" ';

$user='Database user';

$password='Database password';

$connection = new PDO($hostDb, $user, $password);

?>

Query File (PDO): "example.php"

$query = "Here the query you are going to perform";

$result=$connection->$query;

?>

To finish just say that of course you can hide the warning but it´s not a good idea because can help you in future save time if an error happens (all of us knows the theory but if you work a lot of hours sometimes... brain is not there ^^ ).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值