Page 1 of 1

php and mysql

Posted: Fri Jun 19, 2009 11:10 pm
by shoyle
Hi, i'm begeiner on php and mysql and im trying to make a search engine, the search result should display a url (and it does) with hiperlink to the page (sorry im not bery good at english..)
this is the code:


<?php

// Get the search variable from URL

$var = @$_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10;

// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}

// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("mysql host","user","pass"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using

// Build SQL Query
$query = "select * from searchengine where keywords like \"%$trimmed%\"
order by id"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
}

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";

// begin to show results set
echo "Results";
$count = 1 + $s ;

// now you can display the results returned (i think its here somewhere where ihave to make the change)
while ($row= mysql_fetch_array($result)) {
$title = $row["url"];

echo "$count.)&nbsp;$title" ;
$count++ ;
}

$currPage = (($s/$limit) + 1);

//break before paging
echo "<br />";

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>&nbsp&nbsp;";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";

?>


thanks a lot!!!

Re: php and mysql

Posted: Fri Jun 19, 2009 11:35 pm
by Eric!
Here is your code again using php tags to make it readable.

Code: Select all

<?php
 
// Get the search variable from URL
 
$var = @$_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable
 
// rows to return
$limit=10;
 
// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
 
// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
 
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("mysql host","user","pass"); //(host, username, password)
 
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using
 
// Build SQL Query
$query = "select * from searchengine where keywords like \"%$trimmed%\"
order by id"; // EDIT HERE and specify your table and field names for the SQL query
 
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
 
// If we have no results, offer a google search as an alternative
 
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
}
 
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
 
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
 
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
 
// begin to show results set
echo "Results";
$count = 1 + $s ;
 
// now you can display the results returned (i think its here somewhere where ihave to make the change)
while ($row= mysql_fetch_array($result)) {
$title = $row["url"];
 
echo "$count.)&nbsp;$title" ;
$count++ ;
}
 
$currPage = (($s/$limit) + 1);
 
//break before paging
echo "<br />";
 
// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>&nbsp&nbsp;";
}
 
// calculate number of pages needing links
$pages=intval($numrows/$limit);
 
// $pages now contains int of pages needed unless there is a remainder from division
 
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
 
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
 
// not last page so give NEXT link
$news=$s+$limit;
 
echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
 
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
 
?>
While I don't know what your problem or question is, I can tell you line 64 is wrong. Use single quotes forassociated arrays.

Code: Select all

$title = $row['url'];

Re: php and mysql

Posted: Sat Jun 20, 2009 3:05 am
by requinix
Eric! wrote:While I don't know what your problem or question is, I can tell you line 64 is wrong. Use single quotes forassociated arrays.

Code: Select all

$title = $row['url'];
...or double quotes. There's nothing wrong with that line.

Re: php and mysql

Posted: Sat Jun 20, 2009 5:22 am
by terier
... or no quotes at all... (I know it's not a good technique, but it works :) )

Re: php and mysql

Posted: Sat Jun 20, 2009 8:47 am
by shoyle
when i use this search engine it displays my results: the keyword and a url that i have on my database, i want to be able to click on the url and get sended to the page of the url.
I'm getting something like this: ww.google.com but i want something like http://www.google.com linked to whatever page it says.

Thanks again:D

Re: php and mysql

Posted: Sat Jun 20, 2009 12:40 pm
by Eric!
No wonder nothing stuck out as wrong except that one associated array declaration, which appearently wasn't wrong.

Your problem is just a html formatting thing.
line 66 changes to

Code: Select all

echo "$count.)&nbsp;<a href=\"http://$title\">$title</a>";
I'm assuming here $title doesn't include the 'http://' part of the url.

I think I got the escape codes right. If not there seem to be plenty of folks around to pipe up.

Re: php and mysql

Posted: Sat Jun 20, 2009 6:12 pm
by shoyle
Nope, that didnt work.. it still comes up as "just words" but no matter how many times i click it wont work.. :banghead: and no matter what i type on the database (- http:// or + http://) it wont work :?

Re: php and mysql

Posted: Sat Jun 20, 2009 6:41 pm
by Eric!
shoyle wrote:...and no matter what i type on the database (- http:// or + http://) it wont work :?
What does that mean?

I'm just guessing because I don't have all the information. You're just writing this to the browser right? Can you post the html source that is output? Also can you post what the value of $title is as an example? I'm sure there is just a formatting problem.

Re: php and mysql

Posted: Sat Jun 20, 2009 7:52 pm
by shoyle
in my database i got something like this:

id..
title: inicio
description: starting page (which i dont know how to display but aint as important as url)
url: http://www.inmosuhr.com
keywords: inicio home index


so it displays the keywords on top
the number of results and the url...

is that what ure asking? :D