Solved: PHP/MySQL: Variable not working

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Gibbs

Thread Starter
Joined
Feb 28, 2005
Messages
609
Hey,

I have a problem. I am trying to get a UserTo table from MySQL to ensure that the mail was meant for the reader logged in. To do this i'm using:
PHP:
$result = mysql_query("SELECT * FROM $table WHERE UserTo='$cookie' AND mail_id='$mail_id'") or die ("<br><br>Can't execute $sql: " .mysql_error());
$row = mysql_fetch_array($result);
if($row[UserTo]==$cookie) 
{
} 
else 
{
echo "<p class=\"error\"><b>This isn't your mail!</b></p>" .$row[UserTo];
exit;
}
As you can see near the end i've printed the $row[UserTo] variable to see what it outputs. It doesn't give me anything which is why it isn't equal to $cookie.

$cookie works and prints the logged in username. $table and the database connection works too so i'm finding it difficult to assess this problem.

If anybody could point out what i'm doing wrong or what's wrong with this code I would be very grateful.

Thanks!
 

Gibbs

Thread Starter
Joined
Feb 28, 2005
Messages
609
brendandonhu said:
Echo mysql_num_rows() to see if your query is returning any results.
That brings up:
Warning: Wrong parameter count for mysql_num_rows()
 

Gibbs

Thread Starter
Joined
Feb 28, 2005
Messages
609
brendandonhu said:
You have to write the parameter...it would be $result for you.
I apologise i'm still new to PHP.

0 is returned when using what you suggested.

Thanks for your help so far
 
Joined
Jul 8, 2002
Messages
14,681
Ok, your SQL query isn't returning any results. You can
echo "SELECT * FROM $table WHERE UserTo='$cookie' AND mail_id='$mail_id'";
to see what your query is, the error is probably in there.
 

Gibbs

Thread Starter
Joined
Feb 28, 2005
Messages
609
The original line didn't have the ' around $mail_id but that brings up the error
Can't execute : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
Heres what I tried with the edited one:

Code:
echo mysql_query("SELECT * FROM $table WHERE UserTo='$cookie' AND mail_id='$mail_id'");
Brings up: Resource id #17

Code:
echo "SELECT * FROM $table WHERE UserTo='$cookie' AND mail_id='$mail_id'";
Brings up: SELECT * FROM mail WHERE UserTo='MyUsername' AND mail_id=''
 

Gibbs

Thread Starter
Joined
Feb 28, 2005
Messages
609
It isn't....

The strange this is that the ID is stored to get to this part anyway. For example this works:
PHP:
$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=500 valign=top>";
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>";
I'm trying to follow this tutorial (http://www.obsidweb.net/v2/print.php?tutid=2&code=yes&gfx=)

If I can find an alternative way of getting the mail id I can probably get this to work...
 
Joined
Jul 8, 2002
Messages
14,681
Well if you put an empty variable in your query it's not going to give you any results. You have to set $mail_id if you want it to have a value.
 

Gibbs

Thread Starter
Joined
Feb 28, 2005
Messages
609
Yeah. I'm still confused how it was used earlier successfully though...

Ah well i've decided to remove the string and it works.

Thanks for your help :)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Top