Creare una semplice sistema di ricerca in php
Posted on 30. Jan, 2007 by Administrator in php, tutorials
In questo tutorial creeremo la nostra piccola applicazione in php mysql.
Iniziamo creando un file chiamato search.php, ora abbiamo bisogno di creare una nuova connessione al db usando mysql_connect.
<?php
$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error()); // Creates a new connection to the mysql server.
mysql_select_db('database'); // Connects to the database.
?>
Sostituiamo 'localhost', 'mysql_user', 'mysql_password' con le nostre informazioni e continuiamo. Adesso dobbiamo creare un form ma prima dobbiamo verificare se il form è stato inviato o meno.
<?php
$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error()); // Creates a new connection to the mysql server.
mysql_select_db('database'); // Connects to the database.
if(!isset($_POST['search'])){ // If the form has not been submitted.
}elseif(isset($_POST['search'])){ // ElseIf the form has been submitted.
} // End ElseIf.
?>
Se il form non è stato inviato mostreremo il form altrimenti la ricerca nel db.
<?php
$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error()); // Creates a new connection to the mysql server.
mysql_select_db('database'); // Connects to the database.
if(!isset($_POST['search'])){ // If the form has not been submitted.
?>
<form method="POST">
Search: <input type="text" name="term" id="term" /><br/>
<input type="submit" name="search" id="search" value="Search" />
</form>
<?php
}elseif(isset($_POST['search'])){ // ElseIf the form has been submitted.
} // End ElseIf.
?>
Fatto cio possiamo mettere il codice che ricerca le info nel db.
<?php
$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error()); // Creates a new connection to the mysql server.
mysql_select_db('database'); // Connects to the database.
if(!isset($_POST['search'])){ // If the form has not been submitted.
?>
<form method="POST">
Search: <input type="text" name="term" id="term" /><br/>
<input type="submit" name="search" id="search" value="Search" />
</form>
<?php
}elseif(isset($_POST['search'])){ // ElseIf the form has been submitted.
$term = addslashes($_POST['term']); // This is our search term, has a little sql injection protection with addslashes.
} // End ElseIf.
?>
Ora che il termine è stato definito possiamo muoverci lungo la tabella in cui vogliamo cercare. la nostra tabrlla di esempio si chiama 'tutorial'.
<?php
$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error()); // Creates a new connection to the mysql server.
mysql_select_db('database'); // Connects to the database.
if(!isset($_POST['search'])){ // If the form has not been submitted.
?>
<form method="POST">
Search: <input type="text" name="term" id="term" /><br/>
<input type="submit" name="search" id="search" value="Search" />
</form>
<?php
}elseif(isset($_POST['search'])){ // ElseIf the form has been submitted.
$term = addslashes($_POST['term']); // This is our search term, has a little sql injection protection with addslashes.
$search = mysql_query("SELECT `title` FROM `tutorials` WHERE `title` LIKE '%$term%'" ); // Using the LIKE syntax we can look in a database for a best match for the input, also the % work as wild cards.
} // End ElseIf.
?>
In modo che selezionerai soltanto il titolo a partire dalla tabella tutorial,il simbolo % indica una qualsiasi sequenza alfanumerica.
mysql_num_rows conta i risultati totali trovati, se il conteggio è 0 che lasceremo l'utente conoscere e se non visualizzeremo i risultati
<?php
$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error()); // Creates a new connection to the mysql server.
mysql_select_db('database'); // Connects to the database.
if(!isset($_POST['search'])){ // If the form has not been submitted.
?>
<form method="POST">
Search: <input type="text" name="term" id="term" /><br/>
<input type="submit" name="search" id="search" value="Search" />
</form>
<?php
}elseif(isset($_POST['search'])){ // ElseIf the form has been submitted.
$term = addslashes($_POST['term']); // This is our search term, has a little sql injection protection with addslashes.
$search = mysql_query("SELECT `title` FROM `tutorials` WHERE `title` LIKE '%$term%'" ); // Using the LIKE syntax we can look in a database for a best match for the input, also the % work as wild cards.
$total = mysql_num_rows($search); // Counts the total results found.
if($total == 0){ // If the total is equal to 0 that means no results have been found.
echo 'Nothing Found '; // Lets the user know nothing has been found!
}else{ // Else there is results.
} // End Else.
} // End ElseIf.
?>
So if there is nothing found lets give them a little message saying "Nothing Found ", but if there is lets use a while loop along with mysql_fetch_array to display the data found.
<?php
$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die(mysql_error()); // Creates a new connection to the mysql server.
mysql_select_db('database'); // Connects to the database.
if(!isset($_POST['search'])){ // If the form has not been submitted.
?>
<form method="POST">
Search: <input type="text" name="term" id="term" /><br/>
<input type="submit" name="search" id="search" value="Search" />
</form>
<?php
}elseif(isset($_POST['search'])){ // ElseIf the form has been submitted.
$term = addslashes($_POST['term']); // This is our search term, has a little sql injection protection with addslashes.
$search = mysql_query("SELECT `title` FROM `tutorials` WHERE `title` LIKE '%$term%'" ); // Using the LIKE syntax we can look in a database for a best match for the input, also the % work as wild cards.
$total = mysql_num_rows($search); // Counts the total results found.
if($total == 0){ // If the total is equal to 0 that means no results have been found.
echo 'Nothing Found '; // Lets the user know nothing has been found!
}else{ // Else there is results.
while($r = mysql_fetch_array($search)){ // This will loop through each row found and return the title of the tutorial.
echo $r['title'] . '<br />';
} // End While.
} // End Else.
} // End ElseIf.
?>
Link: http://deadman2.com/Tutorials/PHP/Creating%20a%20smiple%20search.lawl






Leave a reply