1

please assist. i have a database with a couple of tables and rows and i want the php to print specific rows as and when i want it to. at the moment it renders all the content of the spesific table on my webpage. in future, i would like it to display the contents of a specific table if a cirtain user is logged in so im going to do that when i understand if statements and get over this hurdle 1st. my code is as follows:

 <?php
include 'connect-mysql.php';

echo "<br/>";

$query  = "SELECT CUSTOMER_NAME, RAMSCODE  FROM customer";
$result = mysql_query($query) or die (mysql_error());

while($row = mysql_fetch_array($result))
{
    echo "{$row['CUSTOMER_NAME']} <br>" .
         "RAMSCODE: {$row['RAMSCODE']} <br>" ;
} 

?>
1
  • 3
    How do you plan on identifying that specific user? How do you know who's logged in? a cookie? a session? what? Commented Nov 15, 2011 at 9:53

3 Answers 3

4

To fetch specific rows from a table you have to include a WHERE clause in your SQL statement.

For example:

$query  = "SELECT CUSTOMER_NAME, RAMSCODE  FROM customer WHERE customer_id = 2";

Match the WHERE xxxxx clause to any column in your table

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, that was fairly easy
if it solved your problem, don't forget to mark it as the solution. it helps leflings!
1

You need to specifiy yor criteria as a where clause in the SQL

$query  = "SELECT CUSTOMER_NAME, RAMSCODE  FROM customer where RAMSCODE = %1";
$result = mysql_query($query,mysql_real_escape_string($yourcode)) or die (mysql_error());

Also you really need to Read the Manuals!

4 Comments

Would someone care to explain why this (and @leflings) answers were down voted. Using a where clause in the select statement is as correct an answer as possible given the lack of detail in the question. I accept that the Read The Manuals comment may have come accross as a little flippant but its good advice.
Your answer seems good. Maybe someone thought that the string sanitization was not pertinent to the question. But I will vote you back up to zero.
@Buttle -- I know the string sensitization and placeholders make the answer more verbose -- but I am loath to post an answer that is open to the dreaded "sql injection" or "cross site scripting" attacks.
yes, I would probably include that stuff as well. I can only guess why someone would have voted it down, and that's the only thing I can imagine.
0

As far as i've get what you want is to display only that row from customers table, which customer is logged in. you can use some thing like this:

while($row = mysql_fetch_array($result))
{
    if($row['CUSTOMER_NAME'] == " //Customer Logged In (customer name from session)"){
         echo "{$row['CUSTOMER_NAME']} <br>" .
         "RAMSCODE: {$row['RAMSCODE']} <br>" ;
    }else{
         //do nothing or continue with printing all
    }
}

Hope this helps.

3 Comments

Filtering in the application does not scale!
"does not scale" is too vague a comment for a beginner. like, for instance, the poster, and me.
@toon81 The point I think is that you could have gotten the correct information from the database in the first place, making this while loop unnecessary. Why loop through a million rows in PHP when you could have just retrieved 1 row with the right MySQL query. I think that's what James was referring to.

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.