0

Any ideas why this simple php code won't display results when trying to echo the data.

<?php
        {
            mysql_connect("localhost" , "" , "") or die (mysql_error());
            mysql_select_db("") or die(mysql_error());


            $pid=intval($_SESSION["User_id"]); 
            $query = "SELECT `car`, `details`, `price` FROM `Car``";

            //executes query on the database
            $result = mysql_query ($query) or die ("didn't query");

            //this selects the results as rows
            $num = mysql_num_rows ($result);    


            while($row=mysql_fetch_assoc($result))
            {

                $_SESSION['car'] = $row['car'];
                $_SESSION['details'] = $row['details'];
                $_SESSION['price'] = $row['price'];
            }
        }
        ?>  

        <?php echo $_SESSION['car']; ?>
        <?php echo $_SESSION['details']; ?>
        <?php echo $_SESSION['price']; ?>

Just testing at the moment to see if the car, price and details display from the database and they don't seem to.

7
  • 1
    You shouldn't be using mysql_ functions because they are deprecated. Use pdo instead. Commented Apr 9, 2013 at 2:05
  • Are the quotes inside the query going the correct direction? Looks like they are copy/paste characters that are not quotes Commented Apr 9, 2013 at 2:07
  • @JayRizzi, yeah it is going to the right place Commented Apr 9, 2013 at 2:08
  • Oh, there may be some issue with $_SESSION not showing immediately the values, some reason i recall having proba with that a while ago...is the only problem that the session doesnt store the values, or that the query just wont return, like if you changed the rows to just echo rather than session storage, are you getting results? Commented Apr 9, 2013 at 2:14
  • @JayRizzi, the query won't return. It just displays login. I have used $_SESSION on other application, can't figure out why it won't work on here Commented Apr 9, 2013 at 2:16

4 Answers 4

2

You missed session_start(); at start of page and change

$query = "SELECT `car`, `details`, `price` FROM `Car``";
                                                     ^

to

$query = "SELECT `car`, `details`, `price` FROM `Car`";
Sign up to request clarification or add additional context in comments.

5 Comments

Tried that. It just displays login
And i have got session start at the top
@user2236990 Can you describe It just displays login?
Yeah, basically when i visit that specific php page (car.php) it simply displays login in the middle of the page instead of car details
@user2236990 Show your full code. Whatever you have posted it's impossible to know where is problem. In your posted code we have solved your errors.
0

Are you expecting one or many results for this query ?
If many results, you are saving the last entry in the session.
If only one, just do : $row=mysql_fetch_assoc($result) instead of this while.

Comments

0

Check the query.Try to echo it, copy, paste in MySQL and run it. But you have $pid, have you put it in the query?

$query = "SELECT car, details, price FROM Car WHERE id = $pid ";

Comments

-1

I rather remove all backticks since non of those identifiers are reserved keywords.

$query = "SELECT car, details, price FROM Car";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.