Forum Moderators: coopster

Message Too Old, No Replies

simple search script

         

anigma

9:35 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



hi, i'm new to php.. i was working on a simple php search script that uses mysql.. but the problem is, how do i make the script echo out, Name: The name of the search (if it is in the mysql database) No search found (if it isn't in the database) and No search given if you just click on the search button..

here's the script:

<?php

$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("vinyldealers", $db);
$query = mysql_query("SELECT shops.name FROM shops WHERE name LIKE '%$name%'");
if(!mysql_num_rows($query)) {
echo "Sorry, no matching results.";
exit();
}
while ($record = mysql_fetch_assoc($query)) {
while (list($fieldname, $fieldvalue) = each ($record)) {
echo $fieldname.": <b>".$fieldvalue."</b><br>";
}
echo "<br>";
}

?>

anigma

6:02 am on Sep 16, 2005 (gmt 0)

10+ Year Member



no one?

dkin

7:59 am on Sep 16, 2005 (gmt 0)

10+ Year Member



I dont see why you have a while loop inside of a while loop, but I think something like this should work for you.

$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("vinyldealers", $db);
$query = mysql_query("SELECT shops.name FROM shops WHERE name LIKE '%$name%'");
if(!mysql_num_rows($query))
{

echo "Sorry, no matching results.";
exit();

}

else
{
while ($record = mysql_fetch_assoc($query)) {

echo "Name";

}
}

anigma

12:42 pm on Sep 17, 2005 (gmt 0)

10+ Year Member



i just get Name and nothing else

dkin

6:38 pm on Sep 17, 2005 (gmt 0)

10+ Year Member



because you have to insert the variables you would like to return, change name to the name of the columns you would like displayed.

anigma

7:00 pm on Sep 17, 2005 (gmt 0)

10+ Year Member



so i should type shops.name in there?

dkin

7:20 pm on Sep 17, 2005 (gmt 0)

10+ Year Member



$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("vinyldealers", $db);
$query = mysql_query("SELECT name FROM shops WHERE name LIKE '%$name%'");
if(!mysql_num_rows($query))
{

echo "Sorry, no matching results.";
exit();

}

else
{
while ($row = mysql_fetch_array($query)) {

echo $row['name'];

}
}

It would look something like that, now just take variable I have inserted and alter it to fit the rest of your column names.

anigma

9:20 pm on Sep 17, 2005 (gmt 0)

10+ Year Member



it works now, but if i for example search for blabla then i still get the Test name i saved in mysql

dkin

10:54 pm on Sep 17, 2005 (gmt 0)

10+ Year Member



post your code, your query and your results and I will see what I can do.

anigma

8:35 am on Sep 18, 2005 (gmt 0)

10+ Year Member



<?php

$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("vinyldealers", $db);
$query = mysql_query("SELECT name FROM shops WHERE name LIKE '%$name%'");
if(!mysql_num_rows($query))
{
echo "Sorry, no matching results.";
exit();

}

else
{
while ($row = mysql_fetch_array($query)) {

echo $row['name'];

}
}

?>

all i want to do is, when i search for a name i have in my database, then i get it out on my website.. however, if i do not have that name in the database i want it to echo out Sorry, no search results...

and when the name is echoed out, it should be something like: Name <bold>the name i searched for</bold>

if possible, could you like maybie make the <bold>name i searched for</bold> to a link?

that's all...

now, when i search for test i just get up Test on the screen.. but when i search for blabla i get Test up again. I don't get Sorry, no search results....

that's all i want

dkin

6:31 pm on Sep 18, 2005 (gmt 0)

10+ Year Member



<?php

$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("vinyldealers", $db);
$query = mysql_query("SELECT name FROM shops WHERE name regexp '$search'");
if(!mysql_num_rows($query))
{
echo "Sorry, no matching results for the term $search";
exit();

}

else
{
while ($row = mysql_fetch_array($query)) {

echo 'Name: <b><a href="">'.$row['name'].'</a></b>';

}
}

?>

Use that code, change the search variable on your form to $search, this way there is no confusion, input where you would like the variables to link to and you should be ok.

anigma

7:33 pm on Sep 18, 2005 (gmt 0)

10+ Year Member



ok, i didn't quite get that, where should i change the variable to $search?

in the form where i type in the search?

here's the form..

<form action="sok.php" method="post">
<center><div class="style1" style="padding:10 ">Søk etter Film tittel
<input type="text" style="margin:6px;width: 100px;" name="name" class="input">
<br>
<input type="submit" value="Start søk" name="Søk" class="knapp" />
</center><br>
</form>

dkin

7:38 pm on Sep 18, 2005 (gmt 0)

10+ Year Member



<form action="sok.php" method="post">
<center><div class="style1" style="padding:10 ">Søk etter Film tittel
<input type="text" style="margin:6px;width: 100px;" name="search" class="input">
<br>
<input type="submit" value="Start søk" name="Søk" class="knapp" />
</center><br>
</form>

Use that

anigma

12:06 pm on Sep 19, 2005 (gmt 0)

10+ Year Member



i got this error on the page..

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\public_html\www\resultsbyname.php on line 6
Sorry, no matching results for the term

dkin

9:24 pm on Sep 19, 2005 (gmt 0)

