0

Hi guys, could you please check this code for me. basically displaying data from two table.
i get error massage like...
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\dis_take.php on line 91

<?php  
 		
 	$con = mysql_connect("localhost","root","");
	if (!$con)
  	{
  	die('Could not connect: ' . mysql_error());
  	}

	mysql_select_db("uni", $con);
	
$result = mysql_query("SELECT take.StudentID,student.StudentName,take.CourseID,course.CourseName FROM take,student,course WHERE take.StudentID = student.StudentID AND take.CourseID = course.CourseID AND StudentID LIKE '$_POST[sid]%' ORDER BY StudentID ASC");

echo"<br>";
		
echo "<center><table width=700 border=1>";
echo "<tr><th>StudentID</th><th>StudentName</th><th>CourseID</th><th>CourseName</th></tr>";

while($row = mysql_fetch_array ($result))

{

echo "<tr><td>";
echo $row['StudentID'];
echo "</td><td>";
echo $row['StudentName'];
echo "</td><td>";
echo $row['CourseID'];
echo "</td><td>";
echo $row['CourseName'];
echo "</td></tr>";

}

echo "</table>";
		
	mysql_close($con); 
?>
4
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by mtvaran
0

Try to use echo mysql_error() just after mysql_query() function in order to see the actual MySQL error.

0

It looks like your sql query has an error in it somewhere. I think the problem is with the $_POST[sid] not being encapsulated change it to {$_POST['sid']} . It should work.

If not then try this. Change the sql query to the following.

$sql = "SELECT take.StudentID,student.StudentName,take.CourseID,course.CourseName FROM take,student,course WHERE take.StudentID = student.StudentID AND take.CourseID = course.CourseID AND StudentID LIKE '$_POST[sid]%' ORDER BY StudentID ASC";
echo $sql;
$result=mysql_query($sql);

The above will print out the sql query on the page. I hope you are using PHPmyAdmin. Run the query there and see if you get the response you are looking for. If you find an error, fix the sql statement and try again.

Let us know if you still have a problem.

0

It looks like your sql query has an error in it somewhere. I think the problem is with the $_POST[sid] not being encapsulated change it to {$_POST['sid']} . It should work.

If not then try this. Change the sql query to the following.

$sql = "SELECT take.StudentID,student.StudentName,take.CourseID,course.CourseName FROM take,student,course WHERE take.StudentID = student.StudentID AND take.CourseID = course.CourseID AND StudentID LIKE '$_POST[sid]%' ORDER BY StudentID ASC";
echo $sql;
$result=mysql_query($sql);

The above will print out the sql query on the page. I hope you are using PHPmyAdmin. Run the query there and see if you get the response you are looking for. If you find an error, fix the sql statement and try again.

Let us know if you still have a problem.

this is my full query for searching data from multiple table. still i could not get any result. could anyone check please where am i made mistake?

<?php  
 		
 	$con = mysql_connect("localhost","root","");
	if (!$con)
  	{
  	die('Could not connect: ' . mysql_error());
  	}

	mysql_select_db("uni", $con);
	
$sql = "
    SELECT take.StudentID
          ,student.StudentName
          ,take.CourseID
          ,course.CourseName
    FROM take
        ,student
        ,course
    WHERE take.StudentID = student.StudentID
    AND take.CourseID = course.CourseID
    AND take.StudentID LIKE '$_POST[sid]%'
    ORDER BY take.StudentID ASC
";

$result = mysql_query($sql) or trigger_error('MySQL Error: ' . mysql_error(), E_USER_ERROR);

echo"<br>";
		
echo "<center><table width=700 border=1>";
echo "<tr><th>StudentID</th><th>StudentName</th><th>CourseID</th><th>CourseName</th></tr>";

while($row = mysql_fetch_array ($result))
echo mysql_error();
{
 
echo "<tr><td>";
echo $row['StudentID'];
echo "</td><td>";
echo $row['StudentName'];
echo "</td><td>";
echo $row['CourseID'];
echo "</td><td>";
echo $row['CourseName'];
echo "</td></tr>";

}

echo "</table>";
		
	mysql_close($con); 
?>
0

Its difficult to find out what's wrong from the code itself. It comes down to debugging and finding the cause of the problem yourself. Did you check out the result from the echo $sql; as I told you before? What output did it give?

Did it print something like: SELECT take.StudentID,student.StudentName,take.CourseID,course.CourseName FROM take,student,course WHERE take.StudentID = student.StudentID AND take.CourseID = course.CourseID AND take.StudentID LIKE '%' ORDER BY take.StudentID ASC; Is your $_POST[sid] getting the value you want it to pull?
Did you encapsulate the variables like I told you to?
Are you getting an error or just not getting any data?
Is StudentID a unique ID. If so why do you need to use LIKE?

mysql_fetch_array() is giving you an error because $result is not a resource. $result is not a resource because you sql query is incorrect.

Edited by sudeepjd: n/a

0

Its difficult to find out what's wrong from the code itself. It comes down to debugging and finding the cause of the problem yourself. Did you check out the result from the echo $sql; as I told you before? What output did it give?

Did it print something like: SELECT take.StudentID,student.StudentName,take.CourseID,course.CourseName FROM take,student,course WHERE take.StudentID = student.StudentID AND take.CourseID = course.CourseID AND take.StudentID LIKE '%' ORDER BY take.StudentID ASC; Is your $_POST[sid] getting the value you want it to pull?
Did you encapsulate the variables like I told you to?
Are you getting an error or just not getting any data?
Is StudentID a unique ID. If so why do you need to use LIKE?

mysql_fetch_array() is giving you an error because $result is not a resource. $result is not a resource because you sql query is incorrect.

YES now it works, i had to take the "line 33" off.i previously put it for error finding. :)

Edited by mtvaran: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.