user123356 Posted October 21, 2017 Share Posted October 21, 2017 (edited) Hi! I'm new at php and I need a help with this code I need to Search an employee from a Database that I already have I have the Search Form and I write the name and the lastname and the details of the employee appear, But I need help. I already have the search, but I tells me when I call the query that I have a fatal error Trying to get property of non-object in in this part of the code $count = $query -> num_rows;and this Fatal error: Call to undefined method EmployeeModel::searchEmployees() in here $employees = $this->model->searchE($_GET['str']); require_once('views/search.php'); where I need to call this function in search.php I'm gonna leave my search.php I need a big help here I'm gonna be really thankfull <form method="get" action="index.php" name="searchform" id="searchform"> <input type="text" name="str" id="str"> <input type="submit" name="submit" id="submit" value="Search"> </form> <?php /*class SearchE {*/ $user = "root"; $password = ""; $host = "localhost"; $dbase = "employees_assign"; $table = "tbl_employees"; $search_term= isset($_GET['str']) ? $_GET['str'] : ''; /*mysqli_connect($host,$user,$password); @mysqli_select_db($dbase) or die("Unable to select database");*/ $connect = mysqli_connect('localhost','root','','employees_assign'); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql = "SELECT * FROM tbl_employees WHERE emp_fname LIKE '%$search_term%' OR emp_lnames LIKE '%$search_term%'"; //$query = mysqli_query($connect,$sql); $query = $connect ->query($sql); //$count= mysqli_num_rows(mysqli_result $query); $count = $query -> num_rows; //$count=$query->num_rows; /*if ($query === false) { error_log($connect->error); die("something failed."); }*/ if ($count == 0) { echo "<fieldset><b>No Results Found for Search Query '$search_term'</b></fieldset>"; } else { print "<table border=1>\n"; while ($row = mysqli_fetch_array($query)){ $emp_fname= $row['emp_fname']; $emp_lname= $row['emp_lname']; print "<tr>\n"; print "</td>\n"; print "\t<td>\n"; print "<font face=arial size=4/><div align=center>$emp_fname</div></font>"; print "</td>\n"; print "\t<td>\n"; echo "<font face=arial size=4/>$emp_lname</font>"; print "</td>\n"; print "</tr>\n"; } print "</table>\n"; } /*}*/ ?> Edited October 21, 2017 by requinix fixing messed up bbcode Quote Link to comment Share on other sites More sharing options...
requinix Posted October 21, 2017 Share Posted October 21, 2017 /*if ($query === false) { error_log($connect->error); die("something failed."); }*/Make that work, try again, and check your error log. As for the other error, that's something completely unrelated to the code you've shown. Quote Link to comment Share on other sites More sharing options...
user123356 Posted October 22, 2017 Author Share Posted October 22, 2017 It tells me that ) Notice: Trying to get property of non-object in C:\wamp64\www\employees_done\views\emp_search.php on line 41Call Stack#TimeMemoryFunctionLocation10.0016235768{main}( )...\index.php:020.0099274384Employee->loadViews( )...\index.php:830.0131285872require_once( p' )...\Employee.php:19 Quote Link to comment Share on other sites More sharing options...
requinix Posted October 22, 2017 Share Posted October 22, 2017 Well that doesn't make sense: presumably the error message is talking about error_log($connect->error);so $connect is the "non-object" mentioned. But if that were the case then $query = $connect ->query($sql);that line would have failed catastrophically and everything after wouldn't even happen. Therefore there's a misunderstanding somewhere. $connect = mysqli_connect('localhost','root','','employees_assign'); echo "connect is "; var_dump($connect);What does that output? Then, $query = $connect ->query($sql); echo "query is "; var_dump($query);what about that? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.