10+ Year Member



well, this scriptworks perfectly for me I tested it on my database, so it is a problem with your dbs then, make sure the names are all correct and that your only trying to extract the actual name. I changed the SELECT name FROM to a column that didnt exist and I got the results you described, so MAKE SURE that it is the column you are looking for.

<?php

$db = mysql_connect("localhost", "user", "pass");
mysql_select_db("vinyldealers", $db);
$query = mysql_query("SELECT name FROM shops WHERE name regexp '$search'");
if(!mysql_num_rows($query))
{
echo "Sorry, no matching results for the term $search";
exit();

}

else
{
while ($row = mysql_fetch_array($query)) {

echo 'Name: <b><a href="">'.$row['name'].'</a></b><br>';

}
}

?>

anigma

7:02 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



heh, it didn't work.. :\ but i managed to make a new script.. take a look, it works fine but i only have one problem..
when you search for a name, then it gives out a link to the name, but how can i for example make a link like, the_best.php, if i would search for The best and i have that in my database, then i only get The/ as the link, but what i need is the_best.php, how do i do that?

<?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>Skriv inn en film tittel, eller en del av navnet...</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("localhost","user","pass"); //(host, username, password)

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

// Build SQL Query
$query = "select name from shops where shops.name like \"%$trimmed%\"
order by shops.name"; // 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 "";
echo "";

// google
echo "";
}

// 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>Ditt søk for: <b> $var </b> </p>";

// begin to show results set

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["name"];

echo "Navn: <b><a href=$row[name]>$row[name]</a></b>";
$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 "";
}

// 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 "";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Resultater, $a av $numrows</p>";

?>

<br><br><br><p>Nytt søk</p>

<form name="form" action="test2.php" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>

[edited by: jatar_k at 1:41 am (utc) on Sep. 22, 2005]
[edit reason] generalized user/pass [/edit]

anigma

7:03 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



thanks for your help btw :)

anigma

7:04 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



it isn't actually my script, but i did some changes on it...

dkin

11:25 pm on Sep 21, 2005 (gmt 0)

10+ Year Member



first of all edit your post and remove your username and password for your database.

Second of all, do you think you can target what your trying to do a little better, that is a 113 line script, I need to know exactly whats not working for you.

anigma

7:08 pm on Sep 23, 2005 (gmt 0)

10+ Year Member



i've put some informasion in my mysql and when i search for them in the search form then i get a link for the name i search for.. however if i search for 'test' then i get a link to, [localhost...] now the thing here is, i want the 'name' to be like test.php, or new_news.php, (if i search for names that consist of two words)..

anigma

7:10 pm on Sep 23, 2005 (gmt 0)

10+ Year Member



i want single words to be like blabla.php, and two words or more to be with underline _, for example, the_boondock_saints.php or some_page.php

mcibor

9:47 am on Sep 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe the solution to your problem would be sth else. You can store the desired url in the database!

CREATE TABLE shops (
id int not null auto_increment,
name varchar(25) not null,
url varchar(25) not null
#...
PRIMARY KEY(id));

and when you do the search:
$query = "SELECT name, url FROM shops WHERE shops.name LIKE '%$trimmed%'
ORDER BY shops.name";

then you can make the link as
echo "Navn: <b><a href=".$row['url'].">".$row['name']."</a></b>";

Best regards
Michal Cibor

anigma

9:55 am on Sep 24, 2005 (gmt 0)

10+ Year Member



i get this error when trying to create the shops table,

MySQL sa:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(id))' at line 6

mcibor

10:09 pm on Sep 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, there's a comma after url:

CREATE TABLE shops (
id int not null auto_increment,
name varchar(25) not null,
url varchar(25) not null,
#...
PRIMARY KEY(id));

Or if the table is already there, you can add a new column:

ALTER TABLE shops ADD url varchar(25) not null AFTER name;

Best regards
Michal Cibor

anigma

12:23 pm on Sep 26, 2005 (gmt 0)

10+ Year Member



i got this error,

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\public_html\resultater.php on line 36
Couldn't execute query

but when i add things into the table? do i add in name or url? or both?

mcibor

8:50 pm on Sep 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the query you put both name and url.
In this example you posted before the

name ; url

test ; test.php
boondock saints ; boondock_saints.php

Do you see now?
It's more flexible solution, as you may wish to someday change the links to another dir, or whatever.

To automatize the process

$name = "test";
$url = str_replace(" ", "_", $name).".php";//url = test.php

$name = "boondock saints";
$url = str_replace(" ", "_", $name).".php";//url = boondock_saints.php

Best regards
Michal Cibor

anigma

12:22 pm on Sep 27, 2005 (gmt 0)

10+ Year Member



so when i search for test i get test.php? but what if i search for something else? do i get like #*$!_blabla.php if i search for that?

anigma

12:24 pm on Sep 27, 2005 (gmt 0)

10+ Year Member



when i search for like something_heh1 and something_heh2 then i need to get something_heh1.php on all my searches, not just the ones who i set in the script..

mcibor

9:29 pm on Sep 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Now I don't understand. Tell us in the first place. What do you wish to acomplish with these files? What data do you want to put into them?

Explain please

Michal Cibor