very slow search results - mysqli - php - php

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!

Related

PHP search with PDO

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>

Print out table details from database

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>

Making html page appear on click of submit button

I am trying to make another page to appear when I click certain button, I basically know how to do this, but this time I am in trouble.I have following code:
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<!--See siin all tekstiväli-->
<H3>Minu küsitlused </H3>
<hr>
<br>
<br>
<br>
<ol>
<?php
include_once 'init/init.funcs.php';
$result = mysql_query('SELECT * from katse_kysimustik_pealkiri');
while($row = mysql_fetch_assoc($result)) {
$titles[] = $row['pealkiri'];
}
foreach($titles as $title) {
?>
<li>
<?php echo $title ?>
<form action='Minu_kysitlused_1.php'>
<input type="button" name = "saada" value="saada">
<input type="button" value="tulemused">
<input type="button" value="lõpeta ennetähtaegselt">
<input type="button" value="X">
</li>
</form>
<?php
}
?>
</ol>
</body>
</html>
<?php
if(isset($_POST['saada'])){
echo "<meta http-equiv='refresh' content='0;url=http://localhost/Praks/saada.html'>";
}
?>
Everything works just fine but when I click button 'saada' nothing happens. What should I do to make saada.html appear on this click?
replace this:
<form action='Minu_kysitlused_1.php' method="post">
Missing:
method="post"
why you try with meta tag
You just put header("location:yourpagename");
just like below
if(isset($_POST['saada'])){
header("Location:http://localhost/Praks/saada.html");
}
try like this
header function must be First thing to sent as html otherwise it will not work
<?php
if(isset($_POST['saada'])){
header( "Location : localhost/Praks/saada.html");
die();
}
?>
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<!--See siin all tekstiväli-->
<H3>Minu küsitlused </H3>
<hr>
<br>
<br>
<br>
<ol>
<?php
include_once 'init/init.funcs.php';
$result = mysql_query('SELECT * from katse_kysimustik_pealkiri');
while($row = mysql_fetch_assoc($result)) {
$titles[] = $row['pealkiri'];
}
foreach($titles as $title) {
?>
<li>
<?php echo $title ?>
<form action='Minu_kysitlused_1.php'>
<input type="button" name = "saada" value="saada">
<input type="button" value="tulemused">
<input type="button" value="lõpeta ennetähtaegselt">
<input type="button" value="X">
</li>
</form>
<?php
}
?>
</ol>
</body>
</html>
second approach
<form action="your-url">
<input type="submit" name="Submit" value="saada"/>
</form>
in first time, I don't understand why do you use the form tag to call a page, if there aren't any data passed via POST?
Anyway, in your foreach cicle you can use a very simple collection of link:
foreach($titles as $title) {
echo $title;
echo 'Link 1';
echo 'Link 2';
echo 'Link 3';
echo 'Link 4';
}
This solution is too clean.
Bye
Marco

$_SESSION doesn't seem to work across files?

