Jump to content

[SOLVED] Getting data from a cell in a MySQL database


ecopetition

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";
}

Archived

This topic is now archived and is 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.