Cerca articolo o scritps su sastgroup.com
es: banca sella

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.
?>

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • del.icio.us
  • De.lirio.us
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Reddit
  • scuttle
  • Smarking
  • Spurl
  • YahooMyWeb
  • DZone
  • Internetmedia
  • Snap2r
  • Technorati


Correlati


Commenti

Scrivi un commento





Commenti recenti:


Ultimi dal forum