Jump to content

PHP/MySQL Query Trouble


krysco

Recommended Posts

I've created a database using MySQL and I'm trying to use PHP to search the database and output the results in a table. I got it to work without searching and I got it to print out ALL of the database's entries. I am trying to be able to get more specific results by searching the database and outputting only the results I asked for. I tried to modify the first code I used adding a script that is supposed to use a variable from a search page as the search terms with which php is to query the MySQL database. I got all of this code from researching other websites and taking bits and pieces from them and trying to put them together. I think that one site's method may not be completely compatible with another. The main two sites I used are these:

 

Free Webmaster Help (most of the code came from here):

http://www.freewebmasterhelp.com/tutorials/phpmysql/1

This site had a great tutorial but it sort of skimped on the search feature so I went to this site...

 

http://www.devpapers.com/article/306

This site was all about the search feature but I'm not sure that the method it's using is compatible with the setup I got from the first site.

 

When I try the search my browser displays this error:

Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /var/www/tutorial/search3.php on line 21

And the below that it shows the headers for my table but has no results.

 

Here is the code of the two pages...

 

Search Page:

<form name="search_contacts" action="search3.php" method="get">
Search By Last Name:<input name="q" type="text" size="15" /> <br />
  <input type="submit" name="Submit" value="Search" class="button" />
</form>

 

Search Script and Results Page:

<?
$username="root";
$password="catch2@";
$database="contacts";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM `$database`WHERE (
`id` LIKE '%$q'%
OR `first` LIKE '%$q%'
OR `last` LIKE '%$q%'
OR `phone` LIKE '%$q%'
OR `mobile` LIKE '%$q%'
OR `fax` LIKE '%$q%'
OR `email` LIKE '%$q%'
OR `web` LIKE '%$q%'
)";

$result=mysql_query ($query);
$num=mysql_numrows($result);

mysql_close;
?>

<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Phone</font></th>
<th><font face="Arial, Helvetica, sans-serif">Mobile</font></th>
<th><font face="Arial, Helvetica, sans-serif">Fax</font></th>
<th><font face="Arial, Helvetica, sans-serif">E-mail</font></th>
<th><font face="Arial, Helvetica, sans-serif">Website</font></th>
</tr>

<?
$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo $first." ".$last; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $phone; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $mobile; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $fax; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo $email; ?>">E-mail</a></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="<? echo $web; ?>">Website</a></font></td>
</tr>

<?
$i++;
}


echo "</table>";

 

Please Help!  ???

Link to comment
https://forums.phpfreaks.com/topic/137687-phpmysql-query-trouble/
Share on other sites

`id` LIKE '%$q'%

 

theres a problem.. idk if its THE problem

 

`id` LIKE '%{$q}%'

 

is more like it but I guess the { } are not so manditory.. thats just how I code

 

oh and right after the

 

<?

 

try

 

$q = $_REQUEST['q'];

Only a quick example mate.

 

Are you posting $q or getting $q from a url

 


<?php

$q=$_POST['q']; // Posting from a form.

$q=$_GET['q']; // Getting q from a url.

$q=$_REQUEST['q']; //  Using i ver post or get. 

?>

 

 

Also you need to name the submit button

 

so it got a condition.

 

example

<?php

if(isset($_POST['submit'])){

// database

}
?>

 

 

 

If it a url condition you need to add a condition.

 

example

 

url example http;//www.what_ever.com?cmd=$g

<?php

if($_GET['cmd']==$q){

//database query.
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.