-5

I'm recieving the above error when I try to run this code, I have tried multiple solutions, using fetch_array too:

$conn = mysql_connect('localhost', '-----', '-----','-----') 
or die('Error connecting to mysql');
$sql = "SELECT * FROM Subject";
$result = mysql_query($sql);
$row=null;
echo "<table>";
while( $row = $mysql_fetch_assoc[$result]){

echo "<tr><td>";
echo $data['SubjectNo'];
echo "</td><td>";
echo $data['SubjectName'];
echo"</td></tr>";
} 
echo "</table";
echo"urnan";

?>
2
  • 2
    $mysql_fetch_assoc !== mysql_fetch_assoc....(the $ makes a difference) - but you shouldn't be using the MySQL extension anyway..... it's 2016 now, not 2006 Commented Jan 14, 2016 at 10:37
  • 1
    Possible duplicate of PHP: "Notice: Undefined variable" and "Notice: Undefined index" Commented Jan 14, 2016 at 10:38

2 Answers 2

5

There is an error in the line

while( $row = $mysql_fetch_assoc[$result]){

mysql_fetch_assoc is a PHP function, not a variable. So no need to put a $ sign in front of it.

So it should read

while( $row = mysql_fetch_assoc($result)){
Sign up to request clarification or add additional context in comments.

Comments

1

Error is clear undefined variable means you are using a variable that not defined anywhere in your code.

What is the issue in your code?

You are using $mysql_fetch_assoc as a variable that is not defined in your code.

More important, $mysql_fetch_assoc it not equal to mysql_fetch_assoc as my other mate mentioned in comments.

Modified Code:

This should be use as:

while( $row = mysql_fetch_assoc($result)){
  // your stuff
}

And second issue in you code is these brackets [] it should be ()

Side Note:

I suggest you to use mysqli_* or PDO instead of mysql_* extension because its deprecated and not available in PHP 7.

References from PHP Manuals:

PHP Data Objects (PDO)

MYSQL Improve Extension (mysqli_*)

6 Comments

The dollar sign was my mistake, I am now receiving the error " mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in (line 19)"
@Jw13: now if you issue is resolved than chose the best answer and mark as accpeted by click on the left green tick.
the issue isnt resolved, as I now have a similar problem, do you know a solution to the response " mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in (line 19)"
@Jw13: this is another issue and new question friend.
$result = mysql_query($sql); if(!$result) { echo mysql_error(); } check this @Jw13 for errors.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.