not sure if it's db or php mis handling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

not sure if it's db or php mis handling

Post by m3rajk »

i'm not sure where i'm not handling the variable right..... it's suppossed to be a number that is equivalent to max number of ims they want to have when a page loads. 0 = off, then they can also choose 1,2,3,4,,5 or infinite. the array to select that is associative that plugs in the respective number (0 through 6 inclusive) and the default is 2, i use the number directly here:

Code: Select all

function bgnpg($title){ # begins all pages
  include("/home/joshua/includes/fyd.db.php"); # includes file
  $ims=array();
  if($_COOKIE['login']){ # we're logged in
    $db=mysql_connect($host, $login2, $pass2) or die("cannot access mysql"); # get the sql connection
    $fyd=mysql_select_db('findyourdesire', $db) or die("cannot connect to db"); # select the db
    $un=$_COOKIE['un']; $pw=$_COOKIE['pw']; # what we wont change on-the-fly
    $fprefs=mysql_query("SELECT uid, gmt_offset, tds, login_duration, msgs FROM users WHERE username='$un' AND password='$pw
'", $db); # get the prefs
    if(mysql_num_rows($fprefs)>0){ # we can update the cookies
      $prefs=mysql_fetch_array($fprefs); $gmto=$prefs['gmt_offset']; $utds=$tds[$prefs['tds']]; 
      $duration=$durr[$prefs['login_duration']]; $accepts=($prefs['msgs']*1); $uid=$prefs['uid'];
      $expire=(time()+($duration*60));
      setcookie(un, $un, $expire); # set username
      setcookie(pw, $pass, $expire); # set password
      setcookie(login, TRUE, $expire); # set login
      setcookie(gmto, $gmto, $expire); # set the gmt offset
      setcookie(utds, $rtds, $expire); # set the time display style
      $active=gmdate("Y-m-d H:i:s", time());
      $update=mysql_query("UPDATE users SET last_activity='$active' WHERE username='$un'", $db); # try to update users (we d
on't really care if it fails)
      if($accepts){ # person accepts ims
        if($accepts>5){ # the user wants them ALL
          $fims=mysql_query("SELECT msg_id FROM msgs WHERE to_id='$uid' AND viewed='0'", $db);
          $amtims=mysql_num_rows($fims);
          if($amtims){ # we have ims
            for($i=0;$i<$amtims;$i++){ # for each im
              $gimid=mysql_fetch_array($fims); $ims=$gimid['msg_id']; # record the msg_id
            }
          }
        }else{ # user wants $accepts amount
          $fims=mysql_query("SELECT msg_id FROM msgs WHERE to_id='$uid' AND viewed='0' ORDER BY msg_id ASC LIMIT '$accepts'"
, $db);
          $errno=mysql_errno($db);$error=mysql_error($db);echo"<p>errno: $errno<br />error:$error</p>";
          $amtims=mysql_num_rows($fims);
          if($amtims){ # we have ims
            for($i=0;$i<$amtims;$i++){ # for each im
              $gimid=mysql_fetch_array($fims); $ims=$gimid['msg_id']; # record the msg_id
            }
          }
        }
      }
    }else{ cookies('logout'); } # there was an error for some reason
  } # end cookie updating
  # set headers to stop caching
  header("Expires: Sat, 24 Feb 1979 20:00:00 GMT");
  header("cache-control: no-store,no-cache,must-revalidate");
  header("cache-control: post-check=0,pre-check=0", false);
  header("Pragma: no-cache");
  echo <<<END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>FindYourDesire.com -- $title</title>
    <meta name="Author" content="Pages coded by Josh Perlmutter for Desired Creations LLC">
    <meta name="Author" content="Graphics created by Alix Stolzer for Desired Creations LLC">
    <meta name="Author" content="Smileys created by Amber Beausoleil for Desired Creations LLC">
    <style type="text/css">
      <!-- this comment is for non-css compliant browsers
      {text-decoration:none}
      end of css -->
    </style>
  </head>
  <body bgcolor="#878787" text="#ffffff" alink="#950c0c" vlink="#3347c5" link="#000000">
    <center>

END;
  if(count($ims)){ # if there's any ims
    echo '      <script language="javascript">';
    foreach($ims as $im){ # foreach im to display
      echo "    window.open('http://24.91.157.113/findyourdesire/message.php?mid=$im', '$im', 'height=200,width=200,scrollba
rs=auto,resizable=yes');";
    }
    echo '      </script>';
  }
}
the error is on the line right before the debug line. the debug lined causes....
errno: 1064
error:You have an error in your SQL syntax near ''2'' at line 1
and the msgs table DOES exist, infact....

Code: Select all

mysql&gt; show tables;
+--------------------------+
| Tables_in_findyourdesire |
+--------------------------+
| bio                      |
| comments                 |
| forums                   |
| friends                  |
| interests                |
| mail                     |
| msgs                     |
| pests                    |
| posts                    |
| stats                    |
| threads                  |
| tops                     |
| users                    |
| votes                    |
+--------------------------+
14 rows in set (0.51 sec)
 
mysql&gt; describe msgs;
+---------+---------------------+------+-----+---------------------+----------------+
| Field   | Type                | Null | Key | Default             | Extra          |
+---------+---------------------+------+-----+---------------------+----------------+
| msg_id  | bigint(20) unsigned |      | PRI | NULL                | auto_increment |
| to_id   | int(10) unsigned    |      | MUL | 0                   |                |
| from_id | int(10) unsigned    |      |     | 0                   |                |
| from_un | varchar(15)         |      |     |                     |                |
| sent    | datetime            |      | MUL | 0000-00-00 00:00:00 |                |
| message | text                |      |     |                     |                |
| viewed  | tinyint(1)          |      |     | 0                   |                |
+---------+---------------------+------+-----+---------------------+----------------+
7 rows in set (0.19 sec)
 
mysql&gt;
right now there is nothing in the msgs table. i don't understand what's wrong here... i thought the lLIMIT$access part of the query would limit it to $access amount of msgs
Last edited by m3rajk on Mon Sep 01, 2003 10:56 am, edited 2 times in total.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

the syntax error line, where is that?
I'm not sure at all, but perhaps.

Code: Select all

$fims=mysql_query("SELECT msg_id FROM msgs WHERE to_id='$uid' AND viewed='0' ORDER BY msg_id ASC LIMIT '$accepts'" 
, $db);
'$accepts' without singelquotes? Will that work?
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

yes,

Code: Select all

$fims=mysql_query("SELECT msg_id FROM msgs WHERE to_id='$uid' AND viewed='0' ORDER BY msg_id ASC LIMIT '$accepts'" 
, $db);
is the line causing the error. i haven't triedit without the quotes, but as mcgruff has pointed out it's better to use them. on top of that, i've noticedit ONLY happens on the first pageload after logging in
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

changed the debug slightly...error reported:
errno: 1064
error:You have an error in your SQL syntax near ''2'' at line 1
query: SELECT msg_id FROM msgs WHERE to_id='1' AND viewed='0' ORDER BY msg_id ASC LIMIT '2'

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/joshua/includes/fyd.funcs.php on line 57
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

ok. i tried with without the '' around accepts and it works fine, but why is it only giving me one pageload before i'm "timed out"

i know it's less than a min for a page load., i should get more. i don't understand, it's liek it's causing the cookies to expire instead of update
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

m3rajk wrote:ok. i tried with without the '' around accepts and it works fine, but why is it only giving me one pageload before i'm "timed out"

i know it's less than a min for a page load., i should get more. i don't understand, it's liek it's causing the cookies to expire instead of update
About single quotes.. It's true that you should use it, except when it's within the LIMIT (reserve myself for others also, but LIMIT is the only one i can think of right now.)

About the cookies, try to add double quotes (singles might work also), as following:

Code: Select all

setcookie("un", $un, $expire); # set username
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

setting them isn't the issue. i know they get set. the issue is that the login allows only one pageload logged in, provided it's before the timeout, which means something is wrong and the logout call is being done in the page-start function.
the cookie and page starting functions:

Code: Select all

function cookies($action){ # inserts cookies
  if($action==='join-1'){ # make & set the confirmation code
    $conf=md5(uniqid(microtime(),1)); # make unique id
    $expire=time()+60*60; # set expiration an hour from now
    setcookie('confcode', $conf, $expire); #create cookie
  }elseif($action==='join-3'){
    $un=$_POST['un']; $pw=md5($_POST['pw']); # set the username and password into cookies, pw is masked
    $expire=time()+60*60; # set expiration an hour from now
    setcookie(un, $un, $expire); # username
    setcookie(pw, $pw, $expire); # pw
    setcookie(utds, 'm/d/Y H:i:s', $expire); # time display
    setcookie(gmto, '-5', $expire); # gmt offset
  }elseif($action==='logout'){ # make all cookies expire
    $expire=time()-(60*60*24*7); # set expire to 60 sec*60 min*24 hrs* 7 days ago (make sure a lagging computer will kill it even if my host is in new zealand)
    setcookie(un, NULL, $expire); # makes user name expire & sets it to NULL incase their computer errs
    setcookie(pw, NULL, $expire); # makes password expire & sets it to NULL incase their computer errs
    setcookie(login, NULL, $expire); # makes login expire & sets it to NULL incase their computer errs
    setcookie(utds, NULL, $expire); # makes the tds expire & sets it to NULL incase their computer errs
    setcookie(gmto, NULL, $expire); # makes the gmt offset expire & sets it to NULL incase their comp errs
  }
}

function bgnpg($title){ # begins all pages
  include("/home/joshua/includes/fyd.db.php"); # includes file
  $ims=array();
  if($_COOKIE['login']){ # we're logged in
    $db=mysql_connect($host, $login2, $pass2) or die("cannot access mysql"); # get the sql connection
    $fyd=mysql_select_db('findyourdesire', $db) or die("cannot connect to db"); # select the db
    $un=$_COOKIE['un']; $pw=$_COOKIE['pw']; # what we wont change on-the-fly
    $fprefs=mysql_query("SELECT gmt_offset, tds, login_duration, msgs FROM users WHERE username='$un' AND password='$pw'", $db); # get the prefs
    if(mysql_num_rows($fprefs)>0){ # we can update the cookies
      $prefs=mysql_fetch_array($fprefs); $gmto=$prefs['gmt_offset']; $utds=$tds[$prefs['tds']]; 
      $duration=$durr[$prefs['login_duration']]; $accepts=$prefs['msgs'];
      $expire=(time()+($duration*60));
      setcookie(un, $un, $expire); # set username
      setcookie(pw, $pass, $expire); # set password
      setcookie(login, TRUE, $expire); # set login
      setcookie(gmto, $gmto, $expire); # set the gmt offset
      setcookie(utds, $rtds, $expire); # set the time display style
      $active=gmdate("Y-m-d H:i:s", time());
      $update=mysql_query("UPDATE users SET last_activity='$active' WHERE username='$un'", $db); # try to update users (we don't really care if it fails)
      if($accepts){ # person accepts ims
	if($accepts>5){ # the user wants them ALL
	  $fims=mysql_query("SELECT msg_id FROM msgs WHERE to_id='$un' AND viewed='0'", $db);
	  $amtims=mysql_num_rows($fims);
	  if($amtims){ # we have ims
	    for($i=0;$i<$amtims;$i++){ # for each im
	      $gimid=mysql_fetch_array($fims); $ims=$gimid['msg_id']; # record the msg_id
	    }
	  }
	}else{ # user wants $accepts amount
	  $fims=mysql_query("SELECT msg_id FROM msgs WHERE to_id='$un' AND viewed='0' ORDER BY msg_id ASC LIMIT '$accepts'", $db);
	  $amtims=mysql_num_rows($fims);
	  if($amtims){ # we have ims
	    for($i=0;$i<$amtims;$i++){ # for each im
	      $gimid=mysql_fetch_array($fims); $ims=$gimid['msg_id']; # record the msg_id
	    }
	  }
	}
      }
    }else{ cookies('logout'); } # there was an error for some reason
  } # end cookie updating
  # set headers to stop caching
  header("Expires: Sat, 24 Feb 1979 20:00:00 GMT");
  header("cache-control: no-store,no-cache,must-revalidate");
  header("cache-control: post-check=0,pre-check=0", false);
  header("Pragma: no-cache");
  echo <<<END
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>FindYourDesire.com -- $title</title>
    <meta name="Author" content="Pages coded by Josh Perlmutter for Desired Creations LLC">
    <meta name="Author" content="Graphics created by Alix Stolzer for Desired Creations LLC">
    <meta name="Author" content="Smileys created by Amber Beausoleil for Desired Creations LLC">
    <style type="text/css">
      <!-- this comment is for non-css compliant browsers
      {text-decoration:none}
      end of css -->
    </style>
  </head>
  <body bgcolor="#878787" text="#ffffff" alink="#950c0c" vlink="#3347c5" link="#000000">
    <center>

END;
  if(count($ims)){ # if there's any ims
    echo '      <script language="javascript">';
    foreach($ims as $im){ # foreach im to display
      echo "	window.open('http://24.91.157.113/findyourdesire/message.php?mid=$im', '$im', 'height=200,width=200,scrollbars=auto,resizable=yes');";
    }
    echo '      </script>';
  }
}
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Hopefully someone else can see the problem. It should work for me.
Even if it works, I would add quotes in the setcookie's. You are making the parser work without quotes, so youre speeds might just get enhanced with them.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i tried commenting out the else, but it's still being set for only ONE pageload, not for the 2 minuted i expect. i put in echo lines to echo out the current system time and the expiration time, it showed the expiration was 120 seconds after the current time, so....


hmm... well, on the off chance it's a speed issue i'll do that, but i doubt it's goingto help






done, and no it didn't help.
Post Reply