为什么我的班级不工作?

时间:2016-06-01 14:47:02

标签: php mysql pdo

伙计们,我为这个问题摸不着头脑。因为,我从我的openshift日志中得到了这个错误

  

PHP致命错误:在a上调用成员函数totalids()   非对象   /var/lib/openshift/573dc0da2d5271d357000294/app-root/runtime/repo/st&com.php   在第46行,引用者:http://express-pad4u.rhcloud.com/

在st&​​amp; com.php的dbconfig.inc.php文件中对$ conn和$ project执行var_dump会给出以下消息:

  

object(PDO)#1(0){} object(projecteg)#2(4){   [ “_db”: “projecteg”:私人] => object(PDO)#1(0){} [“query”] =>空值   [ “stmth”] => NULL [“conn”] => NULL}

这是我的dbconfig.inc.php代码:

session_start();
define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST') . ':' . getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_USER', getenv('**********'));
define('DB_PASS', getenv('**********'));
define('DB_BASE', 'project');
define('DB_PORT', getenv('OPENSHIFT_MYSQL_DB_PORT'));

try {
    $conn = new PDO("mysql:host=" . getenv('OPENSHIFT_MYSQL_DB_HOST') . ";dbname=" . DB_BASE . "", "********", "*********");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch(PDOException $exc) {
    echo $exc->getMessage();
}
include 'classes.inc.php';

$project = new projecteg($conn);

这是st& com.php文件的代码:

<?php
//error_reporting(0);
include_once "includes/dbconfig.inc.php";
$status_replies="";
$status_list="";
$statusui_edit="";
$isowner="";
$is_friend="";
$statusdeletebutton='';     
$reply_delete_button="";
$load=  (int)($_POST['load'])*2;
var_dump($conn);
echo '<br>';
var_dump($project);
echo '<br>';
function hashtags($dat) {
$regex="/#+([a-zA-z0-9._-]+)/";
$dat1=  preg_replace($regex, '<a href="hash_sys.php?tag=$1">$0</a>', $dat);
return $dat1;

}
function taggingsys($dat) {
$regex="/@+([a-zA-z0-9!._-]+)/";

$dat1=  preg_replace($regex, '<a href="home.php?u=$1">$0</a>', $dat);
return $dat1;

}

function ff(&$s) {
  //$conn=""; 
  //require_once 'includes/dbconfig.inc.php';

    $output=array(); 
   // $friends=array();
 //$project= new \projecteg($conn);
  // $totalids=array();
 $verify_friend=array();
foreach ($s as $i=> $r ){

   //array_push($friends, $r);
  $r["friend_one"] == $_SESSION['uname'] ? $friends[]= $r["friend_two"] : $friends[] = $r["friend_one"]; 
  echo '<pre>';var_dump($friends);echo '</pre>';   
  $verify_friend= $project->totalids($friends[$i]);  
  /* foreach ($friends as $v) {
       echo '<br><h1>';       print_r($v); echo '</h1></br>';         

        echo '<br>';
                     //   var_dump($verify_friend);
  // array_push($totalids, $verify_friend);     

        }

    echo '<pre>'; var_dump($verify_friend); echo '</pre>'; */
   array_push($output, $verify_friend);

}
   return $output;
}

$f = array();
$stmt=  $conn->prepare("select friend_one, friend_two from friends where (friend_one=:session OR friend_two=:session) and accepted='1'");
$stmt->bindparam(":session",$_SESSION['uname']);
$stmt->execute();
$f=$stmt->fetchAll();

$ids=  ff($f);

foreach ($ids as $i=>$v){

    $id=$v[$i]['user_id'];

    //fetch user_id from update table in db and inject it to the feed query.
    $totalUpdates=$project->totalUpdates1($id,$load);   
    $total_sess_count=$project->totalupdatescount($id);

    foreach ($totalUpdates as $j=>$row1) {

                $updateid=$row1['update_id'];

                $account_name=$row1['account_name'];
                $u_id=$row1['user_id_u'];
                $author=$row1['author'];
                $post_date=$row1['time'];
                $title= stripslashes($row1['title']);
                $data= stripslashes($row1['update_body']);
              $data1=  hashtags($data);
              //$data1=  taggingsys($data0);
              $pic=$project->viewByUname($author);
              $uid=$pic['user_id'];
         $datemade = strftime("%B %d", strtotime($post_date));
           $avatar=$pic['avatar'];
          if ($avatar!=""){
          $feed_pic='user/'.$uid.'/'.$avatar;
      }  else {
          $feed_pic='img/avatardefault.png';
          }
  //other lengthy logic

这是类逻辑:

<?php

class projecteg
{
    private $_db;
    public $query;
    public $stmth;
    public $conn;

    public function __construct($conn)
    {
        $this->_db = $conn;
    }

    public function totalids($friend)
    {
        try {

            $sql2 = "select user_id from user where uname=:session or uname=:friend and activated='1'";
            $stmth = $this->_db->prepare("$sql2");//Check here syntax of $db 
            $stmth->bindValue(":session", $_SESSION['uname']);
            $stmth->bindValue(":friend", $friend);
            $stmth->execute();
            return $stmth->fetchAll();

        } catch(PDOException $exc) {
            echo $exc->getMessage();
        }
    }
}

如何使我的方法和conn工作。

1 个答案:

答案 0 :(得分:0)

您试图在函数$project中使用ff()而不将其传入或在函数内声明它,因此它不存在于该函数的范围内。做一些应该帮助你的范围的研究。但是现在,如果您想使用它,则需要将$project传递给ff()。像这样:

function ff(&$s,$project) {
...
}
$ids =  ff($f,$project);

假设您要使用已经声明的$project。如果不是,您将需要在功能内部创建一个新实例,如Patrick-Q在评论中所说。