PHP Database connection HELP

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
user123456
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Oct 21, 2017 11:53 pm

Sun Oct 22, 2017 12:30 am

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','','empass');

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;



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";
}

/*}*/
?>



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






my controller is this

?php
require_once('./models/Employeem.php');

class Employee {

private $model;


public function __construct() {
$this->model = new Employeem();
}


public function loadViews() {


require_once('views/search.php');

if(isset($_GET['id'])) {

$employees = $this->model->getOne($_GET['id']);
require_once('views/detail.php');

}else if(isset($_GET['str'])) {

$employees = $this->model->searchE($_GET['str']);
require_once('views/search.php');

}else{
$employees = $this->model->getAll();
require_once('views/list.php');
}

require_once('views/footer.php');

}

}

I been really researching about this and I really don't know what to do

Thank you for your help

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 525
Joined: Mon Feb 22, 2016 5:52 pm

Sun Oct 22, 2017 12:45 pm

Since this was your first post, I will respond to this one.

The error is telling you that there is no such method, I see that you've tried a few things and that's good, but the problem in this instance is that you are trying to call the method the wrong way - there is a difference between procedural and object orientated methods and you seem to be trying both.

MySQLi was introduced as a stop gap to allow you to move from MySQL to PDO in a more comfortable way. As you are learning I would suggest that you learn PDO rather than try and bodge the code that you have together - the many snippets of code out on the internet can be confusing, using PDO will give you more consistency.

As to the rest of the code, it is even more ancient and invalid by modern standards. Learn to use external stylesheets, and learn how to correctly format tables and forms, although I do have reservations on the use of tables in this case.

Also use the Code button </> when posting code and copy and paste the whole error on a single line.

User avatar
benanamen
New php-forum User
New php-forum User
Posts: 23
Joined: Mon Oct 16, 2017 1:06 pm

Sun Oct 22, 2017 3:19 pm

OP, DO NOT POST DUPLICATE THREADS.

You already posted this exact thing here viewtopic.php?f=2&t=28327
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

Post Reply