Jump to content

Recommended Posts

Hello.

 

I'm trying to get row data from a cell in a table in a MySQL database based on information I have about one cell in the same row in the same table (but a different column). I have data about one cell in the table and want to retrieve the data from another cell in a different column of the same row by specifying the name of the column I want to get the data from (or by any alternative).

 

Can anyone please help me with this if they're not too confused?

 

Thank you

Peter

Yeah I tried that type of code:

 

(this is from a forum software)

$sql = "SELECT group_id
	FROM group_members
	WHERE user_id = ". $config['last_reg_user_id'] ."
	";
$result = $db->query($sql);
while($row = $db->fetch_assoc($result))
{
$newest_user_group[] = $row;
}

 

But it doesn't work. It returns an array.

$sql = "SELECT group_id
	FROM group_members
	WHERE user_id = ". $config['last_reg_user_id'] ."
	";
$result = $db->query($sql);
while($row = $db->fetch_assoc($result))
{
$newest_user_group = $row['group_id'];
}

The above code should assign $newest_user_group the value of group_id you selected from the group_members table. I'm presuming that's what you wanted to do.

Does this array value contain any data $config['last_reg_user_id']?

If not it will return no results.

Your query is certainly gramatically correct.

 

Try printing the $sql variable to the screen first to check

OK so your query is indeed returning values.

When your are returning the query data in the form of $row through a loop you are adding to the $newest_user_group array:

 

$newest_user_group[] = $row;

 

To see what the array contains use after the loop:

 

print_r($newest_user_group);

 

This will be the field values in the database records that you returned with your query

If there should be only 1 record in the database for the value you are adding into the query then use this code:

$result = $db->query("SELECT group_id FROM group_members WHERE user_id = ".$config['last_reg_user_id']." LIMIT 1");
$row = $db->fetch_assoc($result);
if(is_numeric($row['group_id'])) {
print "found group id: ".$row['group_id'];
}
else {
print "no record found";
}

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.