I want to highlight every $searchkey results.
I tried other solutions but they don't work with a search engine.
Results appear in a table.
I want every appearance of the keyword to be highlighted.
Thanks for your help!
<?php
$dbhost = 'xxx';
$username = 'xxx';
$password = 'xxx';
mysql_connect("$dbhost" , "$username", "$password" );
mysql_select_db("xxx") or die("could not find the database!");
$output = '';
if(isset($_GET['search']))
{
$searchkey = $_GET['search'];
$query = mysql_query("SELECT * FROM xxx WHERE email LIKE '%$searchkey%' ") or die("Could not search");
$count = mysql_num_rows($query);
if ($count == 0)
{
$output = 'There was no search results !' ;
}
else
{
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr><th>email</th><th>hashkey</th></tr>";
while ($row = mysql_fetch_array($query))
{
$email = $row['email'];
$hashkey = $row['hashkey'];
echo "<tr><td>";
echo $email;
echo "</td><td>";
echo $hashkey;
echo "</td></tr>";
}
echo '<div>'."$count results were found for '$searchkey'.".'</div>'.'</br>';
}
echo "</table>";
$searchkey= $words;
}
?>
You can use Regular Expression in PHP to do this. In this code I use <strong> tag, you can replace it as you want.
while ($row = mysql_fetch_array($query))
{
$email = preg_replace('/(' . $searchkey . ')/s', '<strong>\1</strong>', $row["email"]);
$hashkey = $row['hashkey'];
echo "<tr><td>";
echo $email;
echo "</td><td>";
echo $hashkey;
echo "</td></tr>";
}
Related
<td> <?php echo $b; ?> </td>
I am trying to put the "ad_soyad" value to the table by returning the "ad_soyad" value to the HTML table code above. I have tried return value but it doesn't work. Any suggestions?
function func2()
{
$servername = "127.0.0.1";
$username = "root";
$password="";
$database = "customer_list";
$port = 3306;
$con = mysqli_connect($servername,$username,$password, $database, $port);
$sql = "SELECT ad_soyad,telefon,e_posta,cari_kart_kodu,olusturma_tarihi,guncelleme_tarihi FROM customer_list";
$result =$con->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "ad soyad:".$row["ad_soyad"]." ";
echo "<br>";
$aa = $row["ad_soyad"];
echo "telefon:".$row["telefon"]." ";
echo "<br>";
echo "eposta:".$row["e_posta"]." ";
echo "<br>";
echo "Cari Kart Kodu:".$row["cari_kart_kodu"]." ";
echo "<br>";
echo "Oluşturma Tarihi:".$row["olusturma_tarihi"]." ";
echo "<br>";
echo "Guncelleme Tarihi:".$row["guncelleme_tarihi"]." ";
echo "<br>";
echo "<br>";
echo "<br>";
}
;
}
I want to add a "download to Excel" button on a website.
I have built a search engine using PHP and mySQL.
When I search a keyword a table appears, I want this table to be
I have tried solution on this website but the data wouldn't go in the right cell!
Thanks for your help!!
<?php
$dbhost = 'xxx';
$username = 'xxx';
$password = 'xxx';
mysql_connect("$dbhost" , "$username", "$password" );
mysql_select_db("Data") or die("could not find the database!");
$output = '';
if(isset($_GET['search']))
{
$searchkey = $_GET['search'];
$query = mysql_query("SELECT * FROM Linkedin WHERE email LIKE '%$searchkey%' ") or die("Could not search");
$count = mysql_num_rows($query);
if ($count == 0)
{
$output = 'There was no search results !' ;
}
else
{
echo '<table class="table table-striped table-bordered table-hover">';
echo "<tr><th>Email</th><th>Hashkey</th><th>Breached account</th></tr>";
while ($row = mysql_fetch_array($query))
{
$email = preg_replace('/(' . $searchkey . ')/i', '<mark>\1</mark>', $row["email"]);
$hashkey = $row['hashkey'];
echo "<tr><td>";
echo $email;
echo "</td><td>";
echo $hashkey;
echo "</td><td>";
echo "Linkedin";
echo "</td></tr>";
$output = '</table>';
}
echo '<div>'."$count results were found for '$searchkey'.".'</div>'.'</br>';
}
echo $output;
}
?>
Use jQuery datatables for the purpose. It is easy and elegant.
https://datatables.net/extensions/buttons/examples/initialisation/export
I'm trying to learn php and mysql, I was trying to read data from my database where i was following something online and encounter an error(s) here is my code
<?php
include 'includes/conn.php';
$query =sprintf("Select * from customer");
$result = mysql_query($query);
if (!$result)
{
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
$message .= 'everything' .$query;
die($message);
}
while ($customer = mysql_fetch_assoc($result))
{
echo $row['cust_id'];
echo $row['fname'];
echo $row['lname'];
echo $row['gender'];
echo $row['dob'];
}
mysql_free_result($result);
?>
//this is where i was trying to make a connection with the database
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db ='telmar_php';
$con = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
?>
Replace
$message ='from you see this then it's not connecting!'.mysql_error() ."\n";
here your string is single quotation marks in it's is causing the problem
with
$message ="from you see this then it's not connecting!".mysql_error() ."\n";
...and to display your results change this:
while ($customer = mysql_fetch_assoc($result))
to this:
while ($row = mysql_fetch_assoc($result))
Please review the below example for reading data my mysql using php.
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
I want every different record of database to be display on each table rows, but I'm unable to retrieve different record for each row and column. Please give me suggestion where I can paste than while block so it will give different result for each row and column.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$rec_limit = 10;
$scriptname=$_SERVER['PHP_SELF'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('online_shopping'); // include your code to connect to DB.
$tbl_name="mobile_db"; //your table name
$start = 0;
$limit = 5;
$sql = "SELECT id,company,model,price,availability,image FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$company=$row['company'];
$model=$row['model'];
$available=$row['availability'];
$price=$row['price'];
}
echo "<table border='2'>";
$j = 0;
for($j = 0; $j<5; $j++)
{
echo "<tr>";
for($i = 0; $i<3; $i++)
{
echo "<td>";
echo "<table border='2'>";
echo "<tr>";
echo "<td><img src='abc.jpg' height='250' width='250'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<table border='2'>";
echo "<tr>";
echo "<td><b>Brand : </b></td>";
echo '<td>'.$company.'</td>';
echo "</tr>";
echo "<tr>";
echo "<td><b>Model : </b></td>";
echo '<td>'.$model.'</td>';
echo "</tr>";
echo "<tr>";
echo "<td><b>Availability : </b></td>";
echo '<td>'.$available.'</td>';
echo "</tr>";
echo "<tr>";
echo "<td><b>Price : </b></td>";
echo '<td>'.$price.'</td>';
echo "</tr>";
echo "</table>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
You need to move the table rows inside the fetch loop or store the row in an array. I have simplified your tables to make the example clearer:
$result = mysql_query($sql);
if (!$result) {
/* Error */
}
echo '<table>';
while ($row = mysql_fetch_array($result)) {
echo '<tr><td><img src="', htmlspecialchars ($row['image']), '">';
echo '<tr><td><table>';
echo ' <tr><th>Brand<td>', htmlspecialchars ($row['company']);
echo ' <tr><th>Model<td>', htmlspecialchars ($row['model']);
echo ' <tr><th>Availability<td>', htmlspecialchars ($row['availability']);
echo ' <tr><th>Price<td>', htmlspecialchars ($row['price']);
echo ' </table>';
}
echo "</table>\n";
Some notes about the code:
Test the return value of mysql_query(). The query might fail.
Escape your output using htmlspecialchars().
You should use <th> elements for your headings and style those, instead of using inline <b> elements.
I added output of $row['image'] which might not do what you want.
And do not use the deprecated mysql extension. Use PDO or mysqli instead.
In your while loop you always rewrite the same variables, after loop you have only last record saved.
In the loop, you have to save records into array.
In your code you have nested tables, but in the first one, there is only one row and one table cell which contains another table. I use just nested table.
<?php
...
$result = mysql_query($sql);
$data = array();
while($row = mysql_fetch_array($result)) {
$data[] = $row;
}
if (count($data) > 0) {
echo '<table>';
foreach ($data as $row) {
echo '<tr>';
echo '<td>Brand: ' . $row['company'];
echo '<td>Model: ' . $row['model'];
echo '<td>Availability: ' . $row['availability'];
echo '<td>Price: ' . $row['price'];
}
echo '</table>';
} else {
echo 'no records';
}
?>
Not sure I fully understand what you are trying to accomplish but try this.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$rec_limit = 10;
$scriptname=$_SERVER['PHP_SELF'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('online_shopping'); // include your code to connect to DB.
$tbl_name="mobile_db"; //your table name
$start = 0;
$limit = 5;
$sql = "SELECT id,company,model,price,availability,image FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);
echo "<table border='2'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['company']."</td>";
echo "<td>".$row['model']."</td>";
echo "<td>".$row['availability']."</td>";
echo "<td>".$row['price']."</td>";
echo "</tr>";
}
echo "</table>";
?>
I am trying to retrieve information from my database depending on the ID a user types into my URL.
For example: If USER A went to www.exampleurl.com/index.php?id=1 it would echo out the user's information which has an ID of 1. Same thing if the id was 2, 3, etc. Users are entering their information via a form in a different file called submit.php.
Here is my code to retrieve data depending on ID :
<?php
$id = $_GET['id'];
//Variables for connecting to your database.
$hostname = "";
$username = "";
$dbname = "";
$password = "";
$usertable = "";
//Connecting to your database
$con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname, $con);
$query = "SELECT * FROM $usertable WHERE id = $id LIMIT 1";
$result = mysql_query($query, $con);
echo "Hello, " . $result['name'];
?>
Any ideas on if my SELECT request is wrong?
EDIT
Here is my code for showing the data altogether in a table. This works fine.
<?php
//Variables for connecting to your database.
$hostname = "";
$username = "";
$dbname = "";
$password = "!";
$usertable = "";
//Connecting to your database
$con = mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname, $con);
//Fetching from your database table.
$query = "SELECT * FROM $usertable";
$result = mysql_query($query, $con);
echo "<table border=1>
<tr>
<th> ID </th>
<th> Name </th>
<th> Age </th>
</tr>";
while($record = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['name'] . "</td>";
echo "<td>" . $record['age'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
→ Try This:
You should consider using PHP PDO as it is safer and a more object oriented approach:
$usertable = "";
$database = new PDO( 'mysql:host=localhost;dbname=DB_NAME', 'DB_USER_NAME', 'DB_USER_PASS' );
$statement = $database->prepare('SELECT * FROM $usertable');
$statement->execute();
$count = $statement->rowCount();
if( $count > 0 ) {
$R = $statement->fetchAll( PDO::FETCH_ASSOC );
for( $x = 0; $x < count($R); $x++ ) {
echo "<tr>";
echo "<td>" . $R[ $x ]['id'] . "</td>";
echo "<td>" . $R[ $x ]['name'] . "</td>";
echo "<td>" . $R[ $x ]['age'] . "</td>";
echo "</tr>";
}
}
else { echo "Error!"; }
you need to use mysql_fetch_assoc function for retrieve the results.
$result = mysql_fetch_assoc(mysql_query($query, $con));
echo "Hello, " . $result['name'];
You should be error checking your mysql_querys:
$query = "SELECT * FROM $usertable WHERE id = $id LIMIT 1";
$result = mysql_query($query, $con);
if(!result)
echo mysql_error();
You should also retrieve the results:
$array = mysql_fetch_assoc($result);
I'll consider some secure features like
Check if $_GET['id'] is set and if is int
Apply Mysql escape with mysql_escape_string() function