Forum Moderators: coopster

Message Too Old, No Replies

PHP5 codes that search MYSQL database

I need a code that can serach mysql database and display the result in rows

         

okonjiaustin

5:43 am on Feb 5, 2008 (gmt 0)

10+ Year Member



I have been using the codes below for searching mysql databases for quite sometime and it had benn working fine for me, but now (may be due to newer versions of php) they no longer work. Can someone please either correct them or send me new codes for the newer versions of php(php5).

I call this code, search.php, while the one after this is called view.php

The table is personnel with columns (id-auti_increment, firstname,lastname,nick,email,salary)

<HTML>
<?php
if ($searchstring)
{
$sql="SELECT * FROM personnel WHERE $searchtype LIKE '%$searchstring%' ORDER BY firstname ASC";
$db = mysql_connect("localhost", "root", "");
mysql_select_db("learndb",$db);
$result = mysql_query($sql,$db);
echo "<TABLE BORDER=2>";
echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Options</B></TR>";
while ($myrow = mysql_fetch_array($result))
{
echo "<TR><TD>".$myrow["firstname"]."
".$myrow["nick"];
echo "<TD><a href=\"view.php?id=".$myrow[id]."\">View</a>";
}
echo "</TABLE>";
}
else
{
?>
<form method="POST" action="<?php $PHP_SELF?>">
<table border="2" cellspacing="2">
<tr><td>Insert you search string here</td>
<td>Search type</td></tr>
<tr>
<td><input type="text" name="searchstring" size="28"></td>
<td><select size="1" name="searchtype">
<option selected value="firstname">First Name</option>
<option value="lastname">Last Name</option>
<option value="nick">Nick Name</option>
<option value="email">Email</option>
</select></td>
</tr>
</table>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset"
></p>
</form>
<?php
}
?>
</HTML>

This is view.php
<HTML>
<?php
$db = mysql_connect("localhost", "root", "");
mysql_select_db("learndb",$db);
$result = mysql_query("SELECT * FROM personnel WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
echo "First Name: ".$myrow["firstname"];
echo "<br>Last Name: ".$myrow["lastname"];
echo "<br>Nick Name: ".$myrow["nick"];
echo "<br>Email address: ".$myrow["email"];
echo "<br>Salary: ".$myrow["salary"];
?>
</HTML>

Please why is this code no more working and what is the solution.

Thanks.

okonjiaustin

phparion

7:00 am on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld!

I guess nobody is going to fix it for you while you are not making any effort. Let's start doing it!

first step, display your query by echoING your $sql variable and see if you are getting the query structure correct. If you do not get the variables values in the query it means your old PHP version had REGISTER GLOBAL ON and this has set it to OFF so use ini_set() or htaccess to switch on this for your script life time, alternatively use global arrays, suggested method, i.e $_GET,$_POST or $_REQUEST to cover both previous arrays to get the variable values.

I hope you will solve it by this, if not, please return with the updated status of the problem and post your sql query structure here to get further help.

thank you

okonjiaustin

7:18 am on Feb 5, 2008 (gmt 0)

10+ Year Member



Thanks for your suggestion. I will do that and get back to you.

okonjiaustin

okonjiaustin

4:51 am on Feb 6, 2008 (gmt 0)

10+ Year Member



Hi PhpArion,

You are a great guy, this problem has given me gray hairs for sometime now, but within a few seconds you have just given the solution.

I am very happy to be associated with this forum. I still have one more problem I will submit by tomorrow on retrieving images from mysql databases.

Thanks, good guy till tomorrow.

Okonjiaustin

phparion

9:53 am on Feb 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it is where experience counts :) I have been fixing such things for six years now and can make wild guesses about solutions, anyway I won't praise myself more :P

feel free to post your all problems here, there are many other people to help you who are even better than me.. WebmasterWorld Rocks!