Tawanda
Tawanda

Reputation: 21

mySQL database: printing specific row or rows from a db table

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>" ;
} 

?>

Upvotes: 1

Views: 8598

Answers (3)

James Anderson
James Anderson

Reputation: 27478

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!

Upvotes: 1

talha2k
talha2k

Reputation: 1

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.

Upvotes: 0

leflings
leflings

Reputation: 646

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

Upvotes: 4

Related Questions