1

I have this php file. The lines marked as bold are showing up the error :"mysql_query() expecets parameter 2 to be a resources. Well, the similar syntax on the line on which I have commented 'No error??' is working just fine.

 function checkAnswer($answerEntered,$quesId)
 {
  //This functions checks whether answer to question having ques_id = $quesId is satisfied by $answerEntered or not

  $sql2="SELECT keywords FROM quiz1 WHERE ques_id=$quesId";
  **$result2=mysql_query($sql2,$conn);**
  $keywords=explode(mysql_result($result2,0));

  $matches=false;
  foreach($keywords as $currentKeyword)
  {
   if(strcasecmp($currentKeyword,$answerEntered)==0)
   {
    $matches=true;
   }
  }

  return $matches;

 }

 $sql="SELECT answers FROM user_info WHERE user_id = $_SESSION[user_id]";
 $result=mysql_query($sql,$conn);         // No error??
 $answerText=mysql_result($result,0);

 //Retrieve answers entered by the user
 $answerText=str_replace('<','',$answerText);
 $answerText=str_replace('>',',',$answerText);
 $answerText=substr($answerText,0,(strlen($answerText)-1));
 $answers=explode(",",$answerText);

 //Get the questions that have been assigned to the user.
 $sql1="SELECT questions FROM user_info WHERE user_id = $_SESSION[user_id]";
 **$result1=mysql_query($sql1,$conn);**
 $quesIdList=mysql_result($result1,0);
 $quesIdList=substr($quesIdList,0,(strlen($quesIdList)-1));
 $quesIdArray=explode(",",$quesIdList);



 $reportCard="";
 $i=0;
 foreach($quesIdArray as $currentQuesId)
 {
  $answerEnteredByUser=$answers[$i];

  if(checkAnswer($answerEnteredByUser,$currentQuesId))
  {
   $reportCard=$reportCard+"1";
  }
  else
  {
   $reportCard=$reportCard+"0";
  }
  $i++;
 }

 echo $reportCard;
?>

Here is the file connect.php. It is working just fine for other PHP documents.

<?php
 $conn= mysql_connect("localhost","root","password");
 mysql_select_db("quiz",$conn);
?>
3
  • I marked the lines as bold but sadly it didn't come up. The error causing lines are marked within a pair of **. Commented Jun 11, 2010 at 15:08
  • At which point are you including connect.php? Commented Jun 11, 2010 at 15:09
  • You should always check query results for errors. Even if the SQL itself is fine, there's far too many other reaons for queries to fail to NOT check for error conditions. At least do an ... or die(mysql_error()) after each query. Commented Jun 11, 2010 at 18:34

3 Answers 3

5
$result2=mysql_query($sql2,$conn);

$conn is not defined in the scope of your function (even if you're including the connect.php file before that.

although you can use the suggestion to make $conn global, it's usually better practice to not make something global just for the sake of globalizing it.

i would instead pass $conn to the function as a parameter. this way, you can reuse the same function you wrote with different connections.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot. I am a beginner in PHP and I still have the habit of forgetting that global variables are not available in functions in PHP, unlike C. THanks a lot.
2

$conn isn't declared as a global so the function cannot access it, as it is not defined within it.

Either simply add

global $conn;

To the top of the function to allow it to access the $conn.

Or you can remove $conn from the mysql_query() statement. By default it will use the current connection (as mentioned in the comments below).

2 Comments

Or you can remove $conn from the mysql_query() statement. By default it will use the current connection.
thanks a lot. I am a beginner in PHP and I still have the habit of forgetting that global variables are not available in functions in PHP, unlike C. THanks a lot.
1

Where do you set $conn? It doesn't look like you set a connection within that function

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.