php入门之连接mysql数据库

时间:2025-12-03 08:31:53 php语言

php入门之连接mysql数据库

  php入门之连接mysql数据库的一个类,学习php的朋友可以参考下,希望对大家有所帮助!

  项目结构:

  运行效果;

  conn.php

  复制代码 代码如下:

  <?php

  class ConnectionMySQL{

  /pic/p>

  private $host="localhost";

  /pic/p>

  private $name="root";

  /pic/p>

  private $pass="";

  /pic/p>

  private $table="phptest";

  /pic/p>

  private $ut="utf-8";

  /pic/p>

  function __construct(){

  $this->ut=$ut;

  $this->connect();

  }

  /pic/p>

  function connect(){

  $link=mysql_connect($this->host,$this->name,$this->pass) or die ($this->error());

  mysql_select_db($this->table,$link) or die("没该数据库:".$this->table);

  mysql_query("SET NAMES '$this->ut'");

  }

  function query($sql, $type = '') {

  if(!($query = mysql_query($sql))) $this->show('Say:', $sql);

  return $query;

  }

  function show($message = '', $sql = '') {

  if(!$sql) echo $message;

  else echo $message.'<br>'.$sql;

  }

  function affected_rows() {

  return mysql_affected_rows();

  }

  function result($query, $row) {

  return mysql_result($query, $row);

  }

  function num_rows($query) {

  return @mysql_num_rows($query);

  }

  function num_fields($query) {

  return mysql_num_fields($query);

  }

  function free_result($query) {

  return mysql_free_result($query);

  }

  function insert_id() {

  return mysql_insert_id();

  }

  function fetch_row($query) {

  return mysql_fetch_row($query);

  }

  function version() {

  return mysql_get_server_info();

  }

  function close() {

  return mysql_close();

  }

  /pic/p>

  function fn_insert($table,$name,$value){

  $this->query("insert into $table ($name) value ($value)");

  }

  /pic/p>

  function fn_delete($table,$id,$value){

  $this->query("delete from $table where $id=$value");

  echo "id为". $id." 的记录被成功删除!";

  }

  }

  $db = new ConnectionMySQL();

  $db->fn_insert('test','id,name,sex',"'','hongtenzone','M'");

  $db->fn_delete('test', 'id', 1);

  ?>

【php入门之连接mysql数据库】相关文章:

php连接mysql数据库代码09-13

php基础之连接mysql数据库和查询数据02-24

PHP对数据库MySQL的连接操作12-02

PHP脚本测试连接MySQL数据库12-07

如何在PHP中连接MySQL数据库07-11

PHP与MYSql连接与查询11-30

PHP连接局域网MYSQL数据库的技巧11-23

PHP连接MYSQL数据库的3种常用方法03-01

php网站如何连接到远程mysql数据库11-28