Hi all

i have this code that will search a record from the database

<?php
define ('DB_NAME', 'try1');
define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
   die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
   die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$term = $_GET['fname'];

$sql = mysql_query("select * from employee where fname like '%$term%'");
$result=mysql_query($sql);
$num=mysql_num_rows($result);

//mysql_close();

$i=0;
while ($i < $num) {
//while ($row = mysql_fetch_array($sql)){
    $emp_number=mysql_result($result,$i,"emp_number");
   $fname=mysql_result($result,$i,"fname");
   $mname=mysql_result($result,$i,"mname");
   $lname=mysql_result($result,$i,"lname");
   $homeadd=mysql_result($result,$i,"homeadd");
   $emailadd=mysql_result($result,$i,"emailadd");
   $ofcnum=mysql_result($result,$i,"ofcnum");
   $mobilenum=mysql_result($result,$i,"mobilenum");
   $homenum=mysql_result($result,$i,"homenum");
   $position=mysql_result($result,$i,"position");
   $practice=mysql_result($result,$i,"practice");
   $projname=mysql_result($result,$i,"projname");
   $projmgr=mysql_result($result,$i,"projmgr");
   $teamlead=mysql_result($result,$i,"teamlead");
    }

   ?>
<html><br />
<input name="textfield" type="text" value="<?php echo $emp_number; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $fname; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $mname; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $lname; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $homeadd; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $emailadd; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $ofcnum; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $mobilenum; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $homenum; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $position; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $practice; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $projname; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $projmgr; ?>" size="40" />
<br />
<input name="textfield" type="text" value="<?php echo $teamlead; ?>" size="40" />
<input type="submit" name="edit" value="Edit" />
<input type="submit" name="update" value="Update" />
</html> 

but when i click my search button, i got this error

( ! ) Warning: mysql_query() expects parameter 1 to be string, resource given in C:\wamp\www\log-in\search.php on line 22
Call Stack
#   Time    Memory  Function    Location
1   0.0015  393280  {main}( )   ..\search.php:0
2   0.0182  402952  mysql_query ( ) ..\search.php:22

( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\wamp\www\log-in\search.php on line 23
Call Stack
#   Time    Memory  Function    Location
1   0.0015  393280  {main}( )   ..\search.php:0
2   0.0191  403200  mysql_num_rows ( )  ..\search.php:23

( ! ) Notice: Undefined variable: emp_number in C:\wamp\www\log-in\search.php on line 48 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: fname in C:\wamp\www\log-in\search.php on line 50 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: mname in C:\wamp\www\log-in\search.php on line 52 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: lname in C:\wamp\www\log-in\search.php on line 54 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: homeadd in C:\wamp\www\log-in\search.php on line 56 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: emailadd in C:\wamp\www\log-in\search.php on line 58 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: ofcnum in C:\wamp\www\log-in\search.php on line 60 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: mobilenum in C:\wamp\www\log-in\search.php on line 62 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: homenum in C:\wamp\www\log-in\search.php on line 64 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: position in C:\wamp\www\log-in\search.php on line 66 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: practice in C:\wamp\www\log-in\search.php on line 68 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: projname in C:\wamp\www\log-in\search.php on line 70 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: projmgr in C:\wamp\www\log-in\search.php on line 72 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: teamlead in C:\wamp\www\log-in\search.php on line 74 Call Stack #TimeMemoryFunctionLocation 10.0015393280{main}( )..\search.php:0 " size="40" />

Recommended Answers

All 22 Replies

"select * from `employee` where `fname` like '%$term%'" 

try this or

"select * from `employee` where `fname` like `%$term%`"

this type query

Hi arti18

i got this error when i used the second code

( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\log-in\search.php on line 23
Call Stack
#   Time    Memory  Function    Location
1   0.0011  393056  {main}( )   ..\search.php:0
2   0.0082  399376  mysql_num_rows ( )  ..\search.php:23

( ! ) Notice: Undefined variable: emp_number in C:\wamp\www\log-in\search.php on line 48 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: fname in C:\wamp\www\log-in\search.php on line 50 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: mname in C:\wamp\www\log-in\search.php on line 52 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: lname in C:\wamp\www\log-in\search.php on line 54 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: homeadd in C:\wamp\www\log-in\search.php on line 56 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: emailadd in C:\wamp\www\log-in\search.php on line 58 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: ofcnum in C:\wamp\www\log-in\search.php on line 60 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: mobilenum in C:\wamp\www\log-in\search.php on line 62 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: homenum in C:\wamp\www\log-in\search.php on line 64 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: position in C:\wamp\www\log-in\search.php on line 66 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: practice in C:\wamp\www\log-in\search.php on line 68 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: projname in C:\wamp\www\log-in\search.php on line 70 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: projmgr in C:\wamp\www\log-in\search.php on line 72 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: teamlead in C:\wamp\www\log-in\search.php on line 74 Call Stack #TimeMemoryFunctionLocation 10.0011393056{main}( )..\search.php:0 " size="40" /> 
Member Avatar for Zagga

Hi,

You are getting the errors because your database query on line 21 is failing.
I'm guessing it's because $term = $_GET['fname']; isn't defined. You can find out by adding an echo "Term is " . $term; on line 20.

You also have lots of problems with your HTML code (no <head> or <body> tags, the same name for every field etc.)

$sql = mysql_query("select * from employee where fname like'%'".$term."'%'");

try it like this.

Hi imBaCodes and Zagga

i tried the codes you gave but its still the same :(

Try to eco your $term.. and tell us the value.

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\log-in\search.php on line 23

thats the error.

Firstly, you are excecuting your query twice, like this;

$sql = mysql_query("select * from employee where fname like '%$term%'");
$result=mysql_query($sql);

Remove one of the excecutions then let us know wat the result is.

You can either use;

$sql = mysql_query("select * from employee where fname like '%$term%'");

OR

$sql = "select * from employee where fname like '%$term%'";
$result=mysql_query($sql);
Member Avatar for Zagga

As Webville312 mentions above.

Also are you sure $term is being set? $term is getting it's value from $_GET['fname'] but where is this value coming from?

thank you Webville!

now here is what i got

( ! ) Notice: Undefined variable: emp_number in C:\wamp\www\log-in\search.php on line 48 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: fname in C:\wamp\www\log-in\search.php on line 50 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: mname in C:\wamp\www\log-in\search.php on line 52 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: lname in C:\wamp\www\log-in\search.php on line 54 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: homeadd in C:\wamp\www\log-in\search.php on line 56 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: emailadd in C:\wamp\www\log-in\search.php on line 58 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: ofcnum in C:\wamp\www\log-in\search.php on line 60 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: mobilenum in C:\wamp\www\log-in\search.php on line 62 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: homenum in C:\wamp\www\log-in\search.php on line 64 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: position in C:\wamp\www\log-in\search.php on line 66 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: practice in C:\wamp\www\log-in\search.php on line 68 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: projname in C:\wamp\www\log-in\search.php on line 70 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: projmgr in C:\wamp\www\log-in\search.php on line 72 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" />
( ! ) Notice: Undefined variable: teamlead in C:\wamp\www\log-in\search.php on line 74 Call Stack #TimeMemoryFunctionLocation 10.0012393200{main}( )..\search.php:0 " size="40" /> 

Zagga,

the term is from my search-form.php and here is the code:

<title>Search</title>
<form action="search.php" method="get" />
<font face="arial">

<p>First Name: <input type="text" name="fname" /> </p>

<input type="submit" value="search" />
<body bgcolor="#3399ff">
</font>
</form>
Member Avatar for Zagga

Hi again,

the notices are due to the way you are setting the variables (between line 30 and 43).

You can fetch the sql result with:

$row = mysql_fetch_array($result);

Then retrieve the variables with:

$empnumber = $row['emp_number'];
$fname = $row['fname'];
$mname = $row['mname'];

etc.

Hi again Zagga,

it still has the same error.

Hi, pls reupload your last code, and make sure your server is running normal.

Hi Kastaji!

<?php
define ('DB_NAME', 'try1');
define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$term = $_GET['fname'];
echo "Term is " . $term;
$sql = "select * from employee where fname like '%$term%'";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
$row = mysql_fetch_array($result);


$i=0;
while ($i < $num) {

    $empnumber = $row['emp_number'];
    $fname=$row['fname'];
    $mname=$row['mname'];
    $lname=$row['lname'];
    $homeadd=$row['homeadd'];
    $emailadd=$row['emailadd'];
    $ofcnum=$row['ofcnum'];
    $mobilenum=$row['mobilenum'];
    $homenum=$row['homenum'];
    $position=$row['position'];
    $practice=$row['practice'];
    $projname=$row['projname'];
    $projmgr=$row['projmgr'];
    $teamlead=$row['teamlead'];
    }

    ?>
<html><br />
<input name="emp_number" type="text" value="<?php echo $emp_number; ?>" size="40" />
<br />
<input name="fname" type="text" value="<?php echo $fname; ?>" size="40" />
<br />
<input name="mname" type="text" value="<?php echo $mname; ?>" size="40" />
<br />
<input name="lname" type="text" value="<?php echo $lname; ?>" size="40" />
<br />
<input name="homeadd" type="text" value="<?php echo $homeadd; ?>" size="40" />
<br />
<input name="emailadd" type="text" value="<?php echo $emailadd; ?>" size="40" />
<br />
<input name="ofcnum" type="text" value="<?php echo $ofcnum; ?>" size="40" />
<br />
<input name="mobilenum" type="text" value="<?php echo $mobilenum; ?>" size="40" />
<br />
<input name="homenum" type="text" value="<?php echo $homenum; ?>" size="40" />
<br />
<input name="position" type="text" value="<?php echo $position; ?>" size="40" />
<br />
<input name="practice" type="text" value="<?php echo $practice; ?>" size="40" />
<br />
<input name="projname" type="text" value="<?php echo $projname; ?>" size="40" />
<br />
<input name="projmgr" type="text" value="<?php echo $projmgr; ?>" size="40" />
<br />
<input name="teamlead" type="text" value="<?php echo $teamlead; ?>" size="40" />
<br>
<input type="submit" name="edit" value="Edit" />
<input type="submit" name="update" value="Update" />
</br>
</html> 

there is my code.

where's your form html ?

<html>
<body>
<form>
</form>
</body>
</html>

complete your code fisrt.

echo $num;

make sure return some value not 0
before loop give if condition

if($row) {
$i=0;
while ($i < $num) {}
}

its still the same. here is the code

<?php
define ('DB_NAME', 'try1');
define ('DB_USER', 'root');
define ('DB_PASSWORD', '');
define ('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if (!$db_selected) {
    die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}

$term = $_GET['fname'];
echo "Term is " . $term;
$sql = "select * from employee where fname like '%$term%'";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
$row = mysql_fetch_array($result);
if($row) {
$i=0;
while ($i < $num) {
    $empnumber = $row['emp_number'];
    $fname=$row['fname'];
    $mname=$row['mname'];
    $lname=$row['lname'];
    $homeadd=$row['homeadd'];
    $emailadd=$row['emailadd'];
    $ofcnum=$row['ofcnum'];
    $mobilenum=$row['mobilenum'];
    $homenum=$row['homenum'];
    $position=$row['position'];
    $practice=$row['practice'];
    $projname=$row['projname'];
    $projmgr=$row['projmgr'];
    $teamlead=$row['teamlead'];
    }
 }
    ?>
<html><br />
<form>
<body>
<input name="emp_number" type="text" value="<?php echo $emp_number; ?>" size="40" />
<br />
<input name="fname" type="text" value="<?php echo $fname; ?>" size="40" />
<br />
<input name="mname" type="text" value="<?php echo $mname; ?>" size="40" />
<br />
<input name="lname" type="text" value="<?php echo $lname; ?>" size="40" />
<br />
<input name="homeadd" type="text" value="<?php echo $homeadd; ?>" size="40" />
<br />
<input name="emailadd" type="text" value="<?php echo $emailadd; ?>" size="40" />
<br />
<input name="ofcnum" type="text" value="<?php echo $ofcnum; ?>" size="40" />
<br />
<input name="mobilenum" type="text" value="<?php echo $mobilenum; ?>" size="40" />
<br />
<input name="homenum" type="text" value="<?php echo $homenum; ?>" size="40" />
<br />
<input name="position" type="text" value="<?php echo $position; ?>" size="40" />
<br />
<input name="practice" type="text" value="<?php echo $practice; ?>" size="40" />
<br />
<input name="projname" type="text" value="<?php echo $projname; ?>" size="40" />
<br />
<input name="projmgr" type="text" value="<?php echo $projmgr; ?>" size="40" />
<br />
<input name="teamlead" type="text" value="<?php echo $teamlead; ?>" size="40" />
<br>
<input type="submit" name="edit" value="Edit" />
<input type="submit" name="update" value="Update" />
</br>
</form>
</body>
</html> 

If the query returns no results, the variables are never given a value. You should initialize them with an empty string.

hi pritaeas

this is a search function. the user will search the name of the employee. where will i put an empty string? :/

Its getting an error because the $term = $_GET['fname'] is null.
You dont have a form tag.
If you want it that way try it using AJAX.

This code will put the same value's of the first emplyee into the varables $num times?
To create a list of all employee's that match the search put the mysql_fetch_array and the html-part inside the while loop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.