I am trying to convert search function from one tutorial from mysql into PDO. I need to search inside of my table. With my limited knowledge I was able to do this. However, there are still some things I cannot resolve. What I am missing?
When searching no matter what I search I still get no result.
ORIGINAL
if(isset($_GET['keywords'])){
$keywords = $db->escape_string($_GET['keywords']);
$query = $db->query(
"SELECT name, description
FROM cars
WHERE name LIKE '%{$keywords}%' OR description LIKE '%{$keywords}%'
");
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results.
</div>
<?php
if($query->num_rows){
while ($r = $query->fetch_object()) {
?>
<div class="result">
<?php echo $r->name; ?>
</div>
<?php
}
}
}
?>
PDO version/my version
<?php
include 'inc/header.php';
if(isset($_GET['keywords'])){
$keywords = $_GET['keywords'];
$cars = $db->prepare("SELECT name, description FROM cars WHERE name LIKE '%{$keywords}%' OR description LIKE '%{$keywords}%'
");
$cars->execute();
?>
<div class="result-count">
Found <?php echo $cars->rowCount(); ?> results.
</div>
<?php
if($cars->rowCount()){
while($r = $cars->fetchObject()){
?>
<div class="result">
<?php echo $r->name; ?>
</div>
<?php
}
}
}
?>
Html
<form action="search.php" method="get">
<label>
<input type="text" name="keywords" value="SEARCH" autocomplete="off">
</label>
<input type="submit" value="Search">
</form>
Related
i want to add commenting to each article, so inside my"foreach"cycle I added commenting to each article, but "set a comment" function runs to all art
thats the code for making an article window
<?php $articles_qr = mysqli_query($connection, "SELECT * FROM `articles` ");
$articles = array();
while ( $art = mysqli_fetch_assoc($articles_qr))
{
$articles[] = $art;
}
?>
<?php foreach ($articles as $art)
{
?>
<section>
<div class="containerstuff">
<div class="stuffpic">
<img src="<?php echo "../static/imagespages/",$art['image'] ?>" class="pico">
</div>
<div class="article">
<h1><?php
echo $art['title']
?>
</h1>
<?php
echo $art['text'];
echo $art['id']
?>
</div>
</div>
<div class="scrollmenu">
<?php include "../includes/comments.php";?>
</section>
<?php
} ?>
thats the code comments window
<?php
date_default_timezone_set(timezone_identifier);
include_once '../comments.ink.php'
?>
<div class="containercom">
<img src="#" class="commpic">
<p class="comment"></p>
</div>
<div class="blockcom">
<form class='form' method='POST' action="<?php echo setComments($connection)?>">
<div class='form__group'>
<input type='hidden' name='page_id' value="<?php echo $art['id']?>" >
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='pubdate' value="<?php echo date('Y-m-d H:i:s')?>" >
<textarea name='text' class='form__control' placeholder ='Введите комментарий...' required=''></textarea>
</div>
<div class='form__group'>
<input type='submit' class='form__control2' name='commentSubmit'></input>
</div>
</div>
</form>
and thats the code for INSERT function
<?php
static $firstTime = true;
function setComments($connection) {
if(isset($_POST['commentSubmit'])){
$idcom = $_POST['page_id'];
$uid = $_POST['uid'];
$pubdate = $_POST['pubdate'];
$text = $_POST['text'];
$sql =
"INSERT INTO `comments` (page_id, uid, pubdate, text)
VALUES ('$idcom', '$uid', '$pubdate', '$text')";
$result = $connection->query($sql);
$firstTime = false;
}
}
so how can i make insert only for 1 article (so when i add it now, there are appears as many comments as many articles i have in database)
I think you should use ajax to append a new comment which is the widely used solution, the way u r doing will become difficult to handle for you.
I haven't found the solution, so I just selected another way.
For each article I placed a button to post a comment which will send user to the page with this article and for this button for "href" I wrote php code (href = "comments.php?id=<?php echo $art['id']"?>) and for this page I use $_GET to select articles only for this id. Then I just placed there comments-function that I wrote so it works alright now because function works only for 1 argument
I have made a basic search engine and I try, to fetch the results, on the same page; moreover, the results have been retrieved and stored in the associative array, but the embedded code in HTML shows only one record, of the results. `
<?php
require('Configuration/config.php');
require('Configuration/db.php');
//If the user clicks, on the button search, the execute the query
if (isset($_POST['search_btn'])) {
$search_query = $_POST['search'];
//$search_query = htmlspecialchars($_POST['search']);
//Create the query.
$query = "SELECT * FROM The_primary_arkivum WHERE
Name = '$search_query' OR
Address = '$search_query' OR
Category = '$search_query' OR
Country = '$search_query' OR
State = '$search_query'";
//Get the results.
$results = mysqli_query($conn, $query);
//Fetch the data, of the result, to an array.
$search_results = mysqli_fetch_all($results, MYSQLI_ASSOC);
//var_dump($search_results);
//var_dump($search_query);
//Free result
mysqli_free_result($results);
//Close the connection
mysqli_close($conn);
}
?>
<?php include('included/header.php'); ?>
<body>
<div class = "header">
<h2>Search</h2>
</div>
<form method="post" action="search_index.php">
<div class="input-group">
<label>Search</label>
<input type="text" name="search" value="<?php echo $search; ?>">
</div>
<div class="input-group">
<button type="submit" class="btn" name="search_btn">Search</button>
</div>
<?php foreach($search_results as $search_result) : ?>
<div class="mySlides fade">
<?php echo $search_result['Name']?>
<?php echo $search_result['Address']?>
<?php echo $search_result['Country']?>
</div>
<?php endforeach; ?>
</form>
<?php include('included/footer.php'); ?>
`
Your echo statements in your form do not have ending semicolons ;. Try starting there.
<?php echo $search_result['Name'];?>
<?php echo $search_result['Address'];?>
<?php echo $search_result['Country'];?>
I'm creating a comment system in my project. I want each posted question to have a comment. I was able to post the comment, but I am having a problem displaying all comment to it's respective answers. I was able to display only one row but not the row of the comment. I try to use a while loop nested inside the while that echo's each question, but it hangs. When I use if it only displays the first row of the comment of each question.
So my question is how can I display them all?
<div class="answer">
<?php
include 'db.php';
$sql = "select * from answers where questionrid IN(select id from question where id='$qid')";
$result = mysqli_query($con,$sql);
if($result){
while($ro = mysqli_fetch_assoc($result)){
?>
<div class="a_view">
<pre>
<?php
echo $ro["answer"];
?>
</pre>
</div>
<div class="ans_comment">
<?php
if($ro["id"]){
$id = $ro["id"];
$sqli = "SELECT * FROM comments WHERE answerid='$id'";
$query = mysqli_query($con,$sqli);
$row = mysqli_fetch_assoc($query);
$num = mysqli_num_rows($query);
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
}
?>
</div>
<div class="add"><div class="coment">add a comment</div> <div id="coment">
<form class="cform" method="post" action="acomment.php">
<textarea type="text" name="comment" class="tcomment" placeholder="add your comment here,your is
required to give correction or more information about the problem"></textarea><br><br>
<input type="hidden" value="<?php echo $ro["id"]; ?>" name="userid">
<input type="submit" value="Post your comment">
</form>
</div></div>
<?php
}
}else{
echo "no record";
}
?>
<?php
$con->close();
?>
This is the section that made it hang
while($row){
?>
<div><?php echo $row["comments"];?></div>
<?php
}
when I use if, it only echos one row.
Instead of
while($row){
do it just like you're doing in the while loop above
while($row = mysqli_fetch_assoc($query)){
The way you have it now, $row is never changing, and therefore always evaluates to true, leaving you stuck in your loop.
I am trying print out a table that has student testimonials and I want to print out the ID, title, text and student ID but when hit the "search_all" button, I am told that it can't fetch mysqli and I am not sure what to do.
<?php ("session.php"); ?>
<?php require_once("connect.php"); ?>
<?php include("header.php"); ?>
<html>
<body>
<!-- Page Content -->
<h1 class="page-header">Learning Journals
<small>- Admin Search</small>
</h1>
<h3>Search Learning Journals</h3>
<form name="membership_form" action= "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="contactForm" method="post">
<label>Search Journals ID:</label>
<br>
<input type="text" name="journal_search">
<br>
<button type="submit" name ="search_all" class="btn btn-primary">All Journals</button>
</form>
<?php
if(isset($_POST["search_button"]))
{
//create the sql statement
$sql_all= "SELECT * FROM testimonials";
$result = mysqli_query($con,$sql_all);
while ($row = mysqli_fetch_assoc($result))
{
echo
"<h4>Testimonial ID:</h4>". $row["testimonial_ID"].
"<h4>Title:</h4> ". $row["testimonial_title"].
"<h4>Text:</h4> ". $row["testimonial_text"].
"<h4>Student ID:</h4>". $row["student_ID"].
"</br>";
}
mysqli_close($con);
}
include("footer.php");
?>
</body>
</html>
I'm just learning how to do this so please forgive my ignorance!
This page loads VERY slow and the search almost doesn't run on my iphone (sometimes timesout).
Here is my test site: http://webtestkit.com/1KaraokeDJ/index.php
$view=$db->query("select * from 1KaraokeDJ where
Title like '%$data%' ||
Artist like '%$data%' ||
Disc like '%$data%' ||
Brand like '%$data%'
limit 50");
is there a better way to do this search?
There are 28,000+ records now, but it will be in the 100k range later.
here is the full code:
<?php
include("connect.php");
session_start();
if(isset($_POST['submit']))
{
$search=$_POST['search'];
$_SESSION['title']= $search;
if(($_SESSION['title'])!="")
{ header("location:index.php"); }
else
{ echo "<script> alert('Please enter something to search for') </script>"; }
}
?>
<html>
<head>
<title>1KaraokeDJ.com</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="login">
<form method="post">
<p><img src="top.jpg" /></p>
<p>
<?php if(isset($_SESSION['title'])) { ?>
<input name="search" type="search" list="searchkey" value="<?php echo $_SESSION['title'];?>" class="search" />
<?php } else { ?>
<input name="search" type="search" list="searchkey" placeholder="Just type your text here and press enter - ex : Abba" class="search" />
<?php } ?>
</p>
<datalist id="searchkey">
<?php
$tile=$db->query("SELECT * FROM `1KaraokeDJ`");
while($storetitle=mysqli_fetch_object($tile))
{ ?>
<option value="<?php echo $storetitle->title ?>">
<?php } ?>
</datalist>
<p><input type="submit" name="submit" id="click" class="searchbutton" value="Karaoke Search" /></p>
<hr style="width:100%">
<?php if(isset($_SESSION['title'])) {
if(($_SESSION['title']!=""))
{
$data=$_SESSION['title'];
$view=$db->query("select * from 1KaraokeDJ where
Title like '%$data%' ||
Artist like '%$data%' ||
Disc like '%$data%' ||
Brand like '%$data%'
limit 50");
$check=mysqli_num_rows($view);
if($check!="")
{ while($descri=mysqli_fetch_object($view)) { ?>
<div class="reslt">
<h3 id="results">
<?php
echo str_replace($data, '<span class="highlight">'.$data."</span>", $descri->Artist);
echo " - ";
echo str_replace($data, '<span class="highlight">'.$data."</span>", $descri->Title);
?>
</h3>
<p class="Description">
<?php
echo str_replace($data, '<span class="highlight">'.$data."</span>", $descri->Brand);
echo " - ";
echo str_replace($data, '<span class="highlight">'.$data."</span>", $descri->Disc);
echo " - ";
echo $descri->Track;
?>
<p>
<hr>
</div>
<?php } ?>
<p><?php echo $check ?> Results</p>
<p class="highlight">Showing Up To 50 Results - Try Refining Your Search</p>
<?php } else { ?>
<div class="reslt">
<h3 id="results">Nothing Found!</h3>
<p class="Description">Try Changing Your Search Terms<p><hr>
</div>
<?php } } } ?>
</form>
</div>
</body>
</html>
Your answer is:
Place full-text indexes on your columns. Use match instead of like to actually use said indexes
an example of match
WHERE MATCH(field1 , field2) AGAINST ('aaa*' IN BOOLEAN MODE)
manual for match
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
<datalist id="searchkey">
<?php
$tile=$db->query("SELECT * FROM `1KaraokeDJ`");
while($storetitle=mysqli_fetch_object($tile))
{ ?>
<option value="<?php echo $storetitle->title ?>">
<?php } ?>
</datalist>
for whatever reason, saving a datalist to the client browser is very slow!