0

I have this code and i only want to display a record from the table :

<?php
session_start();
echo $_SESSION['idu'];
$connect=mysql_connect("localhost","root","");
mysql_select_db("bilet");
$queryreg=mysql_query("SELECT * FROM oferte WHERE idu='$_SESSION['idu']' ");
while($row = mysql_fetch_array($queryreg)):
  {
    echo   $row['adresa'];
  echo  $row['descriere'] ;
  echo $row['stele'] ;
    echo $row['pret'] ;
  }
  endwhile;


?>

I get this error :

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in D:\xampp\htdocs\bilet\viz.php on line 6.

 The table has more then 4 rows , but i just wanna see if it works.
stefan9976
  • 81
  • 1
  • 3
  • 11

2 Answers2

4

The specific issue has to do with your line 6 (as the error suggests). You're not properly including your session variable into the string. Try this instead:

$queryreg = mysql_query("SELECT * FROM oferte WHERE idu='" . $_SESSION['idu'] . "'");

That being said, there are a few other things to keep in mind. If the user ever sets $_SESSION['idu'], then your code is highly vulnerable to an SQL injection attack (read up here on how to prevent it)


As an aside, as @meda pointed out, you're actually using two different ways of doing while-loops. While you have it technically correct, it's certainly not a recommended way of doing such. I suggest choosing one or the other:

// Choice 1: Recommended
while ($row = mysql_fetch_array($queryreg)) {
    echo $row['adresa'];
    echo $row['descriere'];
    echo $row['stele'];
    echo $row['pret'];
}

// Choice 2
while ($row = mysql_fetch_array($queryreg)):
    echo $row['adresa'];
    echo $row['descriere'];
    echo $row['stele'];
    echo $row['pret'];
endwhile;
Community
  • 1
  • 1
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
2

Try with...

$queryreg=mysql_query("SELECT * FROM oferte WHERE idu='" . $_SESSION['idu'] . "'");

And you won't confuse to PHP interpreter.

Zerquix18
  • 769
  • 6
  • 19