1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Call to a member function query() on a non-object in

Discussion in 'PHP' started by dmanto2two, Dec 1, 2009.

  1. #1
    I saw the previous thread on this topic but the resolution failed to solve my problem. I am and definately feel, like a total noob on php. I was toying around with user authentication and i can't seem to get queries to work. At all. It's depressing, I've tried super global, mysql_query auto connect functions, and changing up my tables. I still seem to have the same error. It blows on a mastodon scale.
    The code in question is:
    <?php
    require_once( "db_connect.php" );

    $conn = db_connect();


    $query= "select * from user where email='".$email."'";

    function register($email, $password, $fname, $lname) {



    $result =

    $conn-> mysql_query
    ($query);
    if (!$result) {
    throw new exception('Query error');
    }

    if ($result->num_rows>0) {
    throw new exception ('that email is in use, find another.');
    }

    $result = $conn->query("insert into user values
    ( '".$email."', sha1('".$password."'), '".$fname."', '".$lname."')");

    if (!$result) {
    throw new exception ('could not register you, database error,failsauce.');
    }


    return true;
    }

    ?>

    The code is intended to be a registering function in a larger try construc register code.

    thnx
    alsso srory fro teh badd sppellin
     
    dmanto2two, Dec 1, 2009 IP
  2. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Also the error was
    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\data_valid_fns.php on line 7

    Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\function_register.php on line 25
     
    dmanto2two, Dec 1, 2009 IP
  3. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the data base is
    email varchar(100) no pri null
    passwrd char(40) no null
    fname varchar(30) no null
    lname varchar(30) no null

    not sure if you guys need that but i thott i'd include it.
     
    dmanto2two, Dec 1, 2009 IP
  4. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    also the connect is good I just checked, so is the db user and password, as well as the syntax on the query i just ran it through the shell
     
    dmanto2two, Dec 1, 2009 IP
  5. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The function is used within this block of code

    <?php
    Require_once( 'login_con2.php' );

    $fname=$_POST['firstname'];
    $lname=$_POST['lastname'];
    $email=$_POST['email'];
    $pwrd=$_POST['password'];
    $pwrd2=$_POST['password2'];

    session_start();

    try {
    if (!filled_out($POST)) {

    throw new Exception
    ('The form is not filled out.' );
    }

    if (!valid_email($email)) {
    throw new Exception('Not a valid email adress');
    }

    if ($pwrd != $pwrd2) {
    throw new Exception ('passwords do not match');
    }


    register ($fname, $lname, $email, $pwrd);
    $_SESSION['valid_user'] = $fname;
    }
    catch (Exception $e) {}
    ?>
     
    dmanto2two, Dec 2, 2009 IP
  6. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    why on some places u r using $conn-> mysql_query and on some places u r using $conn->query? it looks like u r typing wrong function name
     
    lmao, Dec 2, 2009 IP
  7. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    sorry i need to amend that. i tried using a built in function when my own queries were not working
     
    dmanto2two, Dec 2, 2009 IP
  8. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    <?php
    require_once( "db_connect.php" );





    global $query;

    function register($email, $password, $fname, $lname) {

    $conn = db_connect();

    //$query=
    $result =

    $conn-> query
    ("select * from user where email='".$email."'";);
    if (!$result) {
    throw new exception('Query error');
    }

    if ($result->num_rows>0) {
    throw new exception ('that email is in use, find another.');
    }

    $result = $conn->query("insert into user values
    ( '".$email."', sha1('".$password."'), '".$fname."', '".$lname."')");

    if (!$result) {
    throw new exception ('could not register you, database error, contact me, and try again later.');
    }


    return true;
    }

    ?>


    I also attempted to make the query variable a superglobal
    it did ni work
     
    dmanto2two, Dec 2, 2009 IP
  9. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #9
    Are you sure your db_connect returns object ? It may fail somehow.
    If you want access global $query inside register() then declare global $query inside it.
    
    $query= "select * from user where email='".$email."'";
    
    function register($email, $password, $fname, $lname) {
    global $query;
    ...
    }
    
    PHP:
     
    stOK, Dec 3, 2009 IP
  10. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    no luck. Just tried still comes out with the same error. I was reading the error report again. It mentions non-object. Perhaps as in object oriented php. Do i have to give $result a constant class or definition. I kinda skimed over that part of php and moved on. if that is the problem how do it do this and where?
     
    dmanto2two, Dec 3, 2009 IP
  11. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    why dont u use mysql_real_escape_string() function with every variable u r using in any query may be the query has some syntax error becoz of some ' char that is why the query is throwing the syntax error and becoz of that the object is not being created
     
    lmao, Dec 3, 2009 IP
  12. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I tried this

    $query = "select * from users where email ='%s'";
    mysql_real_escape_string($email);

    got a failed to connect with blah user and blah password.
    is there a way to change the login in parameters for this function?
     
    dmanto2two, Dec 3, 2009 IP
  13. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\function_register.php on line 15

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\function_register.php on line 15

    Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\function_register.php on line 19

    the good news is i fixed the for each error :)
     
    dmanto2two, Dec 3, 2009 IP
  14. BrettOwnz

    BrettOwnz Member

    Messages:
    286
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #14
    The call to a member function error is caused by you not declaring the class as a new object/variable in the function_register.php document. I don't know the contents of the db_connect.php file, but this is what you need to do... Just add the below code (modify it to work for you) to the top of your document somewhere..

    
    $conn = new NAMEOFYOURCONNECTIONCLASS;
    
    PHP:
    That should help with at least that error.. Let me know!
     
    BrettOwnz, Dec 3, 2009 IP
  15. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    lol i tried webcoding because i didn't want to learn oop, but this sounds like it might work :). I'm going to suck it up and try to cram it. then code what you just said into my function. while i'm at it do i define it in the fuction, db_connect, or separate file
    also this is db_connect:

    <?php

    function db_connect() {
    $result = new mysqli('-------', '-----', ----', '------');
    if (!result) {
    throw new Exception('could not connect to database');
    } else {
    return $results;
    }
    }
    ?>
     
    dmanto2two, Dec 3, 2009 IP
  16. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #16
    20 or so pages of oophp and i think i know what i need to do. brett do i create a class around query, create an object for it then call the object where i would normally call the query variable?
    also in my reading they stressed that oophp made php more modular. how is oophp better than just encapsulating my php code in functions then calling the functions in view pages?
     
    dmanto2two, Dec 5, 2009 IP
  17. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #17
    actually would it be possible for me to create a master class with all variables, functions and objects to be used in the project and then just inherit all of the definitions and atributes? then maybe my query could be defined.
     
    dmanto2two, Dec 5, 2009 IP
  18. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #18
    alright so i created a class around db_connect.php
    literally i just put class connect{ the connecttion function}
    now i get
    Call to undefined method connect::query() in C:\xampp\htdocs\function_register.php on line 20
    does this mean my query needs to be in a class or that i made the connect class wrong
    db_connect.php is now

    <?php
    class connect {
    function db_connect() {
    $result = new mysqli('-------', '-----', ----', '------');
    if (!result) {
    throw new Exception('could not connect to database');
    } else {
    return $results;
    }
    }
    }
    ?>
    the only change to the register code was this $conn = new connect;
    do i create an object for the $conn variable that includes the function db connect if so how?
     
    dmanto2two, Dec 6, 2009 IP
  19. BrettOwnz

    BrettOwnz Member

    Messages:
    286
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #19
    This is what I would usually do....

    In db_connect.php:
    
    <?php
    class db{
    function connect(){
    $con = mysql_connect(PUTYOURINFOHERE);
    $db = mysql_select_db(DB);
    }
    function query($query){
    if(!isset($con)){
    echo "ERROR: Not connected to database; Please connect";
    } else {
    $query = mysql_query($query);
    }}
    function close(){
    if(isset($con)){
    mysql_close($con);
    } else {
    echo "ERROR: Not connected to database; Could not close DB";
    }}}
    ?> 
    
    PHP:
    Then in register.php:
    
    <?php
    include('db_connect.php');
    $db = new db;
    ?>
    
    PHP:
    To connect:
    
    <?php
    $db->connect();
    ?>
    
    PHP:
    To make a query:
    
    <?php
    $sql = "SELECT * from `members` WHERE id='1'";
    $db->query($sql);
    ?>
    
    PHP:
    To close DB:
    
    <?php
    $db->close();
    ?>
    
    PHP:
    That should work.. but I did that very quickly. Let me know if any errors occur so I can fix them.

    -Brett
     
    BrettOwnz, Dec 6, 2009 IP
  20. dmanto2two

    dmanto2two Peon

    Messages:
    56
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Well you solved the error reports.Unfortunetly the page just reders blank. I made some errors on purpose but still blank. mysql shell reads no input. I've started to completely rewrie the page from the grounds up.
    anyway
    register_function
    <?php
    require_once( "db_connect.php" );







    function register($email, $password, $fname, $lname) {

    $db = new db;
    $db->db_connect();


    $query= "select * from users where email ='.$email.'";


    $result =

    $db-> query
    ($query);
    //global $query;
    if (!$result) {
    throw new exception('Query error');
    }
    $db->close();

    if ($result->num_rows>0) {
    throw new exception ('that email is in use, find another.');
    }

    $result = $conn->query("insert into user values
    ( '".$email."', sha1('".$password."'), '".$fname."', '".$lname."')");

    if (!$result) {
    throw new exception ('could not register you, database error, contact me, and try again later.');
    }


    return true;
    }

    ?>

    db_connect is

    <?php

    class db{
    function db_connect() {
    $conn = mysql_connect ('--------', '-----', '----', '------');
    $db = mysql_select_db(DB);
    }
    function query($query) {
    if(!isset($con)) {
    echo "ERROR";
    }
    else {
    $query = mysql_query($query);
    }}
    function close() {
    if(isset($con)) {
    mysql_close($con);
    }
    else {
    echo "error";
    }}}
    ?>

    and the register page is

    <?php
    Require_once( 'login_con2.php' );

    $fname=$_POST['firstname'];
    $lname=$_POST['lastname'];
    $email=$_POST['email'];
    $pwrd=$_POST['password'];
    $pwrd2=$_POST['password2'];
    $form_vars=$_POST["form_vars"];

    session_start();

    try {
    if (!filled_out($form_vars)) {

    throw new Exception
    ('The form is not filled out.' );
    }

    if (!valid_email($email)) {
    throw new Exception('Not a valid email adress');
    }

    if ($pwrd != $pwrd2) {
    throw new Exception ('passwords do not match');
    }


    register ($fname, $lname, $email, $pwrd);
    $_SESSION['valid_user'] = $fname;

    echo "winn";
    }
    catch (Exception $e) {}
    ?>


    Brett, you are hero:)
     
    dmanto2two, Dec 6, 2009 IP