Jump to content

PHP/MySQL: Need help getting variable


Gibbs

Recommended Posts

I have a basic Private Message system working on my site. However i'm having problems with PHP getting the mail_id information from MySQL.

If I do this I can get the mail ID:
[code]$result=mysql_query("select * from $table where UserTo='$cookie' ORDER BY SentDate DESC") or die ("ERROR: PLEASE REPORT THIS");
while ($row=mysql_fetch_array($result))
{
echo $row[mail_id];
[/code]

However i'm trying to get the mail_id in a different part and am finding it difficult. When somebody selects a link this is used:
[code]$result =mysql_query("select * from $table where UserTo='$cookie' and mail_id='[mail_id]'") or die ("cant do it1");
//echo mysql_num_rows($result);
$row = mysql_fetch_array($result);
if($row[UserTo]==$cookie) [/code]

However the mail_id isn't recognised like previously. Is there a way to use the code at the top to get the mail_id to work here?

Any help appreciated. Thanks
Link to comment
https://forums.phpfreaks.com/topic/34561-phpmysql-need-help-getting-variable/
Share on other sites

if you are pulling the mail_id from the first query and using it in the second query then you have to assign it to a variable and use it that way such as
[code]
<?php
$result=mysql_query("select * from $table where UserTo='$cookie' ORDER BY SentDate DESC") or die ("ERROR: PLEASE REPORT THIS");
$row=mysql_fetch_array($result);
$mailId  = $row[mail_id];
$result =mysql_query("select * from $table where UserTo='$cookie' and mail_id='$mailId'") or die ("cant do it1");
//echo mysql_num_rows($result);
$row = mysql_fetch_array($result);
if($row[UserTo]==$cookie)
?>
[/code]
I'm still confused. I've tried this (i'll post the entire part as it may make more sense).

[code=php:0]$result=mysql_query("select * from $table where UserTo='$cookie' ORDER BY SentDate DESC") or die ("ERROR: PLEASE REPORT THIS");
echo "<table cellpadding=2 cellspacing=1 width=400 valign=top>";
$row = mysql_fetch_array($result);

$messageid = $row[mail_id]; // mail_id

while ($row=mysql_fetch_array($result))
{
echo "<tr><td width=30><p>Mail:</td><td><a href=pm.php?action=view&mail_id=$row[mail_id]>
$row[Subject]
</a></td><td width=50> <a href=pm.php?action=delete&id=$row[mail_id]>
<center>Delete
</a><br></td></tr>";
}
echo "</table>";
}


if($action==view)
{
$result = mysql_query("select * from $table where UserTo='$cookie' and mail_id='$messageid'") or die ("cant do it1");
$row = mysql_fetch_array($result);
if($row[UserTo]==$cookie)
{
}
else
{
echo "<p class=\"error\"><b>This isn't your mail!</b></p>" .$row;
exit;[/code]
so if they click on view they are sent away and back to this page by the
[code]
<?php
echo "<tr><td width=30><p>Mail:</td><td><a href=pm.php?action=view&mail_id=$row[mail_id]>
$row[Subject]
</a></td><td width=50> <a href=pm.php?action=delete&id=$row[mail_id]>
<center>Delete
</a><br></td></tr>";
?>
[/code]
then you can use the mail_id in the url for the second query
you would need

[code]
<?php
if($action==view)
{
if(isset($_GET['mail_id'])) { $mailId = $_GET['mail_id']; }
$result = mysql_query("select * from $table where UserTo='$cookie' and mail_id='$mailId'") or die ("cant do it1");
$row = mysql_fetch_array($result);
if($row[UserTo]!=$cookie)
{
echo "<p class=\"error\"><b>This isn't your mail!</b></p>" .$row;
exit;
}
?>
[/code]

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.