1

source code:

$display = 1004;
$result1 = mysql_query("SELECT id, username FROM users where parentid=$diplay");
$resrow = mysql_fetch_row($result1); 
$g = $resrow[0]; 
$g1 = $resrow[1]; 
$g2 = $resrow[3]; -error not display
$g3 = $resrow[4];
$g4 = $resrow[5];


echo "ID: $g";
echo "ID: $g2";
echo "ID: $g3";
echo "ID: $g4";

i have to show 1005, 1007, 1008 but i can retrieve $g value only:

ans: 1004 daniel

how i can show other values 1007,1008

Table

id           ----name  -----    parentid
-----------------------------------
1004            daniel         1003
1005            peter          1004
1007            michael        1004
1008            sam            1004

ans:

        $g
        1004
     /        \
    g2         g3   
   1005       1007
7
  • could you show the table structure Commented Nov 5, 2012 at 2:49
  • i think even i am sure that 0,1,4,4,5 are not the colon name ... you need to use the colon name in $resrow['colon name here'] Commented Nov 5, 2012 at 2:50
  • where are these values coming from? 1005, 1007, 1008? Commented Nov 5, 2012 at 2:50
  • id name parentid 1004 daniel 1003, 1005 peter 1004, 1007 michael 1004, 1008 sam 1004 Commented Nov 5, 2012 at 2:51
  • 1
    life saving ---Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Nov 5, 2012 at 2:53

3 Answers 3

2

From your mysql query, you can only get column id and username. You need to modify your sql to include more columns.

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

1 Comment

i need to show id number only, like 1005, 1007, 1008 based on parentid
2

You only select id and username in your query, which might have returned array like this

array(
    'id'=> 1005
    'username'=> 'name'
)

if 1005,1007,1008 are child of 1004. you might want to do

while($resrow = mysql_fetch_array( $result1 )){
    echo "ID:". $resrow['id'];
    echo "Name:". $resrow['username'];
}

4 Comments

Hi, i have to show individual value like $resrow[0], $resrow[1], $resrow[2], only first 3 values only i need to show. thanks,
@user1799052 noop he is right .. you need to use the loop if you want to get the multiple row as you wanted
then why do you want to echo ID? to show only the ID, just run the loop and echo only $resrow['id'].
just echo a separator to see if it is correct. echo $resrow['id'].':';. The result will be 1005,1007,1008,.
1

You are confusing rows with columns. On this database:

users
id    | name    |  parentid
------+---------+----------
1004  | daniel  |  1003
1005  | peter   |  1004
1007  | michael |  1004
1008  | sam     |  1004

The query SELECT id, name FROM users where parentid=1004 will return rows 1005, 1007, and 1008.

mysql_fetch_row() only fetches one row at a time.
That's across (the id and name), not down (several ids).

To get a list of all of the ID values that match, you need to loop through all the rows:

$parentid = 1004;
$result = mysql_query( "SELECT `id` FROM `users` WHERE `parentid`=$parentid" );
while( $row = mysql_fetch_row( $result ) ){ $IDs[] = $row[0]; }
// $IDs is now an array holding each id from the resultset.
// It will look something like:
/*
array(3) {
 [0]=>
    int(1005)
 [1]=>
    int(1007)
 [2]=>
    int(1008)
}
*/

As mentioned above, don't use ext/mysql for new work. I'm using it here only because that's how you asked your original question. You need to update your code and use ext/mysqli or PDO instead.

1 Comment

Hi, echo "$IDs[0]"; echo "$IDs[1]"; this is correct way to print the value?. thanks

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.