0

I am getting 2 errors in my code:

Notice: Undefined variable: sql
AND
Fatal error: Call to a member function bind_param() on a non-object

My code is:

//if there is a plant name
if (isset($_POST['plant_name']) && $_POST['plant_name']) { 
$where .= "AND (common_name) LIKE ? OR (latin_name) LIKE ?";
}
$stmt = $conn2->prepare($sql . $where);
if (isset($_POST['plant_name']) && $_POST['plant_name']) { 
$stmt->bind_param('s', strtolower($_POST['plant_name']));
$stmt->bind_param('s', strtolower($_POST['plant_name'])."%");
}

//execute query
$stmt->execute();

// get the roses and do the query!
$sql = "SELECT * FROM rosename";

//do we have a 'where string' to add to this query
if ($where) {
$query .= $where;
}

$sql = mysql_query($query, $conn2);

I am basically trying to get someone to type in a plant in the plant_name field and then see if it is like any values from latin_name and common_name attributes in the DB.

Could somebody please help me out.

1
  • $sql = "SELECT *.... I presume this should read $query = "SELECT *.... Also put a space before the AND so that you don't end up with rosenameAND Commented Mar 16, 2012 at 20:36

2 Answers 2

2

The $stmt object is probably set to false, due to the fact the query being prepared was erroneous.

Try this:

$stmt = $conn2->prepare($sql . $where);

if (false === $stmt) {
    var_dump($conn2->errorCode();
}

You can read up on error code and prepare via the PHP docs.

ALSO, you need to move the $sql and $where initializations above the $stmt code block. That's why you are getting the undefined error.

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

Comments

0

You seem to be preparing a statement using a variable $sql that is undefined, that's what the notice error is saying. In your code we can't see any variables named $sql defined before the prepare statement where you try to use that variable.

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.