<?php
$SearchType = $_POST["SearchPlace"];
$host= 'localhost';
$user= 'user';
$passwd= 'password';
$database= 'dbase';
$table1= 'BUS';
$table2= 'STUDENT';

$connect= mysql_connect($host, $user, $passwd);
mysql_select_db($database);
$pquery = "select busNo from $table1 where place like '".$SearchType."'";

$squery = "select * from $table2 where busNo like '".$pquery."'"; 

$run = mysql_query($squery);
$num_results = mysql_num_rows($run);




if (!$SearchType )
{
echo "<b>You have not selected search details. Please go back and try again.</b>";
}
else
{

if (!$connect)
{
echo"</b>Error: Could not connect to database. Please try again later.</b>";
}

else
{
for ($i=0; $i<$num_results; $i++)
{ 
$row = mysql_fetch_array($run);

$IDNo= stripslashes($row["IDNo"]);
$sName= stripslashes($row["sName"]);
$startDate= stripslashes($row["startDate"]);
$endDate = stripslashes($row["endDate"]);
$sPhone= stripslashes($row["sPhone"]);
$OBox= stripslashes($row["OBox"]);
$busNo= stripslashes($row["busNo"]);

echo "<tr>\n";
echo "<td align=left>\n";
echo "<p> Student ID: ".$IDNo."";
echo "<br>Student Name: ".$sName."";
echo "<br>Starting Date: ".$startDate."";
echo "<br>Ending Date: ".$endDate."";
echo "<br>Student Phone: ".$sPhone."";
echo "<br>P.O.Box: ".$OBox."";
echo "<br>Bus number: ".$busNo."";
echo "</p>";
echo "</td></tr>\n";

echo "\n";
echo "\n";
echo "</table>\n";
}

}
}
?>

Hi I am getting this warning and I don't know why?!! :sad: :!:

please can you help me to find the error

Dani AI

Generated

Note for : that warning means the call to mysql_num_rows() received something other than a valid result resource (usually FALSE). In other words the SELECT failed before you tried to count rows. As suggested, enable query error reporting and echo the final SQL so you can run it directly against the database; that will show the real syntax or runtime error.

The main logical bug (as pointed out) is treating a SELECT as a literal string inside a LIKE. You probably meant to match STUDENT rows whose busNo exists in BUS for the given place. Two correct patterns:

-- subquery approach
SELECT * FROM STUDENT
WHERE busNo IN (SELECT busNo FROM BUS WHERE place LIKE '%search-term%');

-- join approach (usually more efficient)
SELECT s.* FROM STUDENT AS s
JOIN BUS AS b ON s.busNo = b.busNo
WHERE b.place LIKE '%search-term%';

Practical improvements and a safe example (use prepared statements, validate input, and check execution success):

$search = trim($_POST['SearchPlace'] ?? '');
$pdo = new PDO('mysql:host=localhost;dbname=dbase','user','password',
               [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare(
  'SELECT s.* FROM STUDENT s JOIN BUS b ON s.busNo = b.busNo WHERE b.place LIKE :p'
);
$stmt->execute([':p' => "%$search%"]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

Final tips: ensure the POST key is set and not empty before running queries, include wildcards with LIKE if you intend substring matches, always check the query result before calling row-counting functions, and escape output (htmlspecialchars) when printing database values. If you must keep older mysql_* calls, explicitly test for FALSE after mysql_query and inspect mysql_error() instead of calling mysql_num_rows immediately.

Your query is probably failing. Try

$run = mysql_query($squery) or die(mysql_error());

And see if you get an error.

Or echo out your SQL statment and trying running it against the database.

I tried

$run = mysql_query($squery) or die(mysql_error());

I got this message "Query was empty"

Wat does this mean?

$pquery = "select busNo from $table1 where place like '".$SearchType."'";
 
$squery = "select * from $table2 where busNo like '".$pquery."'";

If $_POST was given as "Foobar" (for want of a better example), $squery would be set to:

select * from STUDENT where busNo like 'select busNo from BUS where place like 'Foobar''

Try running that by itself :-|

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.