Help!


#1

Hey, I have question for this script, which should do live ajax search from sql database. I’m not getting this to work for me. can you guys give a hand. I’m trying to use this script to make an online dictionary.

image of my my Sql database table.

files: index.html,
[php]

Ajax search form
search form
[/php]

search.php,
[php]<?php

// file for database connection
include(‘inc/db.inc.php’);

// configuration file
include(‘inc/config.inc.php’);

if(isset($_GET[‘p’])) {
$page_number = $_GET[‘p’];
$arraySearch = $_GET[‘terms’];
$show_count = $_GET[‘count’];
settype($page_number, ‘integer’);
}
$nospaces = substr($_GET[‘terms’],0,4);
$offset = ($page_number - 1) * $records_number;
// check for an empty string and display a message.
if ($_GET[‘terms’] == “”) {
echo ‘

ex. write ´here and´ or ´search´ without quotes.
’;
// minim 3 characters condition
} else if(strlen($_GET[‘terms’]) < $limitchar) {
echo ‘
’. $limitchar .’ characters minimum
’;
// no spaces in first 4 letters
} else if(preg_replace(’/[a-zA-Z0-9]/’, ‘’, $nospaces)) {
echo ‘
Please use letters or numbers in first 4 characters
’;
} else {

// explode search words into an array
$arraySearch = explode(" “, $_GET[‘terms’]);
// table fields to search
$arrayFields = array(0 => $first_field, 1 => $second_field);
$countSearch = count($arraySearch);
$a = 0;
$b = 0;
$query = “SELECT * FROM $table_name WHERE (”;
$countFields = count($arrayFields);
while ($a < $countFields)
{
while ($b < $countSearch)
{
$query = $query.”$arrayFields[$a] LIKE ‘%$arraySearch[$b]%’";
$b++;
if ($b < $countSearch)
{
$query = $query." AND “;
}
}
$b = 0;
$a++;
if ($a < $countFields)
{
$query = $query.”) OR (";
}
}
$query = $query.") LIMIT $offset, $records_number;";
$search = mysql_query($query);

// get number of search results
$arrayFields = array(0 => $first_field, 1 => $second_field);
$countSearch = count($arraySearch);
$a = 0;
$b = 0;
$query = “SELECT * FROM $table_name WHERE (”;
$countFields = count($arrayFields);
while ($a < $countFields)
{
while ($b < $countSearch)
{
$query = $query."$arrayFields[$a] LIKE ‘%$arraySearch[$b]%’";
$b++;
if ($b < $countSearch)
{
$query = $query." AND “;
}
}
$b = 0;
$a++;
if ($a < $countFields)
{
$query = $query.”) OR (";
}
}
$query = $query.")";
$count_results = mysql_query($query);
$numrows = mysql_num_rows($count_results);

// no results
if($numrows == 0) {
echo ‘

No results found
’;

// show results
} else {

echo ’

'. $_GET['terms'] .' - '. $numrows .' results found

';

while($row = mysql_fetch_assoc($search)) {

$urltitle = str_replace(" “,”_", $row[‘title’]);

echo ’

'.$row['title'].''.$row['title'].'
'.$row['description'].'
'.$row['timesplayed'].'

played

'; } // pagination $maxPage = ceil($numrows/$records_number);

$nav = ‘’;
for($page = 1; $page <= $maxPage; $page++) {
if ($page == $page_number) {
$nav .= “$page”;
}
else
{
$nav .= “<a href=“javascript:htmlData(‘search.php’,'terms=”.$_GET[‘terms’].”&p=$page’)">$page";
}
}

if ($page_number > 1) {

$page = $page_number - 1;
$prev = “<a href=“javascript:htmlData(‘search.php’,'terms=”.$_GET[‘terms’].”&p=$page’)">«";

$first = “<a href=“javascript:htmlData(‘search.php’,'terms=”.$_GET[‘terms’].”&p=1’)">First";
}
else {
$prev = ‘’;
$first = ‘’;
}

if ($page_number < $maxPage) {
$page = $page_number + 1;
$next = “<a href=“javascript:htmlData(‘search.php’,'terms=”.$_GET[‘terms’].”&p=$page’)">»";

$last = “<a href=“javascript:htmlData(‘search.php’,'terms=”.$_GET[‘terms’].”&p=$maxPage’)">Last";
}
else {
$next = ‘’;
$last = ‘’;
}
echo $data;
echo "<div id=“results_bottom”>

$first $prev $nav $next $last

"; } } ?>[/php]

inc/config.inc.php

[php]

<?php $table_name = "tbl_customer"; //which table from database $first_field = "CustomerName"; //which field from table $second_field = "Address"; //which field from table $limitchar = 4; //minimum of characters $records_number = 10; // Number of records to show per page (different from 0) $page_number = 1;// default start page ?>

[/php]

and inc/db.inc.php
[php]

<?php $username = "munjututf_dic"; $password = "password"; $hostname = "localhost"; $database = "munjututf_dic"; mysql_connect($hostname, $username, $password) or die(mysql_error()); mysql_select_db($database) or die(mysql_error()); ?>

[/php]


#2
I'm not getting this to work for me.

That doesn’t give enough information for us to help you solve the problem.

The only thing I do see, you need to update the functions. mysql_ has been depreciated and should not be used at all.


#3

thanks for reply,

All I want is this scrip can ready my database tables and show them while I’m searching this terms.
by my understanding in inc/config.inc.php

[php]
$first_field = “CustomerName”; //which field from table
$second_field = “Address”; //which field from table
[/php]

means table columns and I’m sated them to CustomerName and Address and result should be CustomerName column is the title and Address is the disruption.

there is a working demo example of this script: http://demo.zmeutz.com/search/
My example which doesn’t work: http://dic.munjutut.fi/php/


#4

I fixed the connection
[php]
mysqli_connect($hostname, $username, $password) or die(mysqli_error());
mysqli_select_db($database) or die(mysqli_error());
[/php]

to

[php]
$connection = mysqli_connect($host,$user,$password,$db);
[/php]

I saw same command used in several times in search.php file also for exp.

[php]
$search = mysql_query($query);

[/php].

should I replace those too?


#5

That isn’t the same command. One is the connection the other is actually querying the database.

Learn prepared statements, next.