Jump to content

Recommended Posts

Hi,

 

I have a query which I am trying to repeat. However when I place them next to each the second one creates an error. However when I seperate them with another query inbetween them they both work perfectly. Can anyone please advise why they wont work next to each other and need to be seperated? It seems like a simple error?

 

 

<div class="followbuttonimagearea">
<?php
$query = mysql_query("SELECT * FROM follow JOIN users WHERE user_id = " . ($row['id']) . " ORDER BY follow_user_id ASC LIMIT 10");
while($row = mysql_fetch_array($query)) {

?>

<div class="followimage">
<a href="/test/profileinserttest.php?ID=<?php echo $row['follow_user_id']; ?>"><img src="/test/images/<?php echo $row['logo']; ?>" alt="<?php echo $row['company']; ?>" /></a>
 </div>
<?php
}
?>
</div>
<?php
if (isset($_GET['ID']))
$ID = mysql_real_escape_string($_GET['ID']);
$sql = "SELECT * FROM users WHERE ID = '$ID'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res); // no need to loop since you are retrieving only one row
$num_rows = mysql_num_rows($res); // check to see if any results were found, just in case someone puts an ID in the url without clicking on your link
?>
<div class="followbuttonimagearea">
	 <?php
$query = mysql_query("SELECT * FROM follow JOIN users WHERE user_id = " . ($row['id']) . " ORDER BY follow_user_id ASC LIMIT 10");
while($row = mysql_fetch_array($query)) {	
?>

<div class="followimage">
<a href="/test/profileinserttest.php?ID=<?php echo $row['follow_user_id']; ?>"><img src="/test/images/<?php echo $row['logo']; ?>" alt="<?php echo $row['company']; ?>" /></a>
 </div>
<?php
}
?>
</div>

 

This crashes with an error:

 

<div class="followbuttonimagearea">
	 <?php
$query = mysql_query("SELECT * FROM follow JOIN users WHERE user_id = " . ($row['id']) . " ORDER BY follow_user_id ASC LIMIT 10");
while($row = mysql_fetch_array($query)) {

?>

<div class="followimage">
<a href="/test/profileinserttest.php?ID=<?php echo $row['follow_user_id']; ?>"><img src="/test/images/<?php echo $row['logo']; ?>" alt="<?php echo $row['company']; ?>" /></a>
 </div>
<?php
}
?>
</div>

<div class="followbuttonimagearea">
	 <?php
$query = mysql_query("SELECT * FROM follow JOIN users WHERE user_id = " . ($row['id']) . " ORDER BY follow_user_id ASC LIMIT 10");
while($row = mysql_fetch_array($query)) {	
?>

<div class="followimage">
<a href="/test/profileinserttest.php?ID=<?php echo $row['follow_user_id']; ?>"><img src="/test/images/<?php echo $row['logo']; ?>" alt="<?php echo $row['company']; ?>" /></a>
 </div>
<?php
}
?>
</div>

Edited by justlukeyou

It would be really, really helpful if you would tell us what the error says.

 

 

You are using $row['id'] in both queries. We have no idea where that value is coming from for the first query since you didn't show the code above it. However: Your WHILE loop for the first query is assigning the data retrieved from the database to an array named $row. The WHILE loop will terminate when mysql_fetch_array returns false. When it returns false the value of $row becomes false (because you assigned the result to it). This is pretty much the standard way to process a result set. HOWEVER, you are then using $row['id'] to build your second query. In the example causing the error, $row is NOT an array, it is the value false, so you will get an error making the assignment, and probably an error from mysql_query as well. In the example that does NOT cause the error, you did not use a loop to process the single-row result-set (again, this is standard practice), so $row is still an array when you build that second query.

 

For future reference, you should build your queries into a variable, then execute the variable. That way you can print the query and see why the server is complaining:

 

