0

Os i'm starting to use mysqli, i'm getting a little confused with how it works.

So i have a function:

function verify_payment_date()
{
    $today = date("Y-m-d");

    $email = $_SESSION['email'];

    $result = $this->conn->query("SELECT * FROM user WHERE email=$email");

    while ($row = $result->fetch_object())
    {
        $next_payment_date = $row['next_payment_date'];
    }
}

To set up my connect i do this in the same class:

 private $conn;

function __construct() 
{
    $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or 
                  die('There was a problem connecting to the database.');
}

can anyone give me a helping hand here because i'm totally lost. I was basing some of the code on this website: http://www.willfitch.com/mysqli-tutorial.html

Also the error i'm getting is:

Fatal error: Call to a member function fetch() on a non-object in /home/vhosts/tradingeliteclub.com/subdomains/test/httpdocs/FES/members/classes/Mysql.php on line 52

I'm not sure where to go from here.

Thanks for your time and help.

ragebunny
  • 1,582
  • 10
  • 33
  • 53
  • 1
    Welcome to Stack Overflow! You are not doing any error checking in your query, so it's no wonder your code breaks when it fails. Do proper checking after each `mysql_query()` call. Otherwise, your script will break if the query fails. How to do this is outlined in the [manual on `mysql_query()`](http://php.net/mysql_query) or in this [reference question.](http://stackoverflow.com/questions/6198104/reference-what-is-a-perfect-code-sample-using-the-mysql-extension) – Pekka Dec 21 '11 at 16:41
  • Put the email address in quotes: `$this->conn->query("SELECT * FROM user WHERE email='$email'");`. – gen_Eric Dec 21 '11 at 16:44

1 Answers1

1

$result is not mysql result, your query failed. Try

$result = $this->conn->query("SELECT * FROM user WHERE email='$email'");
// apostrophes

instead.

$this->conn->query returns resource on success, false on fail. You can avoid the error this way

if($result) while ($row = $result->fetch_object())
{
    $next_payment_date = $row['next_payment_date'];
}
Jan Turoň
  • 31,451
  • 23
  • 125
  • 169