Jump to content

[SOLVED] SQL script search


barryflood22

Recommended Posts

This code was provided to me by Anthylon for which I am very greatful, but i cant get it to work, can someone help?

 

search.php

-------------

<HTML>
<HEAD></HEAD>
<BODY>
<form name="form1" method="post" action="searchresults.php">
  <input name="txt_search" type="text" id="txt_search" />
  <label>
  <input name="cmd_search" type="submit" id="cmd_search" value="Search">
  </label>
</form>
</BODY>
</HTML>

 

searchresults.php

------------------

<HTML>
<HEAD></HEAD>
<BODY>
<p><a href="../index.php">< back</a></p>
<p>   
<? 
if($_POST['txt_search']) {
$host = 'localhost';
$db='customer';
$uname='root';
$pass='';
$cnn = mysql_connect($host, $uname, $pass);

if (!$cnn) {
	echo "Unable to connect to DB: " . mysql_error();
	exit;
}
if (!mysql_select_db($db)) {
	echo "Unable to select $db: " . mysql_error();
	exit;
}


$sql = "SELECT * FROM customer WHERE name= '" . $_POST['txt_search'] ."'";

$result = mysql_query($sql);

if (!$result) {
	echo "Could not successfully run query ($sql) from DB: " . mysql_error();
	exit;
}

if (mysql_num_rows($result) == 0) {
	echo "No records found";
	exit;
}

echo 'Results of your search: <br> ';	
$i = 1;
while ($row = mysql_fetch_assoc($result)) {
	echo "$i. ${row['name']}<br>";
	$i++;
}

mysql_free_result($result);
} 
?> 
</p>
</BODY>
</HTML>

 

when i run the script i get the following displayed:

 

'; $i = 1; while ($row = mysql_fetch_assoc($result)) { echo "$i. ${row['name']}

"; $i++; } mysql_free_result($result); } ?>

Link to comment
https://forums.phpfreaks.com/topic/46586-solved-sql-script-search/
Share on other sites

this is what you want:

<?php
//searchresults.php
if($_POST['txt_search']) {
$host = 'localhost';
$db = 'customer';
$uname = 'root';
$pass = 'xxx';
$cnn = mysql_connect($host, $uname, $pass);

if (!$cnn) {
	echo "Unable to connect to DB: " . mysql_error();
	exit;
}
if (!mysql_select_db($db)) {
	echo "Unable to select $db: " . mysql_error();
	exit;
}


$sql = "SELECT * FROM customer WHERE name LIKE '%" . $_POST['txt_search'] ."%'";

$result = mysql_query($sql);

if (!$result) {
	echo "Could not successfully run query ($sql) from DB: " . mysql_error();
	exit;
}

if (mysql_num_rows($result) == 0) {
	echo "No records found";
	exit;
}

echo 'Results of your search: <br> ';	
$i = 1;
while ($row = mysql_fetch_array($result)) {
	echo $i . ${row['name'] ."<br>\n";
	$i++;
}

mysql_free_result($result);
} 
?> 

change this:

echo 'Results of your search: <br> ';	
$i = 1;
while ($row = mysql_fetch_array($result)) {
	echo $i . ${row['name'] ."<br>\n";
	$i++;
}

 

to this:

echo 'Results of your search: <br> ';	
$i = 1;
while ($row = mysql_fetch_array($result)) {
	echo $i ." ". $row['name'] ."<br />\n";
	$i++;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.