I am making a game for my computing class. The user has to change settings of their robot before playing. Most are using Visual Basic or Game Maker but I felt more comfortable with web based languages.
The issue I have just run into is when I am checking whether $_SESSION variables are set. This is a screenshot of my script:
http://i60.tinypic.com/2ia4hnr.jpg
As you can see on the left their is a button called "Play". On the customisation page the user fills in all the settings and they are stored as $_SESSION variables however when I use the If statement on all other pages it always sees them as not being set, so it does not allow them to play.
This is the customise.php page code (sorry for posting all the code but I felt it was needed for me to best explain my issue):
<?php $layout_context = "public"; ?>
<?php include("header.php"); ?>
<div id="main">
<div id="navigation">
<h2><i><u>Navigation</u></i></h2>
<br />
<u>Welcome<br /><br />
<u>How to Play?</u><br /><br />
<u><font color="orange">Customisation</font></u><br /><br />
<?php if (isset($_SESSION['RobotName'], $_SESSION['traction'], $_SESSION['passenger'])) {
?>
<u>Play</u>
<?php
} else {
?>
<u>Play</u>
<?php
}
?>
</div>
<div id="page">
<h1>Robot Customisation</h1>
<br />
<p><h3>Welcome to the BotMod Robot Customisation!</h3><br />
Below you will have three options that enable you to play BotMod. Those are: Your Robot name, Traction Type and Passenger Size.<br /> See the bottom of this page for further guidance.<br /><br />
<br />
<form id="settings" name="settings" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h4>Robot Name:</h4> <input type="text" name="RobotName"><br />
<h4>Traction Type:</h4>
<select name="traction">
<option value="">Please Select Your Traction Type...</option>
<option value="Wheels">Wheels</option>
<option value="Tracks">Tracks</option>
<option value="Skis">Skis</option>
</select>
<br />
<h4>Passenger Size:</h4>
<select name="passenger">
<option value="">Please Select Your Passenger Size...</option>
<option value="Large">Large</option>
<option value="Medium">Medium</option>
<option value="Small">Small</option>
</select>
<br />
<input type="submit" name="Submit" value="Save Settings" />
</form>
<?php
require_once("session_start.php");
// Store all submitted $_POST fields to their own $_SESSION ID to be called
// later. Must use session_start();
if (isset($_POST['Submit']) && $_POST['RobotName'] != '' && $_POST['traction'] != '' && $_POST['passenger'] != '') {
$_SESSION['RobotName'] = $_POST['RobotName'];
$_SESSION['traction'] = $_POST['traction'];
$_SESSION['passenger'] = $_POST['passenger'];
} elseif (isset($_POST['Submit'])) {
echo "<br />";
echo "<br />";
echo "<strong>Error: You must complete all fields on this page!</strong><br />";
}
?>
<br />
<style>
div.container
{
width:30em;
border:1em solid;
}
div.box
{
box-sizing:border-box;
-moz-box-sizing:border-box; /* Firefox */
width: 50%;
border: 1em solid red;
float: left;
}
</style>
<?php if (isset($_POST['Submit']) && $_POST['RobotName'] != '' && $_POST['traction'] != '' && $_POST['passenger'] != '') {
echo "<h4>Your Settings (Click Play BotMod) to begin.</h4>";
?>
<div class="container">
<div class="box"><strong><i>Robot Name:</i></strong> <?php echo $_SESSION['RobotName']; ?><br /></div>
<div class="box"><strong><i>Traction:</i></strong> <?php echo $_SESSION['traction']; ?><br /></div>
<div class="box"><strong><i>Passenger:</i></strong> <?php echo $_SESSION['passenger']; ?><br /></div>
<div class="box"><strong>Play BotMod</strong><br /></div>
<div style="clear: both;"></div>
</div>
<?php
}
?>
</div>
</div>
<?php include("footer.php"); ?>
As you can see I have created an If statement on the play navigation.
<?php if (isset($_SESSION['RobotName'], $_SESSION['traction'], $_SESSION['passenger'])) {
?>
<u>Play</u>
<?php
} else {
?>
<u>Play</u>
<?php
}
?>
This doesn't work on any of the pages: customise.php index.php or instructions.php.
I have created the variables on the customise.php page with this code:
If (isset($_POST['Submit']) && $_POST['RobotName'] != '' && $_POST['traction'] != '' && $_POST['passenger'] != '') {
$_SESSION['RobotName'] = $_POST['RobotName'];
$_SESSION['traction'] = $_POST['traction'];
$_SESSION['passenger'] = $_POST['passenger'];
} elseif (isset($_POST['Submit'])) {
echo "<br />";
echo "<br />";
echo "<strong>Error: You must complete all fields on this page!</strong><br />";
}
?>
My index.php navigation code:
<u><font color="orange">Welcome</font><br /><br />
<u>How to Play?</u><br /><br />
<u>Customisation</u><br /><br />
<?php require_once("session_start.php"); ?>
<?php if (isset($_SESSION['RobotName'], $_SESSION['traction'], $_SESSION['passenger'])) {
?>
<u>Play</u>
<?php
} else {
?>
<u>Play</u>
<?php
}
?>
So does anyone know why the $_SESSION variables do not seem to work? I am using MAMP for this at the moment just in case that has something to do with it.
session_start(); must be called at the top of every page where you use the $_SESSION variable.
Documentation: http://php.net/function.session-start
use session start method
example :
session_start();
You need to call session_start() before accessing any session variables

