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.
5
  • Remove endwhile thats for the alternative syntax Commented Jun 24, 2014 at 2:02
  • 2
    @meda - It's not beautiful, but it's actually legal (he's using both syntaxes) Commented Jun 24, 2014 at 2:04
  • @ChrisForrence yes you are rigjt thats why i removed my answer , i just voted yours up ! ;) Commented Jun 24, 2014 at 2:06
  • @meda - I did include your comment into my answer; while it was done correctly, you do have a valid point. Commented Jun 24, 2014 at 2:07
  • thanks guys. thanks to you I've solved this in 2 minutes. Commented Jun 24, 2014 at 2:13

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

Comments

2

Try with...

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

And you won't confuse to PHP interpreter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.