Jump to content

A problem with mysql query from php


firehawk777

Recommended Posts

Hi there

I am using a query to find what user made a comment. The problem I am having is I get the wrong user from the query. Everything seems fine in the database and I just can't seem to track the problem

		$query = "SELECT * FROM messages";
	$result=mysql_query($query) or die("Invalid Query : ".mysql_error());
		while ($row = mysql_fetch_assoc($result)){
			$pid=($row["pid"]);
			$id=($row["id"]);
			$query = "SELECT * FROM users WHERE id='$pid'";
			$rresult=mysql_query($query) or die("Invalid Query : ".mysql_error());
			while ($row = mysql_fetch_assoc($rresult)){
			$user=($row["user"]);
			}
                       }

 

The query is working in reverse I.E when I search for what messages are from the user. Can anyone see my problem. :confused:

Link to comment
Share on other sites

You're overwriting the variables $row & $user in the nested loops. Having a query inside a loop is very inefficient. You should be able to write a single query to do the work. I usually get the query correct after a lot of trial and error in phpmyadmin.

 

Something like

<?php
$q = "select users.user from users, messages where users.id = messages.pid";
?>

is a good starting point.

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.