OOP class inside a function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
orchid1
Forum Newbie
Posts: 11
Joined: Thu Aug 06, 2009 12:35 pm

OOP class inside a function

Post by orchid1 »

quick question here
I bet this is real simple but
I get an error that reads
"Call to a member function prepare() on a non-object"
seems like it must be a pretty common error and yet I cannot find anything on it that explains why I am getting this in my php
I tried a million things but no luck
anybody know what im doing wrong??
help please :?:

Code: Select all

 
<?php
    function upload_images($selector, $strainName, $clubId){
        if($stmt = $mysql -> prepare("INSERT INTO products (selector, labelLink, clubId) VALUES(?,?,?)")) {
         $stmt -> bind_param("sss", $selector, $strainName, $clubId);
         $stmt -> execute();
            $stmt -> close();
        }
    }
 
}
if(array_key_exists('send',$_POST)){
 
    $var1=ucfirst( $_POST['strain']);
    $var2= ucfirst($_POST['selector']);
    $var3= $displayinfo;
    
upload_images($var1, $var2, $var3);
 
}
 
?>
 
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: OOP class inside a function

Post by papa »

function upload_images($mysql, $selector, $strainName, $clubId)

Edit: Oops, edited code.
Last edited by papa on Wed Sep 02, 2009 3:34 am, edited 1 time in total.
stratbeans
Forum Newbie
Posts: 12
Joined: Sat Aug 29, 2009 2:23 am

Re: OOP class inside a function

Post by stratbeans »

PHP is not accepting $mysql as a valid object. Where is $mysql defined ?
orchid1
Forum Newbie
Posts: 11
Joined: Thu Aug 06, 2009 12:35 pm

Re: OOP class inside a function

Post by orchid1 »

$mysql is just a connection variable
i.e.

Code: Select all

 
<?php
$mysql= mysqli_connect("localhost", "MyUser", "MyPass", "Mydatabase");
    if (!$mysql) {
    echo "Cannot connect to database";
    exit;
    }
 
?>
 
thank you for the help
I'm gonna try passing $mysql to the function
and I'll get back to the function
sorry about the delay I posted at 1 am Im in the US its 8 am now and I just woke up
Thanks again Im going to try it now
orchid1
Forum Newbie
Posts: 11
Joined: Thu Aug 06, 2009 12:35 pm

Re: OOP class inside a function

Post by orchid1 »

Nope that does not work
a.k.a

Code: Select all

 
<?php
    function upload_images($selector, $strainName, $clubId, $conn){
                            if($stmt = $conn->prepare("INSERT INTO products (selector, labelLink, clubId) VALUES(?,?,?)")) {
                              $stmt -> bind_param("sss", $selector, $strainName, $clubId);
                              $stmt -> execute();
                              $stmt -> close();
                    
                            }
 
if(array_key_exists('send',$_POST)){
 
    $var1=ucfirst( $_POST['strain']);
    $var2= ucfirst($_POST['selector']);
    $var3= $displayinfo;
    
upload_images($var1, $var2, $var3, $mysql);
 
}
 
?>
 
 
any more ideas ??

im at a loss :banghead:
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: OOP class inside a function

Post by Ollie Saunders »

Put var_dump($mysql) before line 17 in your posted code. It might help if you also have error_reporting set to E_ALL.
orchid1
Forum Newbie
Posts: 11
Joined: Thu Aug 06, 2009 12:35 pm

Re: OOP class inside a function

Post by orchid1 »

ill give var_dump() a try
orchid1
Forum Newbie
Posts: 11
Joined: Thu Aug 06, 2009 12:35 pm

Re: OOP class inside a function

Post by orchid1 »

Nope doesn't work
Ive got an error in my code that I think is unrelated Ill fix it and get back
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: OOP class inside a function

Post by Luke »

var_dump wasn't supposed to fix your code. It just allows you to inspect the variable to make sure it contains what you think it contains. What happened when you did var_dump($mysql) ?
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: OOP class inside a function

Post by arjan.top »

you have to add global $mysql to access global variable
Post Reply