0

I am trying to fetch some data from my sql table, I've already done it but now it wont work with the same code. Here it is:

$cat = "";
$res = "";
$date = "";

$sql2 = "SELECT category, result, date FROM results WHERE user='" . $_SESSION['login_user'] . "'";
$result2 = mysql_query($sql);

$count2 = mysql_num_rows($result2);

if($count2 == 1)
{
    $row2 = mysql_fetch_assoc($result2);

    $cat = $row2["category"];
    $res = $row2["result"];
    $date = $row2["date"];


}

I keep getting the following error:

Notice: Undefined index: category in C:\xampp\htdocs\Own\RiskCarePHP\profile.php on line 59

Notice: Undefined index: result in C:\xampp\htdocs\Own\RiskCarePHP\profile.php on line 60

Notice: Undefined index: date in C:\xampp\htdocs\Own\RiskCarePHP\profile.php on line 61

Whats wrong with it?

Nurjan
  • 5,889
  • 5
  • 34
  • 54
Gabriel Luque
  • 117
  • 11

2 Answers2

0

There is an error in your syntax.

It should be

$result2 = mysql_query($sql2);

Please check with this

Amrinder
  • 69
  • 6
0

You are missing out the fact that when you do mysql_fetch_assoc(), it returns the array with index hence the result from mysql_query() has to be looped with while(). You are trying to directly access the associative array which is indexed and you are missing the index values to point to the keys like "category", "result" and "date".

<?php 
$cat = "";
$res = "";
$date = "";

$sql2 = "SELECT category, result, date FROM results WHERE user='" . $_SESSION['login_user'] . "'";
$result2 = mysql_query($sql);

$count2 = mysql_num_rows($result2);

if($count2 == 1)
{
    while ($row2 = mysql_fetch_assoc($result2)) {
       $cat = $row2["category"];
       $res = $row2["result"];
       $date = $row2["date"];
    }
}
?>

This will definitely work for you. Cheers!