Jump to content

SQL Query

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
Hey,

I'm running a query on a table, here is the code


<?php


	$sql = "SELECT * FROM links WHERE linkfrom = '".$userid."'";

	//Select everything from the users field where the Username is the same as the Cookie Variable

	$query = mysql_query($sql) or die(mysql_error());

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

	

	$linkfrom = $row['linkfrom'];


	$sql2 = "SELECT * FROM updates WHERE user = '".$linkfrom."'";

	}


	//Select everything from the users field where the Username is the same as the Cookie Variable

	$query = mysql_query($sql2) or die(mysql_error());

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

	

	if($row['gender'] == "Male"){


	echo $row['fullname']." ".$row['type']." ".$row['network']." on his profile";

	

	}else{

	

	echo $row['fullname']." ".$row['type']." ".$row['network']." on her profile";

	}


}

	

?>

Within the table LINKs I have the fields

linkto linkfrom fullnameto fullnamefrom

When I change linkfrom to linkto which is what I want it to be, it returns nothing, although when I have it set to linkfrom it returns something.

Basically what I'm trying to do is pull records from the UPDATE table where the field USER is the same as LINKTO when the field LINKFROM is the same as the logged in user (userid)

Any ideas why it's not working?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Okay never did anything in php before:o, But just by looking at it. Won't it only execute the sql2 query from the final row from the sql1 result? As you always seem to overwrite sql2 every loop. Then perhaps the last row, when using linkfrom happens to have a corresponding record in updates, but the final row using linkto does not.. (So i'm suggesting to close the '}' from the 1st while loop at the end of the 2nd while loop)

Orrr should i better read about php before trying to answer:rolleyes:? :crying:

Also, if php handles these bigger queries, i'm wondering if this works:
"SELECT * FROM updates WHERE user = (SELECT linkto
                                     FROM   links
                                     WHERE  linkfrom = '".$userid."')"
or maybe "IN (SELECT...." if the 2nd select returns multiple rows

#3
Guest_johnny.dacu_*

Guest_johnny.dacu_*
  • Guests
Well... i'll give you a posible solution with a question: Did you check your table. Maybe you dont have the value? I know is silly but sometimes if you don't pay enough attention..

#4
fredators88

fredators88

    Newbie

  • Members
  • Pip
  • 1 posts
Just try to call the fieldname of the table by index. e.g. $row[0]; //it returns to your first fieldname value

Edited by Roger, 30 July 2010 - 01:20 PM.
removed link


#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts

fredators88 said:

Just try to call the fieldname of the table by index. e.g. $row[0]; //it returns to your first fieldname value

That won't work as it's a mysql_fetch_assoc, you need to change to mysql_fetch_array to do that.

also, it's not so good to do, as it relies on the specific order, better to go by name.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
Roger

Roger

    If nothing goes right, go left.

  • Administrators
  • 718 posts
I agree with @oxano, you can probably do this in one query. Can you list your DB schema?
Check out our update Guidelines/FAQ. When posting code, remember to use code tags - Posted Image.