PHP和mysql连接

编程入门 行业动态 更新时间:2024-10-27 14:34:29
本文介绍了PHP和mysql连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以在PHP类文件中创建连接并在类中的所有不同方法中使用它?我试图在构造函数中打开一个连接,我得到一个错误,当我得到关闭连接方法说,我在mysql_close()语句中提供的参数不是一个有效的MYSQL链接的源。 p>

更新:Ok我想出了一个变量拼写错误。

解决方案

这是完全可能的,你只需要使数据库链接一个类成员:

class MyDBClass { var $ sth; function __construct($ host,$ user,$ pass){ $ this-> sth = mysql_connect($ host,$ user,$ pass); } function close(){ mysql_close($ this-> sth); } }

Is it possible to create a connection in a PHP class file and use it in all of the different methods in the class? I am trying to open a connection in the constructor and i get an error when i get to the close connection method saying that the argument that I've provided in the mysql_close() statement isn't a valid MYSQL-Link souce.

Update: Ok I figured it out I had a variable misspelled.

解决方案

It is entirely possible, you just need to make the database link a class member:

class MyDBClass { var $sth; function __construct($host, $user, $pass) { $this->sth = mysql_connect($host, $user, $pass); } function close() { mysql_close($this->sth); } }

更多推荐

PHP和mysql连接

本文发布于:2023-11-22 03:05:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!