{"id":3003,"date":"2014-03-13T11:18:04","date_gmt":"2014-03-13T11:18:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/13\/errors-in-prepared-statement-undefined-variable-sql-and-fatal-error-call-to-a-member-function-bind_param-collection-of-common-programming-errors\/"},"modified":"2014-03-13T11:18:04","modified_gmt":"2014-03-13T11:18:04","slug":"errors-in-prepared-statement-undefined-variable-sql-and-fatal-error-call-to-a-member-function-bind_param-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/13\/errors-in-prepared-statement-undefined-variable-sql-and-fatal-error-call-to-a-member-function-bind_param-collection-of-common-programming-errors\/","title":{"rendered":"Errors in prepared statement &#8211; Undefined variable: sql and Fatal error: Call to a member function bind_param()-Collection of common programming errors"},"content":{"rendered":"<p>I am getting 2 errors in my code:<\/p>\n<pre><code>Notice: Undefined variable: sql\nAND\nFatal error: Call to a member function bind_param() on a non-object\n<\/code><\/pre>\n<p>My code is:<\/p>\n<pre><code>\/\/if there is a plant name\nif (isset($_POST['plant_name']) &amp;&amp; $_POST['plant_name']) { \n$where .= \"AND (common_name) LIKE ? OR (latin_name) LIKE ?\";\n}\n$stmt = $conn2-&gt;prepare($sql . $where);\nif (isset($_POST['plant_name']) &amp;&amp; $_POST['plant_name']) { \n$stmt-&gt;bind_param('s', strtolower($_POST['plant_name']));\n$stmt-&gt;bind_param('s', strtolower($_POST['plant_name']).\"%\");\n}\n\n\/\/execute query\n$stmt-&gt;execute();\n\n\/\/ get the roses and do the query!\n$sql = \"SELECT * FROM rosename\";\n\n\/\/do we have a 'where string' to add to this query\nif ($where) {\n$query .= $where;\n}\n\n$sql = mysql_query($query, $conn2);\n<\/code><\/pre>\n<p>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.<\/p>\n<p>Could somebody please help me out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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[&#8216;plant_name&#8217;]) &amp;&amp; $_POST[&#8216;plant_name&#8217;]) { $where .= &#8220;AND (common_name) LIKE ? OR (latin_name) LIKE ?&#8221;; } $stmt = $conn2-&gt;prepare($sql . $where); if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3003","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3003"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3003\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}