# Solved: PHP/MySQL: Variable not working



## Gibbs (Feb 28, 2005)

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:

```
$result = mysql_query("SELECT * FROM $table WHERE UserTo='$cookie' AND mail_id='$mail_id'") or die ("

Can't execute $sql: " .mysql_error());
$row = mysql_fetch_array($result);
if($row[UserTo]==$cookie) 
{
} 
else 
{
echo "

[B]This isn't your mail![/B]

" .$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!


----------



## brendandonhu (Jul 8, 2002)

Echo mysql_num_rows() to see if your query is returning any results.


----------



## Gibbs (Feb 28, 2005)

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()*


----------



## brendandonhu (Jul 8, 2002)

You have to write the parameter...it would be $result for you.


----------



## Gibbs (Feb 28, 2005)

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


----------



## brendandonhu (Jul 8, 2002)

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 (Feb 28, 2005)

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:


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


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


----------



## brendandonhu (Jul 8, 2002)

Ok, you can see that $mail_id is empty there. Where is it being set?


----------



## Gibbs (Feb 28, 2005)

It isn't....

The strange this is that the ID is stored to get to this part anyway. For example this works:

```
$result=mysql_query("select * from $table where UserTo='$cookie' ORDER BY SentDate DESC") or die ("ERROR: PLEASE REPORT THIS");
echo "[TABLE]";
while ($row=mysql_fetch_array($result)) {
echo "[TR][TD]

Mail:[/TD][TD][URL]
$row[Subject]
[/URL][/TD][TD] [URL]
[CENTER]Delete
[/URL]
[/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...


----------



## brendandonhu (Jul 8, 2002)

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 (Feb 28, 2005)

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


----------

