shan2batman shan2batman - 6 months ago 25
MySQL Question

why is my class not working?

Guys i'm scratching my head for this problem. As, i get this error from my openshift log


PHP Fatal error: Call to a member function totalids() on a
non-object in
/var/lib/openshift/573dc0da2d5271d357000294/app-root/runtime/repo/st&com.php
on line 46, referer: http://express-pad4u.rhcloud.com/


And doing var_dump on $conn and $project from dbconfig.inc.php file in st&com.php gives the following message:


object(PDO)#1 (0) { } object(projecteg)#2 (4) {
["_db":"projecteg":private]=> object(PDO)#1 (0) { } ["query"]=> NULL
["stmth"]=> NULL ["conn"]=> NULL }


here is my dbconfig.inc.php code:

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);


here is the code for st&com.php file:

<?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


here is the class 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();
}
}
}


how do i make my method and conn to work.

Answer

You are trying to use $project inside a function ff() without passing it in or declaring it inside the function so it doesn't exist inside the scope of that function. Do some research on scope that should help you out. But for now you need to pass in $project to ff() if you want to use it. Something like this:

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

That is assuming you want to use the $project you have already declared. If not you will need to create a new instance inside the function like Patrick-Q said in the comments.