Ifeanyi Omeata C.
Ifeanyi Omeata C.

Reputation: 23

Fetch data from a row from mysql database

I need to display a reply data on my page from my 'feedback' field in my mysql table. I want each user to have a different 'feedback' response stored per row and fetched when the user logs into a page through a session. I have set up my database but find it difficult forming the php code to view the feedback on my page...please can someone help../

       <?php
       session_start();

       if ($_SESSION['username'])
        {

       $con = mysqli_connect('localhost','root','');
       if (!$con)
        {
        die('Could not connect: ' . mysqli_error($con));
            }

        mysqli_select_db($con,"loginsession");
        $username = $_SESSION['username'];
        $sql="SELECT * FROM users WHERE username = $username";

        $result = mysqli_query($con,$sql);

        $feedback = mysql_query("SELECT feedback FROM users WHERE username='$username'");

        echo $feedback;


          }

       else 
    header("Location: index.php");  

      ?>

Upvotes: 1

Views: 238

Answers (3)

Asad Raza
Asad Raza

Reputation: 36

<?php
session_start();
if ($_SESSION['username'])
{
   $con = mysqli_connect('localhost','root','');
   if (!$con)
   {
        die('Could not connect: ' . mysqli_error($con));
   }
   mysqli_select_db($con,"loginsession");
   $username = $_SESSION['username'];
   $sql='SELECT feedback FROM users WHERE username = "'.$username.'"';
   $result = mysqli_query($con,$sql);
   while($row = mysqli_fetch_array($result))
   {
     echo $row['feedback'];
   } 
}
else 
  header("location: index.php");  
?>

Upvotes: -1

Neo
Neo

Reputation: 11587

$sql = mysql_query("SELECT feedback FROM users WHERE username='{$username}' LIMIT 1");
$feedback = mysql_fetch_assoc($sql);
echo $feedback[0];

Upvotes: 1

Kenny
Kenny

Reputation: 1553

$feedback in this case is not a string, its a mysql resource. You need to fetch each row individually with something like:

echo "<PRE>";
while ($row = mysql_fetch_assoc($feedback)) {
    print_r($row);
}

Also you should put $username through mysql_real_escape_string() or else your code may be vulnerable to SQL injection attacks.

Edit: (Disclaimer) The method you are using and my suggestion are very outdated and have been depreciated in php5.5 I suggest you look into prepared statements.

Upvotes: 6

Related Questions