Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi every one
I want know how I can select item from database when I have composite primary key

I have this table
sID      cID      item1
 1       cs9        rr

when sID &cID are composite primary key
and item1 is what I want to select

and also I want use this code to select every items if there are items

PHP
<?php
$db_name="grade_book";
        $tbl_name="students_course";
        mysql_connect("localhost", "root", "");
        mysql_select_db("$db_name");
$sID=$_SESSION['sID'];
        $sql=mysql_query("SELECT * FROM course WHERE cID in(SELECT cID FROM $tbl_name WHERE sID=$sID)") or die(mysql_error());
        while($mysqlf =mysql_fetch_array($sql))
        {
            $chrs=$mysqlf['chrs'];
            //echo"$sid<br>";
            echo "$chrs<br>";

        }
?>


>>>> I know this select is wrong
Posted
Updated 29-May-11 17:39pm
v3
Comments
TorstenH. 30-May-11 2:49am    
..so you have different items with different "sID" and "cID" and want to select them?
or are they listed behind "Item1" as "Item2", "Item3" and so on?
malemar 30-May-11 9:10am    
no, I do not want to select "sID" & "cID"

eg. from this table
sID cID item1
1 cs9 rr
1 cs2 ee
2 cs3 ff

I want to select "items" as a list
eg.
rr
ee
ff

and
in the php file I have just "sID" as session

how I can select the list "items"?!!
malemar 30-May-11 13:17pm    
do I need to explain more??

1 solution

If example table has data as you wrote, you need to ask your database like this:
SQL
SELECT item1 FROM course WHERE sID = $sID

Return: single record.

SQL
SELECT DISTINCT item1 FROM course

Return: all (not duplicated) records listed in item1 column

If item1 is located in the other table, you should explain your database structure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900