douhao5280 2012-10-26 13:57
浏览 16
已采纳

需要一个类用于创建与mysqli的数据库连接php

I need to create a class for connection to the DB. I know that hardcoding DB info is bad such as:

$user = "xxx";
$password = "yyy";
$server = "zzz";
$dbname = "name";

mysql_connect($user,$password,$server);
mysql_select_db($dbname);

That's why i use "include" such as:

include "config.php" 

which contains all the needed variables, and then I can use mysql_connect with them. But as far as i know, this is a bad practice as well. How can I use the mysqli class (extending it?) the easiest way, and ofcourse the safest possible?

Thanks for any tip

  • 写回答

4条回答 默认 最新

  • dtjw6660 2012-10-26 20:01
    关注

    All the proposed answers could be correct I guess, but for my purpose and circumstances, i created a file that includes mysqli object creation and connection to DB with some functions for escaping data. Than I just include the file in all other .php's that need DB connections and it's very easy to use and execute queries. If someone needs more info or examples let me know.

    Thanks to all

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算