shan2batman shan2batman - 4 months ago 20x
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
on line 46, referer:

And doing var_dump on $conn and $project from 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 code:

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 . "", "********", "*********");

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

$project = new projecteg($conn);

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

include_once "includes/";
$load= (int)($_POST['load'])*2;
echo '<br>';
echo '<br>';
function hashtags($dat) {
$dat1= preg_replace($regex, '<a href="hash_sys.php?tag=$1">$0</a>', $dat);
return $dat1;

function taggingsys($dat) {

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


function ff(&$s) {
//require_once 'includes/';

// $friends=array();
//$project= new \projecteg($conn);
// $totalids=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'");

$ids= ff($f);

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


//fetch user_id from update table in db and inject it to the feed query.

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


$title= stripslashes($row1['title']);
$data= stripslashes($row1['update_body']);
$data1= hashtags($data);
//$data1= taggingsys($data0);
$datemade = strftime("%B %d", strtotime($post_date));
if ($avatar!=""){
} else {
//other lengthy logic

here is the class logic:


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);
return $stmth->fetchAll();

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

how do i make my method and conn to work.


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.