Skip to content

Come creare un modulo di ricerca utilizzando un esempio di codice PHP

Vi portiamo la risposta a questo incidente, almeno lo speriamo. Se continui con le preoccupazioni dicci, per noi sarà un piacere aiutarti

Esempio 1: modulo di ricerca php sql

$localhost="localhost";$username="root";$password="";$dbname="samueldb";$con=newmysqli($localhost,$username,$password,$dbname);if($con->connect_error){die('Error: '.$con->connect_error);}$sql="SELECT * FROM users";if(isset($_GET['search'])){$name=mysqli_real_escape_string($con,htmlspecialchars($_GET['search']));$sql="SELECT * FROM users WHERE firstname ='$name'";}$result=$con->query($sql);?>DOCTYPEhtml><html><head><title>Basic Search form using mysqlititle><linkrel="stylesheet"type="text/css"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">head><body><divclass="container"><label>Searchlabel><formaction=""method="GET"><inputtype="text"placeholder="Type the name here"name="search"><inputtype="submit"value="Search"name="btn"class="btn btn-sm btn-primary">form><h2>List of studentsh2><tableclass="table table-striped table-responsive"><tr><th>IDth><th>First nameth><th>Lastnameth><th>Addressth><th>Contactth>tr>while($row=$result->fetch_assoc()){?><tr><td>echo$row['user_id'];?>td><td>echo$row['firstname'];?>td><td>echo$row['lastname'];?>td><td>echo$row['address'];?>td><td>echo$row['contact'];?>td>tr>}?>table>div>body>html>

Esempio 2: barra di ricerca in php mysqli

mysql_connect("localhost","root","")ordie("Error connecting to database: ".mysql_error());/*
		localhost - it's location of the mysql server, usually localhost
		root - your username
		third is your password

		if connection fails it will stop loading the page and display an error
	*/mysql_select_db("tutorial_search")ordie(mysql_error());/* tutorial_search is the name of database we've created */?>DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><title>Search resultstitle><metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/><linkrel="stylesheet"type="text/css"href="style.css"/>head><body>$query=$_GET['query'];// gets value sent over search form$min_length=3;// you can set minimum length of the query if you wantif(strlen($query)>=$min_length){// if query length is more or equal minimum length then$query=htmlspecialchars($query);// changes characters used in html to their equivalents, for example: < to >$query=mysql_real_escape_string($query);// makes sure nobody uses SQL injection$raw_results=mysql_query("SELECT * FROM articles
			WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')")ordie(mysql_error());// * means that it selects all fields, you can also write: `id`, `title`, `text`// articles is the name of our table// '%$query%' is what we're looking for,% means anything,for example if$query is Hello
		// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'if(mysql_num_rows($raw_results)>0){// if one or more rows are returned do followingwhile($results=mysql_fetch_array($raw_results)){// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loopecho"

".$results['title']."

"
.$results['text']."

"
;// posts results gotten from database(title and text) you can also show id ($results['id'])}}else{// if there is no matching rows do followingecho"No results";}}else{// if query length is less than minimumecho"Minimum length is ".$min_length;}?>
body>html>

Ecco i commenti e le valutazioni

Successivamente puoi trovare le annotazioni di altri amministratori, puoi anche mostrare le tue se ne hai voglia.



Utilizzate il nostro motore di ricerca

Ricerca
Generic filters

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.