Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Help with some PHP

Name: OP !0vfAtk0uVQ 2012-08-31 13:33

ok /prog/ please help me out if any of you can. I NEED help, so please if you cannot or do not want to help at least keep trolling to a minimum.
So, I have this PHP object.
$connection = mysqli::__construct
(i.e. the object that tells the program I have made a connection to the database)
Now, I want to pass $con (which is an object) as an argument to a function, and then inside that function I want to call a method to the object $con, something like this:

function result($con) {
  $res = $con->query();
}
result($connection);

In this case the method is the query() method.
Well, the thing is I cannot do this, because, apparently, the $con that lives inside the function is NULL for some reason.
Even though since PHP 5 all objects that pass as arguments to functions pass by reference (or more precisely, by reference to their identifier), yet I tried to explicitly call it by reference, by putting a & in front of the argument, like this

function result(&$con) {
  $res = $con->query();
}
result($connection);

to no avail. $con is still NULL, if I do a var_dump() to it.
If I var_dump() the $connection, outside of the function, it will tell me that it's an object[etc], so I know that it initialises correctly. So I do something wrong when I pass it as an argument?..
On way to solve the problem, is to do the following

function() {
     $con = mysqli::__construct
     $res = $con->query();
}

but I do not want to create connections every time I  call the function, I want one "central" connection.
Any help will be much appreciated.

Name: >>7 2012-08-31 15:20

Ok OP, I just did a simple test where I create a mysqli connection object and pass it to a function doing query on it and it works just fine. So I have no idea what's up with yours.

<?php
function do_query($sql, $con) {
    $res = $con->query($sql);
    return $res;
}
$conx = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
var_dump($conx);
var_dump(do_query('SHOW TABLES', $conx));
?>

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List