Jump to content

Using PHP to Search MYSQL DB and Display Result


mattyd
 Share

Recommended Posts

I am seeking to learn more about the noted subject, how to use PHP to allow a user to enter search terms and search a database.  I have experimented with this with little results save for errors.  Please see code listed below:

 

search.php

<?
//// filename = search.php
<form method="post" action="result.php3">
<select name="metode" size="1">
<option value="row_name1">metode1</option>
<option value="row_name2">metode2</option>
</select>
<input type="text" name="search" size="25">
<input type="submit" value="Begin Searching!!">
</form>
?> 

 

results.php

//// filename = result.php3
<?
$hostname = "mysql7.000webhost.com"; // Usually localhost.
$username = "a4542527_root"; // If you have no username, leave this space empty.
$password = "*******"; // The same applies here.
$usertable = "people"; // This is the table you made.
$dbName = "a4542527_test1"; // This is the main database you connect to.
MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");
@mysql_select_db( "$dbName") or die( "Unable to select database");
?>
<?
//error message (not found message)
$XX = "No Record Found";
$query = mysql_query("SELECT * FROM $usertable WHERE $metode LIKE '%$search%' LIMIT 0, 30 ");
while ($row = mysql_fetch_array($query))
{
$variable1=$row["row_name1"];
$variable2=$row["row_name2"];
$variable3=$row["row_name3"];
print ("this is for $variable1, and this print the variable2 end so on...");
}

//below this is the function for no record!!
if (!$variable1)
{
print ("$XX");
}
//end
?>

 

Upon viewing search.php I receive the error message:

Parse error: syntax error, unexpected '<' in /home/a4542527/public_html/search.php on line 3

 

I believe I may be missing something and am a bit lost.

Thank-you in in advance for any help or suggestions.

~Matty

Link to comment
Share on other sites

You're using short <? open tags. Change the tags from <? To <?php tags and see if that helps.

 

EDIT: that isn't what the problem is, but it won't hurt to do it.

The problem is you're trying to echo within php tags, without actually issuing an echo . . . 

Link to comment
Share on other sites

In search.php, you open the php tags, then have an html block in there without using 'echo'. If you simply remove the <?php tags around that block of html, it should fix that error. I didn't look over all of the code, so fixing that one may reveal other errors that need attention.

Link to comment
Share on other sites

In search.php, you open the php tags, then have an html block in there without using 'echo'. If you simply remove the <?php tags around that block of html, it should fix that error. I didn't look over all of the code, so fixing that one may reveal other errors that need attention.

 

Thank-you for all of your help.  :D

 

Matty

Link to comment
Share on other sites

That error has been resolved. Now I am able to enter search data and perform a search.  When attempting to do this, though, I receive the error message:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a4542527/public_html/result.php on line 15

 

Note: I am using this code from a tutorial and did not write it myself so I am a bit stuck with some of it.  I am attempting to connect to a pre-existing DB and search for data that already resides there.

 

Here is the actual page: http://bluelinedown.netau.net/search.php

 

Thanks,

~Matty

 

Link to comment
Share on other sites

Frankly, that tutorial is already teaching you some bad habits. Look this over and see my comments within the code. Let me know if you have any questions.

 

<?php
$hostname = "mysql7.000webhost.com"; // Usually localhost.
$username = "a4542527_root"; // If you have no username, leave this space empty.
$password = "*******"; // The same applies here.
$usertable = "people"; // This is the table you made.
$dbName = "a4542527_test1"; // This is the main database you connect to.
MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");
mysql_select_db( "$dbName") or die( "Unable to select database");
//error message (not found message)
$XX = "No Record Found"; // assign a value for the 'empty result set' message
$query = "SELECT * FROM $usertable WHERE $metode LIKE '%$search%' LIMIT 0, 30"; // Query string, separated from the query execution
if( !$result = mysql_query($query) ) { // check query execution success/failure
echo '<br>Query string: ' . $query . '<br>Resulted in an error: ' . mysql_error() . '<br>';  // if execution fails, echo debugging info incl. error and query string
} else {  // if query succeeds
if( mysql_num_rows($result) > 0 ) {  // if result set is not empty, echo the result set
	while ($row = mysql_fetch_array($query)) {
		$variable1=$row["row_name1"];
		$variable2=$row["row_name2"];
		$variable3=$row["row_name3"];
		print ("this is for $variable1, and this print the variable2 end so on...");
	}
} else { // else, if the result set is empty, echo the message for an empty result set.
	echo $XX;
}
}
//end
?>

Link to comment
Share on other sites

Frankly, that tutorial is already teaching you some bad habits. Look this over and see my comments within the code. Let me know if you have any questions.

 

<?php
$hostname = "mysql7.000webhost.com"; // Usually localhost.
$username = "a4542527_root"; // If you have no username, leave this space empty.
$password = "*******"; // The same applies here.
$usertable = "people"; // This is the table you made.
$dbName = "a4542527_test1"; // This is the main database you connect to.
MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");
mysql_select_db( "$dbName") or die( "Unable to select database");
//error message (not found message)
$XX = "No Record Found"; // assign a value for the 'empty result set' message
$query = "SELECT * FROM $usertable WHERE $metode LIKE '%$search%' LIMIT 0, 30"; // Query string, separated from the query execution
if( !$result = mysql_query($query) ) { // check query execution success/failure
echo '<br>Query string: ' . $query . '<br>Resulted in an error: ' . mysql_error() . '<br>';  // if execution fails, echo debugging info incl. error and query string
} else {  // if query succeeds
if( mysql_num_rows($result) > 0 ) {  // if result set is not empty, echo the result set
	while ($row = mysql_fetch_array($query)) {
		$variable1=$row["row_name1"];
		$variable2=$row["row_name2"];
		$variable3=$row["row_name3"];
		print ("this is for $variable1, and this print the variable2 end so on...");
	}
} else { // else, if the result set is empty, echo the message for an empty result set.
	echo $XX;
}
}
//end
?>

 

Hi.  I looked this over and replaced my file with this code.  I received the following result when running a search query:

 

Query string: SELECT * FROM people WHERE LIKE '%%' LIMIT 0, 30

Resulted in an error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE '%%' LIMIT 0, 30' at line 1

 

I am not really sure what that means or how to address the issue.

 

Page: http://bluelinedown.netau.net/search.php

 

Thanks.

~Matty

 

Link to comment
Share on other sites

That means the $search and $metode variables are empty or undefined. Is there anything in the tutorial that looks like this? If not, add this to the results.php file, right after //error message (not found message)

 

if( isset($_POST['search']) ) {
     $search = mysql_real_escape_string($_POST['search']);
}

if( isset($_POST['metode']) ) {
     $metode = mysql_real_escape_string($_POST['metode']);
}

Link to comment
Share on other sites

That means the $search and $metode variables are empty or undefined. Is there anything in the tutorial that looks like this? If not, add this to the results.php file, right after //error message (not found message)

 

if( isset($_POST['search']) ) {
     $search = mysql_real_escape_string($_POST['search']);
}

if( isset($_POST['metode']) ) {
     $metode = mysql_real_escape_string($_POST['metode']);
}

 

I did as you suggested and am getting a different error now:

 

Query string: SELECT * FROM people WHERE row_name2 LIKE '%kate%' LIMIT 0, 30

Resulted in an error: Unknown column 'row_name2' in 'where clause'

 

 

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.