I have worked with code but I found this error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\ACM1\search.php on line 50

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

<?php

// Get the search variable from URL

$var = @$_GET ;
$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("localhost","username","password"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
///mysql_select_db("database") or die("Unable to select database"); //select which database we're using
include "connection.php";
// Build SQL Query
$query = "select * from accounts where Accounts_Id like \"%$trimmed%\"
order by Accountys_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: &quot;" . $trimmed . "&quot; returned zero results</p>";

// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</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: &quot;" . $var . "&quot;</p>";

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

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

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\">&lt;&lt;
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 &gt;&gt;</a>";
}

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

?>

Recommended Answers

All 10 Replies

I think query problem ...run your query in mysql ....or use mysql_affected_rows()

Thank for your reply but I tried but it did not work

do this in this way just copy and paste these line to your old lines,
for this i changes values according to my db....change those.. and also my config.php file is look like below...
search.php

<?php
include ("config.php");
global $conn;
// Build SQL Query 
$query = "select * from products where products_id like \"%$trimmed%\" order by products_id"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query,$conn);
$numrows=mysql_num_rows($numresults);?>

connfig.php

<?
//Edit these variables below
$host = "localhost";
$user = "root";
$pwd = "abc123";
$db = 'dcg';
/**
 * Connect to the mysql database.
 */
try
{
	$conn = mysql_connect($host, $user, $pwd);
		
if($conn)
	mysql_select_db($db, $conn) ;
}
catch(Exception $e)
{
	echo "Sorry Server is Busy...........Please try after some time.";
}
?>

you can use mysql_fetch_array()

// Build SQL Query
$query = "select * from products where products_id like \"%$trimmed%\" order by products_id";

//Get all the 
while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
    printf ("ProductID: %s  Description: %s", $row[0], $row["description"]);
}

MYSQL_BOTH = either number or the column name you can use

When I applied those I dont get error but when i changed the query to be
$query = "select * from accounts where Accounts_Name like \"%$trimmed%\"
order by Accountys_Name"; // EDIT HERE and specify your table and field names for the SQL query

WORKS the Accounts_Id is a primary key

Thank for your reply but I tried but it did not work

This might be because first time u load the page, ur $_GET parameter is empty and u didn't check it before running the query...
do something like

if (isset($_GET['q'])) {
// run the search query here
}

If this doesn't solves ur problem, echo ur query and copy and execute the result in mysql query browser to see if it works there...

I didn't read ur whole code though so apologies if i missd sumthn...
actually without code tags its not easy to read such lengthy codes

@phpbeginners.... u dont need to use MYSQL_BOTH with fetch array... its default value itself is MYSQL_BOTH...

well on you query order by Accountys_Name your not telling it how to order it by like ascending or descending or any other thing like that go look it up at mysql.org they have information there on

Correction:When I apply the following code works.
In my first thread i put Accounts_Id did not and gave me the error message as shown in the first thread.
WHY WHEN I USE Accounts_Id as primary key doesnt work

query = "select * from accounts where Accounts_Name like \"%$trimmed%\"
order by Accountys_Name"; // EDIT HERE and specify your table and field names for the SQL query

try this because you not telling the script how to order the query then it will not work alright if no order the it should be like this alright

$query = "select * from accounts where Accounts_Name like \"%$trimmed%\"";

and if you want order then order should look like this

order by example DESC

DESC is how you are telling it to order the query by and DESC is for descending order alright if you want it to be any other go look it up at mysql.org about order by alright

$query = "select * from accounts where Accounts_Id like '%$trimmed%'
order by Accountys_Id ";

try like this , it works fine. it will useful for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.