Skip to main content
Participating Frequently
December 23, 2017
Answered

Mysql search script

  • December 23, 2017
  • 2 replies
  • 948 views

Hi

not really sure what name I am looking for. I have a mysql database that has fields such as surname, year etc etc, I created it in access and have imported it into mysql,

I need to build a search page that builds a record set based on the search criteria and then displays the results.

Are there any ready made scripts or videos  that will help me in this project?

thanks

Ian

This topic has been closed for replies.
Correct answer osgood_

If you want to search your database by the 'surname' column that is fairly straight forward.

Use the uniquie 'id' field in the database to pass the details to the details page, in this case the unique column is just 'id'

<?php

//connect to database (use your own connection details)

$conn = new mysqli('server_name' , 'username' , 'password' , 'database_name);?>

<?php

if(isset($_POST['submit'])){

//get surname from form field

$surname= $_POST['surname'];

// get results to use in a REPEAT region

$sql = "SELECT * FROM table_name WHERE surname = '$surname' ORDER BY id ASC";

//get number of rows

$num_rows = $conn->query($sql)->num_rows;

//assign results to a variable

$listResults = $conn->query($sql) or die($conn->error);

}

?>

<h3>Search Database</h3>

<form name="search_database" method="post" action="">

<p><label for="surname">Surname</label>

<input type="text" name="surname">

</p>

<input type="submit" name="submit" value="Search" />

</form>

<?php if(isset($num_rows) && $num_rows > 0) {

echo "<h2>Results ($num_rows)</h2>";

}

?>

<?php while($row = $listResults->fetch_assoc()) { ?>

<p><a href="more_details.php?id=<?php echo $row['id']; ?>"><?php echo $row['surname']; ?></a></p>

<?php } ?>

<?php if(isset($num_rows) && $num_rows == 0) {

echo "<h2>Sorry No Results Found</h2>";

}

?>

2 replies

osgood_Correct answer
Legend
December 23, 2017

If you want to search your database by the 'surname' column that is fairly straight forward.

Use the uniquie 'id' field in the database to pass the details to the details page, in this case the unique column is just 'id'

<?php

//connect to database (use your own connection details)

$conn = new mysqli('server_name' , 'username' , 'password' , 'database_name);?>

<?php

if(isset($_POST['submit'])){

//get surname from form field

$surname= $_POST['surname'];

// get results to use in a REPEAT region

$sql = "SELECT * FROM table_name WHERE surname = '$surname' ORDER BY id ASC";

//get number of rows

$num_rows = $conn->query($sql)->num_rows;

//assign results to a variable

$listResults = $conn->query($sql) or die($conn->error);

}

?>

<h3>Search Database</h3>

<form name="search_database" method="post" action="">

<p><label for="surname">Surname</label>

<input type="text" name="surname">

</p>

<input type="submit" name="submit" value="Search" />

</form>

<?php if(isset($num_rows) && $num_rows > 0) {

echo "<h2>Results ($num_rows)</h2>";

}

?>

<?php while($row = $listResults->fetch_assoc()) { ?>

<p><a href="more_details.php?id=<?php echo $row['id']; ?>"><?php echo $row['surname']; ?></a></p>

<?php } ?>

<?php if(isset($num_rows) && $num_rows == 0) {

echo "<h2>Sorry No Results Found</h2>";

}

?>

jen0dorfAuthor
Participating Frequently
December 24, 2017

Hi

Many thanks

have a great holiday

Ian

Nancy OShea
Community Expert
Community Expert
December 23, 2017

STEP 1:  TEST CONNECTION AND FIND OUT WHICH PHP VERSION YOU HAVE.

Copy & paste this code into a new .php file.

Change username, password and database to your own values.

<?php

$con = mysqli_connect("localhost","username","password","database");

// Check connection

if (mysqli_connect_errno())

  {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();

        }

        else echo "Successfully connected, happy coding!"

?>

<?php phpinfo() ?>

Save script above as  test.php and put to local testing server.  Open test.php in your browser to check connection status and tell you which version of PHP you're running.

=============================

STEP 2: CREATE A SEARCH FORM

<form action="searchScript.php" method="GET" id="search">

<input type="text" id="searchField" placeholder="Database Search" name="query" />

<input type="submit" id="submit" value="Find" />

</form>

============================

Steps 3 & 4 will depend on your results in Step 1

Nancy O'Shea— Product User & Community Expert
jen0dorfAuthor
Participating Frequently
December 23, 2017

Hi

thanks for that I'll experiment

gave a good xmas

Ian