$sql = 'SELECT column1, column2 FROM table1 WHERE id = ' . $id;
$res = mysql_query($sql);
if (! $res) {
 print('Error: ' . mysql_error() . ' SQL: ' . $sql);
 // or something like that
} else {
 while ($row = mysql_fetch_assoc($res)) {
   // and so forth

 

 

I see Pikachu has beat me with a better solution. However, my post answers the question for anyone who might be curious.

There's no reason to run the same query twice in a row. mysql_data_seek.

 

Or create the multiple outputs at the same time when running through the query results the one time. Just assign the outputs to two different variables and output the variables where you need those contents. You should be doing this anyway, in my opinion, to separate the logic and the presentation. But, I'm also guessing that since you are using $row['id'] as a parameter in your query(ies) that the code above is being run within a loop of another query! Why aren't you using a JOIN to that unseen query?

 

I just noticed that those two outputs are exactly the same?! Not sure if that is what you really meant to do or if it is only an example. But, assuming you want the two outputs to be different for the same query, this is what I would do:

<?php

$query = "SELECT follow_user_id, logo, company
	  FROM follow
	  JOIN users
	  WHERE user_id = {$row['id']}
	  ORDER BY follow_user_id ASC
	  LIMIT 10"
$result = mysql_query($query);

$followImageHTML = '';
$followButtonHTML = '';
while($row = mysql_fetch_array($result))
{
   //Create two different sets of output using the same query
   $followImageHTML .= "<div class=\"followimage\">\n";;					  
   $followImageHTML .= "<a href=\"/test/profileinserttest.php?ID={$row['follow_user_id']}\">";
   $followImageHTML .= "<img src=\"/test/images/{$row['logo']}\" alt=\"{$row['company']}\" />";
   $followImageHTML .= "</a>\n";
   $followImageHTML .= "</div>\n";

   $followButtonHTML .= "<div class=\"followimage\">\n";
   $followButtonHTML .= "<a href=\"/test/profileinserttest.php?ID={$row['follow_user_id']}\">";
   $followButtonHTML .= "<img src=\"/test/images/{$row['logo']}\" alt=\"{$row['company']}\" />";
   $followButtonHTML .= "</a>\n";
   $followButtonHTML .= "</div>\n";
}
?>

<div class="followbuttonimagearea">
   <?php echo $followImageHTML; ?>
</div>

<div class="followbuttonimagearea">
   <?php echo $followButtonHTML; ?>
</div>

Hi,

 

This is what Im trying to do.

 

What do you mean by "Why aren't you using a JOIN to that unseen query?"

 

<div class="followbuttonimagelinks">
 <div class="followbuttonimagelinksleft">
Followers
 </div>
  <div class="followbuttonimagelinksright">
<a href="/test/profileinserttest.php?ID=<?php echo $row['follow_user_id']; ?>">View All >></a>
</div>
 </div>
  <div class="followbuttonimagearea">
<?php
$query = mysql_query("SELECT * FROM follow JOIN users WHERE user_id = " . ($row['id']) . " ORDER BY user_id ASC LIMIT 10");
while($row = mysql_fetch_array($query)) {     
  ?>	

<div class="followimage">
<a href="/test/profileinserttest.php?ID=<?php echo $row['follow_user_id']; ?>"><img src="/test/images/<?php echo $row['logo']; ?>" alt="<?php echo $row['company']; ?>" /></a>
		 	</div>

<?php
}

  ?>
</div>




<div class="followbuttonimagelinks">
 <div class="followbuttonimagelinksleft">
Following
 </div>
  <div class="followbuttonimagelinksright">
<a href="/test/profileinserttest.php?ID=<?php echo $row['follow_user_id']; ?>">View All >></a>
</div>
 </div>
  <div class="followbuttonimagearea">
											<?php
$query = mysql_query("SELECT * FROM follow JOIN users WHERE follow_user_id = " . ($row['id']) . " ORDER BY user_id ASC LIMIT 10");
while($row = mysql_fetch_array($query)) {     
  ?>	

<div class="followimage">
<a href="/test/profileinserttest.php?ID=<?php echo $row['user_id']; ?>"><img src="/test/images/<?php echo $row['logo']; ?>" alt="<?php echo $row['company']; ?>" /></a>
		 	</div>

<?php
}

  ?>
</div>

Hi,

 

I am finding something very odd with the join function. When I add the join function it echoes every row in the second table. However when I do not use the join function it correctly displays only one value.

 

I dont have any of the same cell names between the two tables so I cant see where the confusion is coming from. Any suggestions please?

 

  <div class="followbuttonimagearea">
<?php
$query = mysql_query("SELECT follow_user_id, logo FROM follow JOIN users WHERE user_id = " . ($row['id']) . " ORDER BY user_id ASC LIMIT 10");
while($row = mysql_fetch_array($query)) {     
  ?>	


<div class="followimage">
<a href="/test/profileinserttest.php?ID=<?php echo $row['follow_user_id']; ?>"><img src="/test/images/<?php echo $row['logo']; ?>" alt="<?php echo $row['company']; ?>" /></a>
		 	</div>
<?php
}
  ?>

 

 

 

 

  <div class="followbuttonimagearea">
	<?php
$query = mysql_query("SELECT follow_user_id FROM follow WHERE user_id = " . ($row['id']) . " ORDER BY user_id ASC LIMIT 10");
while($row = mysql_fetch_array($query)) {     
  ?>	


<div class="followimage">
<a href="/test/profileinserttest.php?ID=<?php echo $row['follow_user_id']; ?>"><img src="/test/images/<?php echo $row['logo']; ?>" alt="<?php echo $row['company']; ?>" /></a>
		 	</div>
<?php
}
  ?>

Hi,

 

Im not totally sure what you mean. I am looking to take follow_user_id from follow and logo from users.

 

The id (355) is in follows only once as user_id so Im confused how it would display all the rows in users.

 

When I echo ($row['id']) it does display 355. But the code displays at user_id in follow.

 

 

[code
$query = mysql_query("SELECT follow_user_id, logo FROM follow JOIN users WHERE user_id = " . ($row['id']) . " ORDER BY user_id ASC LIMIT 10");
while($row = mysql_fetch_array($query)) {
[/code]

 

 

Of course you don't, because you refuse to try to learn.

 

Gosh you can be an obnoxious little madam sometimes. Im sorry but I think you should make it your New Years resolution to be less obnoxious.

 

Like Ive said a number of times. I can now do things in minutes which used to take weeks. If I "refuse to learn" how can now do things in minutes which use to take weeks? That makes your point that I refuse to learn incorrect and some what rude.

 

I have tried echoing all the terms to see if they match and they do. Just because I cant complete a join which to me looks to be in the same format as someone used doesn't mean I "refuse to learn".

 

So can you plesae stop being so obnoxious.

Edited by justlukeyou

I told you what you are NOT doing that is causing the problem. Did you try to do it yet? That's what I mean by refusing to even try. You always just come back and ask for more more more.

 

You claim you can do things in minutes yet none of it works correctly.

I told you what you are NOT doing that is causing the problem. Did you try to do it yet? That's what I mean by refusing to even try. You always just come back and ask for more more more.

 

You claim you can do things in minutes yet none of it works correctly.

 

I've spent around 5 to 6 hours just trying to complete this join query in the last two days. I think that constitutes as trying.

 

How do I tell what to join on. The code looks like a typical join query. Because telling me what it is NOT doing doesn't help very much.

 

Its a bit liking telling someone how not to drive. We could be here all day.

 

 

The code I am using is also stuctured identical to how another member has suggest I use it where follow_user_id, logo, are together.

 

Do I need to say what columns I want to put from users?

 

$query = "SELECT follow_user_id, logo, company
                 FROM follow
                 JOIN users
                 WHERE user_id = {$row['id']}
                 ORDER BY follow_user_id ASC
                 LIMIT 10"

Edited by justlukeyou

"How do I tell what to join on". You figured out the problem so stop whining about not understanding my post. Now actually try to figure out the solution ON YOUR OWN FOR ONCE.

 

http://bit.ly/Z8xTjC

 

I'll just leave this here. 

 

BTW, the fact that it's been a year and you still don't know the basic syntax of a join, shows me either you're not trying or you do in fact have a learning disability you won't own up to. 

I have said this a number of times now. Saying that someone has a learning disability is just disgusting. This isn't a playground and there is no need to use language like that.

 

I would like to take that little rat of a dog in your photo and turn it brown by shoving it deep up your ass. You continue to be rude at me and I might just be rude back.

Edited by justlukeyou
Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.