0

I have the following connection file...

<?php
session_start(); 

// Create new mysql connection object
$DBConnect = @new mysqli("localhost","root","","Ladle");
?>

which I include in the following script...

<?php ob_start() ?>
<?php session_start() ?>
<?php
//Connects to the database
include("inc_LadleDB.php");
$this->DBConnect = $DBConnect;

// Get the e-mail address entered
$email = $_POST['user_email'];

$sql = $this->DBConnect->mysql_query("SELECT * FROM tblEmployees WHERE fldEmail='".
                            $email."'") or die(mysql_error());

$Result = mysql_fetch_assoc($sql);

//validate email fo valid maine account
if($Result)
{ ...

I tried running this but got an error of "Using $this when not in object context"; I just need to perform simple queries and don't want to deal with OO PHP but I have no choice now that mysql is deprecated. How can I reference the included connection file to run the SELECT query in this no OO file?

4
  • 1
    mysql_* != mysqli_*, you can find solution in mysqli in example #1-> Procedural style Commented Nov 14, 2013 at 22:10
  • You can't mix calls to mysql_*() with calls to mysqli_*(). Use mysqli alone, since mysql is deprecated. Commented Nov 14, 2013 at 22:13
  • Question to OP: Doesn't the mysqli documentation show how to call all the functions procedurally? Where did you get the idea that you HAVE to use OO to use them? Commented Nov 14, 2013 at 22:29
  • @Barmar my bad, I didn't notice the manual had a section on OO and procedural calls Commented Nov 14, 2013 at 22:35

3 Answers 3

2

$this is a keyword reserved for Classes in PHP. Since you're running the queries in a procedural manner, there's no need for the $this-> prerequisite.

You can easily use the mysqli object inside your code as follows:

$DBConnect = new mysqli("localhost","root","","Ladle");

// Get the e-mail address entered
$email = $_POST['user_email'];

$sql = $DBConnect->query("SELECT * FROM tblEmployees WHERE fldEmail='".
                            $email."'");

$Result = $DBConnect->fetch_assoc($sql);

//validate email fo valid maine account
if($Result)
{ ...

For reference, using @ to surpass errors is a bad habit to get into. You should really handle errors using try { } catch() { } blocks.

Secondly, your code is vulnerable to SQL injection - so it might be wise for you to look into Prepared Statements.

Sign up to request clarification or add additional context in comments.

4 Comments

To the phantom downvoter, please explain as well, rather than just recklessly clicking the downvote button. This also applies to Barmar's answer...
Quite contrary. try { } catch() { } blocks should NOT be used to handle an error message
So you'd recommend surpassing them over try { } catch() { }?
@BenM thank you, this clarifies quite a lot, I wasn't able to find anything that explained this elsewhere!
1

Just because mysqli provides an OO interface, it doesn't mean that your code has to be written in OO style. You can just use ordinary functions and variables in your application.

$email = $DBConnect->real_escape_string($_POST['user_email']);

$sql = $DBConnect->query("SELECT * FROM tblEmployees WHERE fldEmail='".
                        $email."'") or die($DBConnect->error);

$Result = $sql->fetch_assoc();

Comments

0

You're mixing OO and functional calls, which is causing you confusion. The mysql_* functions are not the same as mysqli_* functions, so everywhere you have mysql_* is wrong.

Also, don't do this: DBConnect->mysql_query. DBConnect is an instance of MySQLi, so just call DBConnect->query.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.