Task-list manager app in PHP doesn't show the list of tasks

I am working on a task list manager for my class that I am in. I have to include sessions and I need to push each task into an array and be able to delete it from the array.
I think I am doing it correctly,but when I run the program, the task list shows up as empty. I think I may not be adding the tasks to the task-list array correctly, and I really would like a fresh set of eyes to take a look, because maybe you will see what I haven't.
index.php
<?php
if (isset($_POST['tasklist'])) {
$task_list = $_POST['tasklist'];
} else {
$task_list = array();
}
// put the array in a session variable
$_SESSION['tasks']=$task_list;
$_SESSION['errors']=$errors;
//start the session to last one year
$lifetime = 60 * 60 * 24 * 365; // 1 year in seconds
ini_set('session.gc_maxlifetime', $lifetime);
//session_set_cookie_params($lifetime, '/');
session_start();
$errors = array();
switch( $_POST['action'] ) {
case 'add':
$new_task = $_POST['newtask'];
if (empty($new_task)) {
$_SESSION['errors'] = 'The new task cannot be empty.';
} else {
$_SESSION['tasks'] = $new_task;
}
break;
case 'delete':
$task_index = $_POST['taskid'];
unset($task_list[$task_index]);
$_SESSION['tasks'] = array_values($_SESSION['tasks']);
break;
}
include('task_list.php');
?>
task_list.php
<?php
session_start(); // Access the current session.
?>
</head>
<body>
<div id="page">
<div id="header">
<h1>Task List Manager</h1>
</div>
<div id="main">
<!-- part 1: the errors -->
<?php if (count($_SESSION['errors']) > 0) : ?>
<h2>Errors</h2>
<ul>
<?php foreach($_SESSION['errors'] as $error) : ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<!-- part 2: the tasks -->
<h2>Tasks</h2>
<?php if (count($_SESSION['tasks']) == 0) : ?>
<p>There are no tasks in the task list.</p>
<?php else: ?>
<ul>
<?php foreach($_SESSION['tasks'] as $id => $task) : ?>
<li><?php echo $id + 1 . '. ' . $task; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<br />
<!-- part 3: the add form -->
<h2>Add Task</h2>
<form action="." method="post" >
<?php foreach($_SESSION['tasks'] as $task) :
array_push($_SESSION['tasks'],$task);
endforeach; ?>
<!--<input type="hidden" name="action" value="add"/> -->
<label>Task:</label>
<input type="text" name="<?php $_SESSION['tasks'] ?>" id="newtask" value="<?php echo $task; ?>" /> <br />
<label> </label>
<input type="submit" value="Add Task"/>
</form>
<br />
<!-- part 4: the delete form -->
<?php if (count($_SESSION['tasks']) > 0) : ?>
<h2>Delete Task</h2>
<form action="." method="post" >
<?php foreach($_SESSION['tasks'] as $task) :
$_SESSION['tasks']=array_diff($_SESSION['tasks'],$task);
endforeach; ?>
<!--<input type="hidden" name="action" value="delete"/> -->
<label>Task:</label>
<select name="taskid">
<?php foreach($_SESSION['tasks'] as $id => $task) : ?>
<option value="<?php echo $id; ?>">
<?php echo $task; ?>
</option>
<?php endforeach; ?>
</select>
<br />
<label> </label>
<input type="submit" value="Delete Task"/>
</form>
<?php endif; ?>
</div><!-- end main -->
</div><!-- end page -->
</body>
</html>
You need to call session_start() before any interaction with the session. So if the files are accessed separately then you need it in both. If task_list.php is only included in index.php then just move it to the top of index.php. Additionally, in your logic for the add action you dont have your syntax right to add a task to $_SESSION['tasks'] array, instead you are overwriting it with whatever is in $new_task.
This is from Murach's PHP book. The book becomes bunk after like the 10th chapter. No valid explanations or examples and questions that assume you already know PHP because it doesnt teach you what you need to know.

Categories