php静态页面需要mysql么,Mysql-php使用静态或非静态连接?

新手开发者分享在使用PHP面向对象操作mysqli时,遇到非静态连接引发内部服务器错误的问题。文章详细解释了静态连接与非静态连接的区别,并提供了解决方案,强调了`$this->connection`的正确用法。
摘要由CSDN通过智能技术生成

我是这个主题的新手.我只是想连接我的数据库并获取数据.当使用静态连接时,它正在工作但不使用非静态.正如我所说,我对Php知之甚少,可能会遗漏一些简单的东西.

尝试获取非静态时出错由于发生内部服务器错误,无法显示页面.

我的代码

> <?php

class DB_Connect extends mysqli{

// protected static $connection;//working

protected $connection; / not working

function __construct() {

}

function __destruct() {

}

public function connect() {

if(!isset($this->$connection)) {

$config = parse_ini_file('./configOop.ini');

$this->$connection = new mysqli($config['dbhost'],$config['username'],$config['password'],$config['dbname']);

}

else{}

return $this->$connection;

/*

// using this part for static connection object, working

if(!isset(self::$connection)) {

$config = parse_ini_file('./configOop.ini');

self::$connection = new mysqli($config['dbhost'],$config['username'],$config['password'],$config['dbname']);

}

else{}

return self::$connection;

*/

}

// Closing database connection

public function close() {

// mysql_close();

}

}

?>

//

// error_reporting(0);

$db=new DB_Connect();

$dbConn=$db->connect();

if($result =$dbConn->query("Select * from cities")or die($dbConn->error)){

if($count=$result->num_rows){

while($row = $result->fetch_object())

{

echo $row->idcities;

}

}

}

?>

解决方法:

一个小小的错误:$this-> $connection应该是$this->连接.

PHP不需要第二个$,因为它已经知道(因为 – >)你指的是一个属性.如果你像你那样添加$,你基本上得到局部变量$connection的值,并使用该值作为属性名称.

标签:php,oop,mysqli

来源: https://codeday.me/bug/20190829/1762052.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值