尝试为 mysqli 连接创建 php 类并用于查询。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21223278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to create php class for mysqli connection and use for query.
提问by Cevil
So I'm trying to create a class which can be used for connecting to mysql database. Should'nt be to advanced and it's all fun and games until i try to use my class. This is my code:
所以我正在尝试创建一个可用于连接到 mysql 数据库的类。不应该是高级的,在我尝试使用我的课程之前,这一切都很有趣和游戏。这是我的代码:
the class:
班上:
<?php
class createCon {
var $host = 'localhost';
var $user = 'root';
var $pass = '';
var $db = 'example';
var $myconn;
function connect() {
$con = mysqli_connect($this->host, $this->user, $this->pass, $this->db);
if (!$con) {
die('Could not connect to database!');
} else {
$this->myconn = $con;
echo 'Connection established!';}
return $this->myconn;
}
function close() {
mysqli_close($myconn);
echo 'Connection closed!';
}
}
?>
and this is where I try to query the database:
这是我尝试查询数据库的地方:
<?php
include 'connect.php';
$connection = new createCon();
$connection->connect();
$query = 'SELECT * FROM `nickname`';
$result = mysqli_query($connection, $query);
if($numrows = mysqli_num_rows($result)) {
echo $numrows;
while ($row = mysqli_fetch_assoc($result)) {
$dbusername = $row['nick'];
$dbpassword = $row['pass'];
echo $dbusername;
echo $dbpassword;
}}
?>
I get the following errors when I try to make a query:
当我尝试进行查询时出现以下错误:
( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, object given in C:\wamp\www\uppgift 1 kompletering\test.php on line 13
( ! ) Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\uppgift 1 kompletering\test.php on line 15
I think i kind of understand why i get the errors, however i have no clue what so ever how to fix it. Tried a million things and nothing works. I am very new to OOP and php and would greatly appriciate some advice!
我想我有点明白为什么我会收到错误,但是我不知道如何解决它。尝试了一百万件事,但没有任何效果。我对 OOP 和 php 很陌生,我会非常感谢一些建议!
回答by jameslafferty
You want to pass in $connection->myconninstead of $connection. As in:
你想传入$connection->myconn而不是$connection. 如:
$result = mysqli_query($connection->myconn, $query);
As it stands, you're passing in an instance of your class, rather than a mysqli, which is what the error messages are complaining about.
就目前而言,您正在传递类的一个实例,而不是 mysqli,这是错误消息所抱怨的。
回答by Sandun Chathuranga
in database connection class you return connection. in php class you have to catch that connection variable
在数据库连接类中,您返回连接。在 php 类中,您必须捕获该连接变量
$connection = new createCon();
$conn = connection->connect();
the you can use that $connvariable as a mysqli_query()parameter like
您可以将该$conn变量用作mysqli_query()参数,例如
$result = mysqli_query($conn, $query);
$result = mysqli_query($conn, $query);
this works for me.
这对我有用。
回答by tip2tail
I personally use mysqli_extfrom Wolf Softwarewith some minor modifications. Their website no longer has it available for download however I have placed a copy of my site for you to download. It is licensed under the GPL so you can tinker with it. It might replace your class, or it might give you the info you need to get your class working!
我个人使用mysqli_ext从狼的软件与一些小的修改。他们的网站不再提供下载,但我已经放置了我的网站的副本供您下载。它是根据 GPL 获得许可的,因此您可以对其进行修改。它可能会取代您的班级,或者它可能会为您提供让您的班级工作所需的信息!
Connection:
联系:
$sDB = new mysqli_ext($dbHost, $dbUser, $dbPass, $dbBase);
if ($sDB->connection_failed) {
/* Error */
}
Select Query:
选择查询:
$sDB->select("SELECT * FROM `albums` WHERE `listedtype` = 'PUBLIC' ORDER BY `createdon` LIMIT ?, ?", "ii", $aStart, $aAlbums);
Other Queries:
其他查询:
$sDB->execute_query("UPDATE `photos` SET `views` = `views` + 1 WHERE `picid` = ?", "s", $sPhoto);
Download from: http://www.tip2tail.co.uk/files/mysqli.class.zip
下载地址:http: //www.tip2tail.co.uk/files/mysqli.class.zip
Hope that helps!
希望有帮助!

