Php 使用OOP从MySQL获取数据
我是OOP PHP的初学者。 我正在尝试创建一个类来连接、查询和获取数据 我完成了下面的编码Php 使用OOP从MySQL获取数据,php,mysql,oop,Php,Mysql,Oop,我是OOP PHP的初学者。 我正在尝试创建一个类来连接、查询和获取数据 我完成了下面的编码 class MySQL { private $set_host; private $set_username; private $set_password; private $set_database; public function __Construct($set_host, $set_username, $set_password){
class MySQL {
private $set_host;
private $set_username;
private $set_password;
private $set_database;
public function __Construct($set_host, $set_username, $set_password){
$this->host = $set_host;
$this->username = $set_username;
$this->password = $set_password;
$con= mysql_connect($this->host, $this->username, $this->password);
if(!$con){ die("Couldn't connect"); }
}
public function Database($set_database)
{
$this->database=$set_database;
mysql_select_db($this->database)or die("cannot select Dataabase");
}
public function Fetch($set_table_name){
$this->table_name=$set_table_name;
$query=mysql_query("SELECT * FROM ".$this->table_name);
$result= mysql_fetch_array($query);
}
}
$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$connect->Fetch('posts');
我想要达到的是
$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$connect->Fetch('posts');
我想用这样的格式获取数据
echo $result[0];
但我不明白为什么会这样
请帮忙
谢谢 您的
Fetch
函数只从数据库中提取一行,而您没有返回结果
该方法不是最好的,但为了实现您的目标:
public function Fetch($set_table_name){
$query=mysql_query("SELECT * FROM ".$set_table_name);
$result = array();
while ($record = mysql_fetch_array($query)) {
$result[] = $record;
}
return $result;
}
这将使每一行成为$result的一部分,但您必须按如下方式访问它:
$result = $connect->Fetch('posts');
echo $result[0]['columnName']; // for row 0;
您可以这样调用fetch函数:
$result = $connect->Fetch('posts');
echo $result[0]['columnName']; // for row 0;
或在循环中:
for ($x = 0; $x < count($result); $x++) {
echo $result[$x][0] . "<BR>"; // outputs the first column from every row
}
for($x=0;$x”;//输出每行的第一列
}
也就是说,将整个结果集提取到内存中不是一个好主意(有人会说这是一个非常糟糕的主意)
编辑:看起来你的班级还有其他问题。。。我必须跑步,但明天会回来检查,如果其他人没有纠正你,我会扩大
Edit2:好的,我们来复习一下你的课,试着解释一些事情:
class MySQL {
//declaring the private variables that you will access through $this->variable;
private $host;
private $username;
private $password;
private $database;
private $conn; // Adding the connection, more on this later.
public function __Construct($set_host, $set_username, $set_password){
$this->host = $set_host;
$this->username = $set_username;
$this->password = $set_password;
// Combining the connection & connection check using 'or'
// Notice that I removed the semi-colon after the mysql_connect function
$this->conn = mysql_connect($this->host, $this->username, $this->password)
or die("Couldn't connect");
}
public function Database($set_database)
{
$this->database=$set_database;
// Adding the connection to the function allows you to have multiple
// different connections at the same time. Without it, it would use
// the most recent connection.
mysql_select_db($this->database, $this->conn) or die("cannot select Dataabase");
}
public function Fetch($table_name){
// Adding the connection to the function and return the result object instead
return mysql_query("SELECT * FROM ".$table_name, $this->conn);
}
}
$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$posts = $connect->Fetch('posts');
if ($posts && mysql_num_rows($posts) > 0) {
echo "Here is some post data:<BR>";
while ($record = mysql_fetch_array($posts)) {
echo $record[0]; // or a quoted string column name instead of numerical index.
}
} else {
echo "No posts!";
}
classmysql{
//声明您将通过$this->variable访问的私有变量;
私人$主机;
私有$username;
私人$password;
私人数据库;
private$conn;//添加连接,稍后详细介绍。
公共函数构造($set\u主机,$set\u用户名,$set\u密码){
$this->host=$set\u host;
$this->username=$set\u username;
$this->password=$set\u password;
//使用“或”组合连接和连接检查
//注意,我删除了mysql\u connect函数后面的分号
$this->conn=mysql\u connect($this->host,$this->username,$this->password)
或者死(“无法连接”);
}
公共功能数据库($set\u数据库)
{
$this->database=$set\u database;
//将连接添加到函数允许您拥有多个
//同时使用不同的连接。如果没有它,它将使用
//最近的连接。
mysql_选择_db($this->database,$this->conn)或die(“无法选择database”);
}
公共函数获取($table\u name){
//将连接添加到函数并返回结果对象
返回mysql\u查询(“从“$table\u name,$this->conn”中选择*);
}
}
$connect=newmysql('localhost','root','');
$connect->Database('cms');
$posts=$connect->Fetch('posts');
如果($posts&&mysql\u num\u行($posts)>0){
echo“这里有一些帖子数据:
”;
而($record=mysql\u fetch\u数组($posts)){
echo$record[0];//或带引号的字符串列名,而不是数字索引。
}
}否则{
回声“没有帖子!”;
}
我希望这有助于。。。这至少能让你开始。如果您还有更多问题,您应该单独询问。这是因为$result是在Fetch函数中设置的。它在Fetch函数之外不可用 而不是这一行
$result=mysql\u fetch\u array($query)代码>,您将需要类似以下内容:返回mysql\u fetch\u数组($query)代码>
然后在你的代码里
$row = $connect->Fetch('posts');
// do something with $row
另一方面,Fetch函数只适用于返回一行的数据库查询。如果要循环查询结果中的行,必须将mysql\u query和mysql\u fetch\u数组调用分离到另一个函数中
另一方面,您不需要将此代码封装到类中。PHP已经提供了基于OOP的数据库类,称为PDO或PHP数据对象。它有一个学习曲线,但它是最佳实践,并将帮助保护您的代码不受SQL注入之类的影响
这是一个非常好的教程:我将创建一个返回对象值的函数
public function returnData($data){
return $this->$data;
}
然后,在您希望获取数据的页面中,只需启动该类并调用该类中的函数
$post->returnDate("row name here");
$用于访问对象的变量,而不是局部变量。您还应该看看MySQLi和PDO。您不应该在您的类中死()。对于Manhim关于和的评论,最好向上抛出一个异常+1。从这些开始,然后扩展它们,而不是围绕旧的mysql
扩展构建一些东西。事实上,除非您必须支持旧版本的PHP,或者需要处理遗留代码,否则您甚至不应该使用mysql
扩展。非常感谢,伙计。这就是我要找的。非常感谢:)+100喜欢:)我有很多类似的问题要问,我需要给你多少钱来回答我可笑的疯狂问题?我不想打扰你,这就是为什么:)@asker:这是你问题的答案,你应该给Fosco一个向上投票和回答点(点击复选标记图形),但请考虑使用Mysqli或PDO来完成你想要做的事情。看我和曼希姆对你的问题的评论。我是新来的,上面写着“投票需要15个声望”:(@Fosco:如果他真的想学习OOP,你应该教他如何投掷/尝试/接球,而不是在课堂上使用die
语句:-)