dragon7713 2014-11-30 10:27
浏览 65
已采纳

试图获取mysqli的非对象属性

I am new to mysqli object oriented procedure. Here I have made a php class for inserting data into database:

class main{
private $con;

function __construct(){
$this->db_connect();
}

private function db_connect(){
  $this->con = new mysqli('localhost', 'user_name', 'password');
  if ($this->con->connect_error) {
     echo '<br />Failed to connect database! Please try again later';
  }else{
     echo '<br />Connected Successfully!';
  }
}

private function db_select($db){
    $this->con->select_db('prefix_'.$db);
}

public function post_data($value2,$value3,$value4,$value5,$value6){
$this->db_select('database_name');
$result = $this->con->query("INSERT INTO table_name (column2,column3,column4,column5,column6) VALUES('$value2','$value3','$value4','$value5','$value6')");
if($result->affected_rows>=1){
  echo 'Data inserted successfully';
}else{
  echo 'Data could not be inserted';
}
}
}

if I include the class in a php page and trying to insert data, then this error message is showing:

Trying to get property of non-object in C:\zpanel\hostdata\dir\public_html\folder\phppage.php on line 43

There Line 43 is:

if($result->affected_rows>=1){
  • 写回答

2条回答 默认 最新

  • dongyi7966 2014-11-30 10:47
    关注

    In your case

    $result contains a boolean value yes/no.

    But $this->con holds the number of data based on your query result.

    I think you need to change your line to

    if($this->con->affected_rows>=1){
    

    Check the php manual for more information.

    http://php.net/manual/en/mysqli.affected-rows.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 关于PHP中POST获取数据的问题
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Python实现hog特征图可视化
  • ¥30 德飞莱51单片机实现C4炸弹
  • ¥50 CrossLink-LIF-MD6000 型 FPGA 的 CMOS 转 MIPI D-PHY IP 核功能使用异常
  • ¥15 proteus控制16x16LED点阵显示屏的设计