1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: PHP/MySQL: Variable not working

Discussion in 'Web Design & Development' started by Gibbs, Jan 16, 2007.

Thread Status:
Not open for further replies.
Advertisement
  1. Gibbs

    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!
     
  2. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    Echo mysql_num_rows() to see if your query is returning any results.
     
  3. Gibbs

    Gibbs Thread Starter

    Joined:
    Feb 28, 2005
    Messages:
    609
    That brings up:
    Warning: Wrong parameter count for mysql_num_rows()
     
  4. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    You have to write the parameter...it would be $result for you.
     
  5. Gibbs

    Gibbs Thread Starter

    Joined:
    Feb 28, 2005
    Messages:
    609
    I apologise i'm still new to PHP.

    0 is returned when using what you suggested.

    Thanks for your help so far
     
  6. brendandonhu

    brendandonhu

    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.
     
  7. Gibbs

    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
    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=''
     
  8. brendandonhu

    brendandonhu

    Joined:
    Jul 8, 2002
    Messages:
    14,681
    Ok, you can see that $mail_id is empty there. Where is it being set?
     
  9. Gibbs

    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...
     
  10. brendandonhu

    brendandonhu

    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.
     
  11. Gibbs

    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 :)
     
  12. Sponsor

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 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/535722

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice