Discuti il tutorial sul forum !
Creare un semplice script php per ricercare dati nel database (parte 2)
In questo tutorial vedremo come creare un semplice script per la ricerca di dati in un database. Per continuare a leggere il tutorial occorre leggere la prima parte.
Inseriamo nello script, la stringa sql che cerca i dati nella tabella
<?
$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.
?>
Se la ricerca non ha prodotto risultati mostra la scritta "not found"
<?
$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.
?>
E infine lo script definitivo che mostra i risultati
<?
$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>
<?
}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.
?>
Correlati
- Rimuovere spazi da una stringa con php
- Rimuovere tag <script> da una stringa con php
- Upload di file con php e ajax
- Ricercare dati in un database access
- Inserire delle informazioni in un database access tramite un form
- Creare un file word al volo con asp
- creare un file xml per feed rss
- Creare un file dtd valido
- Algoritmo a priori (parte 1)
- Algoritmo a priori (parte 2)
- Algoritmo a priori con php e mysql
- Creare Interfaccia Utente in vba
- creare e cancellare un database con phpmyadmin
- creare una tabella con phpmyadmin
- Struttura dati albero
- struttura dati albero n-ario
- Archivio su file con struttura dati lista
- Struttura dati coda
- Struttura dati coda 2
- Struttura dati coda 3
- Interagire con un database access con c++
- Struttura dati lista
- Struttura dati lista 2
- Struttura dati lista doppiamente concatenata
- Struttura dati matrice in c++
- Struttura dati pila
- Struttura dati pila c++
- Struttura dati stack in c++
- Muovere un oggetto 3d con macromedia director (Parte 1)
- Muovere un oggetto 3d con macromedia director (Parte 2)
- Muovere un oggetto 3d con macromedia director (Parte 3)
- Muovere muovere la camera con la tastiera con macromedia director (Parte 4)
- Guida completa alla pixel art (capitolo 2)
- Bordi arrotondati senz utilizzo di immagini con i css
- Figure Isometriche in pixel art (parte 1)
- Figure Isometriche in pixel art (parte 2)
- Figure Isometriche in pixel art (parte 3)
- Bordi arrotondati con i css
- La guida completa ai css capitolo 2
- Creare un rettangolo con bordi 3d con i css
- Creare un box con effetto shadow con i css
- Redirect in php
- Evitare javascript sql injection
- Scrivere codice php in pagine .html
- Gestire le finestre di popup in javascript
- Creare favico animate
- Rimuovere spazi da una stringa con php
- Disabilitare i componenti di un form in javascript
- Visualizzare i dati letti dal database in una tabella
- Rollover di immagini in javascript
- Guida completa al linguaggio php
- Guida javascript
- Creare un linea con DOM
- Colorare il codice php
- messaggio di testo sulla barra di stato in javascript
- Criptare e decriptare un cookie in php
- Zoommare una immagine in javascript
Commenti
Scrivi un commento































