-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";

?>
Jw13
  • 9
  • 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 – Mark Baker Jan 14 '16 at 10:37
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Ben Jan 14 '16 at 10:38

2 Answers2

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)){
dronus
  • 10,774
  • 8
  • 54
  • 80
Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15
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_*)

devpro
  • 16,184
  • 3
  • 27
  • 38
  • 1
    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 Jan 14 '16 at 14:17
  • @Jw13: now if you issue is resolved than chose the best answer and mark as accpeted by click on the left green tick. – devpro Jan 14 '16 at 14:19
  • 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 Jan 14 '16 at 14:25
  • @Jw13: this is another issue and new question friend. – devpro Jan 14 '16 at 14:27
  • $result = mysql_query($sql); if(!$result) { echo mysql_error(); } check this @Jw13 for errors. – devpro Jan 14 '16 at 14:30
  • @Jw13: this will help u for this issue???? http://stackoverflow.com/questions/3129374/how-to-prevent-this-error-warning-mysql-fetch-assoc-expects-parameter-1-to – devpro Jan 14 '16 at 14:32