FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help with Dreamweaver sessions !!!!!!!!





salman_500
Hey guyz,

im new to php and stuff so i dont use notepads to program stuff.. i use dreamweaver as it simplifies stuff... but there is one part that i discovered today that im not gettin...

well you see i use dreamweaver technology to register and login people.. and then i use the dreamweaver session technology to verify a person as logged in and grant acces to restricted pages of the site.... well just when i was building a statistics bar on my page, i found out that i didnt know how to display the total number of members online at the moment....

dreamweaver does not use database for sessions....... can anyone tell me how to show the number of members online at the moment...

als i wud like to know that how can i show the names of the members online....

plz some1 plz help me with this ... i will be very greatful !!!!!!!!!!!!!!!!!

may even pay !!!!!
hexkid
salman_500 wrote:
dreamweaver does not use database for sessions....... can anyone tell me how to show the number of members online at the moment...


Without a database it's really impractical to do that.
I have never used dreamweaver so I can't help you with integrating the database into your development tool.
Anyway ... you want to save last access time to the database and count users for which the last access time is less than [whatever you define as "at the moment"].
SlowWalkere
Sessions don't involve databases, they involve cookies. So your problem is not with Dreamweaver per se. What the sessions do is create a cookie that identifies the user. Then, a unique array is created, which can be accessed by any script that user accesses, called $_SESSION. In that array is stored the log-in info, so that your page knows, when it is loaded, that the user is logged in.

I'm not entirely sure how you implemented the log-in script through dreamweaver. If you post the source it would be easier to give detailed instructions. But in general, what you want to do is the following...

As hexkid said, you want to save the last access time to the database. Presumably, each user has a unique user id and each page they access has a script that checks if the user is logged in (based on that user id). Once you locate that section of the code, you want to add a bit of code that accesses your database, and updates the last time the user accessed it. One simple way to set up this database would be to have one row per member, containing a uid (int) and the time (bigint).

Then, when the user loads a page and he is logged in, do a query like this (where $now is set to time(), and loggedin is the name of the table)...
Code:
UPDATE loggedin SET time = $now WHERE uid = $uid LIMIT 1


Then, when you check to see how many users are logged in, just check how many records have a time that is less than 5/10/15 minutes from the current time (depending on how long you consider someone to be logged in). Voila, you now know how many users are "logged in."

- Walkere
salman_500
erm

so you mean that in order for this to work i will need to creat a table with a seperate row for every member.... like when a person registers to the site the row is automatically created and whenever the person logs in the script that logs him in also enter the timestamp into the field for that member.....

well i dont quite know how to do that...

well you wanted the scripts.... so here they are...

this one is for login...

Code:
<?php
// *** Validate request to login to this site.
session_start();

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($accesscheck)) {
  $GLOBALS['PrevUrl'] = $accesscheck;
  session_register('PrevUrl');
}

if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "rank";
  $MM_redirectLoginSuccess = "main.dwt.php";
  $MM_redirectLoginFailed = "main.dwt.php";
  $MM_redirecttoReferrer = true;
  mysql_select_db($database_solothemes, $solothemes);
     
  $LoginRS__query=sprintf("SELECT username, password, rank FROM users WHERE username='%s' AND password='%s'",
  get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));
   
  $LoginRS = mysql_query($LoginRS__query, $solothemes) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
   
    $loginStrGroup  = mysql_result($LoginRS,0,'rank');
   
    //declare two session variables and assign them
    $GLOBALS['MM_Username'] = $loginUsername;
    $GLOBALS['MM_UserGroup'] = $loginStrGroup;        

    //register the session variables
    session_register("MM_Username");
    session_register("MM_UserGroup");

    if (isset($_SESSION['PrevUrl']) && true) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];   
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>


and this is for registering.

Code:
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO users (name, username, password, email, country, newsletter) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['username'], "text"),
                       GetSQLValueString($_POST['password'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['country'], "text"),
                       GetSQLValueString($_POST['newsletter'], "int"));

  mysql_select_db($database_solothemes, $solothemes);
  $Result1 = mysql_query($insertSQL, $solothemes) or die(mysql_error());

  $insertGoTo = "main.dwt.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
?>



so like can you tell me what exactly to do??

what i get so far is that i have add a bit of code that may be similar to this...its just to give an idea if im write... and if i am you can give me the correct code... its not php but its word form of it Wink

Code:

<?php
$username = 'echo['$_SESSION']'
$time = time()
$sql_query = UPDATE loggedin VALUES $time WHERE user = '$username'

if (isset('$_SESSION'))
   run $sql_query;
?>


and then to count the number of ppl online according to timestamp..
this i dont have enough knowledge about. :p[/code]
hexkid
salman_500 wrote:
so like can you tell me what exactly to do??

what i get so far is that i have add a bit of code that may be similar to this...its just to give an idea if im write... and if i am you can give me the correct code... its not php but its word form of it Wink

Code:

<?php
$username = 'echo['$_SESSION']'
$time = time()
$sql_query = UPDATE loggedin VALUES $time WHERE user = '$username'

if (isset('$_SESSION'))
   run $sql_query;
?>

Your code is about right Smile
I took the liberty of transforming it to a more likely PHP code
Code:
<?php
if (isset($_SESSION['MM_Username'])) {
  $username = $_SESSION['MM_Username'];
  $sql_query = "UPDATE loggedin SET last_access=now() WHERE user='";
  $sql_query .= get_magic_quotes_gpc() ? $username : addslashes($username);
  $sql_query .= "'";
  if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error());
}
?>


salman_500 wrote:
and then to count the number of ppl online according to timestamp..
this i dont have enough knowledge about.


The select would be something like
Code:
$sql = "SELECT COUNT(*) FROM loggedin WHERE last_access>now()-300";
Panthrowzay
the licenses like fourhundred $'s it's easier to learn why HTML Deng two used dream weaver despite the , outragious $400.00 licensing fee. fireworks I'll admit that isn't good program for making graphic design. I don't know but he does begin to be as much is your program than HTML the only thing that's easy about HTML are at HTML tags. in any one could find a M. X. studio 2003 for under $400.00 I would really appreciate it.
salman_500
Panthrowzay wrote:
the licenses like fourhundred $'s it's easier to learn why HTML Deng two used dream weaver despite the , outragious $400.00 licensing fee. fireworks I'll admit that isn't good program for making graphic design. I don't know but he does begin to be as much is your program than HTML the only thing that's easy about HTML are at HTML tags. in any one could find a M. X. studio 2003 for under $400.00 I would really appreciate it.


Whats this????????????
salman_500
[quote="hexkid"]
Your code is about right Smile
I took the liberty of transforming it to a more likely PHP code
Code:
<?php
if (isset($_SESSION['MM_Username'])) {
  $username = $_SESSION['MM_Username'];
  $sql_query = "UPDATE loggedin SET last_access=now() WHERE user='";
  $sql_query .= get_magic_quotes_gpc() ? $username : addslashes($username);
  $sql_query .= "'";
  if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error());
}
?>


hey,

thnx for the code... but i tried to enter it...

i enter it after the part, in my login code,

Code:
  if ($loginFoundUser) {


like this
Code:
 $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
   
   $time = time();
    $loginStrGroup  = mysql_result($LoginRS,0,'user_level');
    $username = $loginUsername;
    $sql_query = "UPDATE users SET online='time()' WHERE username='$LoginUsername'";
    $sql_query .= get_magic_quotes_gpc() ? $username : addslashes($username);
    $sql_query .= "";
       if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error());

    $sql_pass_get = "SELECT * FROM users WHERE username='$loginUsername'";
   $user_info = mysql_fetch_array(mysql_query($sql_pass_get)); 
    //declare session variables and assign them
    $GLOBALS['MM_Username'] = $loginUsername;
    $GLOBALS['MM_UserGroup'] = $loginStrGroup;   


but when i try to log in... i get this error...

here "salman" is my username....

Code:
Error updating database: 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 'salman' at line 1


i dont know why im gettin this error... i tried to solve this by every means n my knowledge.... but no luck.....

plz help !!
hexkid
salman_500 wrote:
i dont know why im gettin this error... i tried to solve this by every means n my knowledge.... but no luck.....


Try this line
Code:

if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error() . ' The command was ' . $sql_query);


Then check the command for SQL errors and correct them Smile
salman_500
hexkid wrote:
salman_500 wrote:
i dont know why im gettin this error... i tried to solve this by every means n my knowledge.... but no luck.....


Try this line
Code:

if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error() . ' The command was ' . $sql_query);


Then check the command for SQL errors and correct them Smile


oh thnx for this part.... but considering my knowledge os SQl....... im headed nowhere.....

this is what i get now...
Code:
Error updating database: 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 'salman'' at line 1 The command was UPDATE users SET online='time()' WHERE username=''salman'


well once again ill tell you that this is the code im using and this is how im puitting it in my login code.... maybe im making a mistake here... but plz go thrtough it...

thnx

Code:
<?php
$login = "$loginFormAction";
// *** Validate request to login to this site.
session_start();

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($accesscheck)) {
  $GLOBALS['PrevUrl'] = $accesscheck;
  session_register('PrevUrl');
}

if (isset($_POST['username'])) {
  $loginUsername=$_POST['username'];
  $password=$_POST['password'];
  $MM_fldUserAuthorization = "user_level";
  $MM_redirectLoginSuccess = "login.php";
  $MM_redirectLoginFailed = "login.php";
  $error = "Invalid Username Or Password";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_solothemes, $solothemes);
     
  $LoginRS__query=sprintf("SELECT username, password, user_level FROM users WHERE username='%s' AND password='%s'",
  get_magic_quotes_gpc() ? $loginUsername : addslashes($loginUsername), get_magic_quotes_gpc() ? $password : addslashes($password));
   
  $LoginRS = mysql_query($LoginRS__query, $solothemes) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
   
   $time = time();
    $username = $loginUsername;
    $sql_query = "UPDATE users SET online='time()' WHERE username='$LoginUsername'";
    $sql_query .= get_magic_quotes_gpc() ? $username : addslashes($username);
    $sql_query .= "'";
       if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error() . ' The command was ' . $sql_query);

   $loginStrGroup  = mysql_result($LoginRS,0,'user_level');
    $sql_pass_get = "SELECT * FROM users WHERE username='$loginUsername'";
   $user_info = mysql_fetch_array(mysql_query($sql_pass_get)); 
    //declare session variables and assign them
    $GLOBALS['MM_Username'] = $loginUsername;
    $GLOBALS['MM_UserGroup'] = $loginStrGroup;   
   $GLOBALS['firstname'] = $user_info['first_name']; 
   $GLOBALS['lastname'] = $user_info['last_name'];
   $GLOBALS['email'] = $user_info['email_address'];
   $GLOBALS['avatar'] = $user_info['avatar']; 
   $GLOBALS['lastlogin'] = $user_info['last_login'];
   $GLOBALS['signup'] = $user_info['signup_date'];

    //register the session variables
    session_register("MM_Username");
    session_register("MM_UserGroup");
   session_register("firstname");
   session_register("lastname");
   session_register("email");
   session_register("avatar");
   session_register("lastlogin");
   session_register("signup");

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];   
    }
    header("Location: " . $MM_redirectLoginSuccess );


  }
  else {
    header("Location: " . $MM_redirectLoginFailed );
  }
}

?>


thnx... awaiting reply...
hexkid
salman_500 wrote:
Code:
  if ($loginFoundUser) {
   
   $time = time();
    $username = $loginUsername;
    $sql_query = "UPDATE users SET online='time()' WHERE username='$LoginUsername'";
    $sql_query .= get_magic_quotes_gpc() ? $username : addslashes($username);
    $sql_query .= "'";
       if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error() . ' The command was ' . $sql_query);


Your error is in this piece of code.
You're using $LoginUsername to build your $sql_query, but $LoginUsername isn't defined anywhere ($loginUsername or $username are defined)!
To check for use of uninitialized variables add these lines to the very top of your script (right after the first <?php)
Code:
error_reporting(E_ALL);
ini_set('display_errors', '1');


You're also adding an extra single quote to the end of the query.

And "SET online='time()'" will not do what you want it to Smile
salman_500
hey,


although i still made changes im still having trouble Crying or Very sad Embarassed Shocked Confused Sad Crying or Very sad

Code:
Error updating database: 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 '() WHERE username=salman' at line 1 The command was UPDATE users SET online=time() WHERE username=salman


i dont know y im getting this but i get this too..

Code:
Warning: Unknown(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0




plzzzzzz hrlp !! Crying or Very sad Crying or Very sad

and in desperation to get it rite... i removed some lines.. and this is what im usin atm..

Code:
   $time = time();
    $username = $_SESSION['MM_Username'];
    $sql_query = "UPDATE users SET online=time() WHERE username=salman";

    mysql_query($sql_query);
      if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error() . ' The command was ' . $sql_query);
salman_500
salman_500 wrote:
hey,


although i still made changes im still having trouble Crying or Very sad Embarassed Shocked Confused Sad Crying or Very sad

Code:
Error updating database: 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 '() WHERE username=salman' at line 1 The command was UPDATE users SET online=time() WHERE username=salman


i dont know y im getting this but i get this too..

Code:
Warning: Unknown(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0




plzzzzzz hrlp !! Crying or Very sad Crying or Very sad

and in desperation to get it rite... i removed some lines.. and this is what im usin atm.. in order to test if the script actually works... i simply gave it my username... but it still gives the error... Sad

Code:
   $time = time();
    $username = $_SESSION['MM_Username'];
    $sql_query = "UPDATE users SET online=time() WHERE username=salman";

    mysql_query($sql_query);
      if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error() . ' The command was ' . $sql_query);
hexkid
salman_500 wrote:
although i still made changes im still having trouble Crying or Very sad Embarassed Shocked Confused Sad Crying or Very sad

Code:
Error updating database: 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 '() WHERE username=salman' at line 1 The command was UPDATE users SET online=time() WHERE username=salman


i dont know y im getting this but i get this too..

There are two errors in your SQL:
a) time() is a function that requires a parameter. It returns the time of the time or datetime expression passed as the parameter.
b) username appears to be a varchar column; you need to enclose the salman constant in quotes.
See below for a (hopefully) working version Smile

salman_500 wrote:
Code:
Warning: Unknown(): Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0




plzzzzzz hrlp !! Crying or Very sad Crying or Very sad

I'm not really sure why this happens. It may be because you mix $_SESSION and session_register. Check the manual page for session_register().

salman_500 wrote:
and in desperation to get it rite... i removed some lines.. and this is what im usin atm..

My changes start with ###
Code:
   $time = time();
    $username = $_SESSION['MM_Username'];
    // $sql_query = "UPDATE users SET online=time() WHERE username=salman";
    ### changed time() to now()
    ### added quotes to salman
    $sql_query = "UPDATE users SET online=now() WHERE username='salman'";

    ### you don't need to execute the query twice
    // mysql_query($sql_query);
      if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error() . ' The command was ' . $sql_query);
salman_500
hey,

after all that time i finally did this just when i was about to give up.... and gues what... it worked !!!

there is the code im usin... as simple as can be...



Code:
  $time = time();
  $sql_query = "UPDATE users SET online=$time WHERE username='$loginUsername'";
      if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error());


it works !!!!


thnx !!!

ill send u frih$'s ! Very Happy


but now can u tell me how to display...
that query u posted above.. i dont know how to use it Embarassed Embarassed

thnx in advance...
hexkid
Thank you for the FRIH$

salman_500 wrote:
but now can u tell me how to display...
that query u posted above.. i dont know how to use it Embarassed Embarassed


I'd make a function to get the number of online users:
Code:
function online_users($minutes) {
  $sql = "SELECT COUNT(*) FROM users WHERE online>NOW()-" . $minutes*60;
  // you may reuse an existing connection to the database
  // instead of using this local (to the function) connection
  /**/$conn = mysql_connect() or die(mysql_error()); // default parameters!
  /**/if (!mysql_select_db('database')) die(mysql_error());
  $res = mysql_query($sql) or die(mysql_error());
  $online = mysql_result($res, 0);
  mysql_free_result($res);
  /**/mysql_close();
  return $online;
}


And then, wherever you want to display the current online users just use
Code:
<p class="info" title="in the last 5 minutes">Online users: <?php echo online_users(5); ?></p>
salman_500
hexkid wrote:
Thank you for the FRIH$

salman_500 wrote:
but now can u tell me how to display...
that query u posted above.. i dont know how to use it Embarassed Embarassed


I'd make a function to get the number of online users:
Code:
function online_users($minutes) {
  $sql = "SELECT COUNT(*) FROM users WHERE online>NOW()-" . $minutes*60;
  // you may reuse an existing connection to the database
  // instead of using this local (to the function) connection
  /**/$conn = mysql_connect() or die(mysql_error()); // default parameters!
  /**/if (!mysql_select_db('database')) die(mysql_error());
  $res = mysql_query($sql) or die(mysql_error());
  $online = mysql_result($res, 0);
  mysql_free_result($res);
  /**/mysql_close();
  return $online;
}


And then, wherever you want to display the current online users just use
Code:
<p class="info" title="in the last 5 minutes">Online users: <?php echo online_users(5); ?></p>


hey,


i put this script in my code..... and it seems to be working fine.... but i get no users online... i ts always "0" i log in and the latest time changes in the database..... but i dont get a user online....

plz help...
hexkid
salman_500 wrote:
... i get no users online... i ts always "0" i log in and the latest time changes in the database..... but i dont get a user online....


The php time() function returns something like 1158092613. If you try to update a column with that value the result will be meaningfull only for certain data types.
Check the value you just wrote into the database (my guess is that they will all be zero)
Code:
<?php
// connect and select a database
$sql = "select online from users order by online desc limit 10";
$res = mysql_query($sql);
while ($row = mysql_fetch_row($res)) {
  echo $row[0], "<br>\n";
}
mysql_free_result($res);
// close database connection
?>


You might want to try date() instead
Code:
...
  $time = date('Y-m-d H:i:s');
  $sql_query = "UPDATE users SET online='$time' WHERE username='$loginUsername'";
      if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error());
...


EDIT: or the MySQL FROM_UNIXTIME() function
Code:
...
  $time = time();
  $sql_query = "UPDATE users SET online=FROM_UNIXTIME($time) WHERE username='$loginUsername'";
      if (!mysql_query($sql_query)) exit('Error updating database: ' . mysql_error());
...
salman_500
well... the function for entering the time into database works fine...

i just looged in now..

and data base shows this value..

Code:
1158144857


login out now....

login in again now.... i get this value..

Code:
1158144896


so i guess thi part is working fine...is it?
hexkid
salman_500 wrote:
well... the function for entering the time into database works fine...


Ok, your `online` column accepts the value from PHP's time(). However when the SQL does WHERE online>NOW()-300 it will compare `online` with NOW()-300. `online` is 1158144896, NOW() is '2006-09-13 11:54:56' and NOW()-300 is 20060913115156!! Wink

The UNIXTIME 1158144896 will never be greater than the peculiar value generated by the subtraction.

Try this:
Code:
function online_users($minutes) {
  $sql = "SELECT COUNT(*) FROM users WHERE online>UNIX_TIMESTAMP(NOW())-" . $minutes*60;
  // ...
}
salman_500
although it still doesn work for me.... i still appreciate it..

thnx..... i think ill do somethin about it.... Sad

thnx for your help... ur gr8....


one other thing you can tell me.... how can i do so that when the data is retrived from the database its shows the first letter as capital ..

like if i use

echo $_SESSION['username'];

it should return :

Salman

instead of

salman..

how can i do that??

thnx..
mathiaus
Code:
echo ucwords($_SESSION['username']);

ucwords
hexkid
mathiaus wrote:
ucwords

Or even ucfirst Wink

echo ucwords('salman five hundred'); // prints "Salman Five Hundred"
echo ucfirst('salman five hundred'); // prints "Salman five hundred"

If you *always* want the first letter capitalized you may increase (just a tiny little bit) your site performance by saving the capital in the database in the first place.
salman_500
hey,

well i got all the stuff up there...

well now i wana know....

how can i do so that i take all data from the database and then show it on the page... i wana do it in normal php ... not in dreamweaver technology...

like there is one page... that displays some of the info... then there is another page... to which the first page links... the user will click on the username of someone on the first page... and then the second page will display further information about that user...

but it has to be in that case form... like i have a main page named as "account.php" ...

in this page i used the "switch" command to show diff pages in one...

so what i want is :

the first page be "members.php" to be shown in the account page as :

Code:
case 'memberslist':
include('includes/members.php');
break;


in this page there has to be list that shows the username, e-mail, website and user level only...

username should link to another page called as "memberdetail.php".
and it should be added as :

Code:
case 'member_info':
include('includes/memberdetail.php');
break;



so like it links to "account.php?pageID=member_info" ..

im having trouble with this as i tried to use the "$_GET['recordID']" command... but.. it doesnt show up....

so any way i can do this??

thnx in advance...
salman_500
ohh.. i forgot this.....

i get this lil error.... you see...

i made this PM script... basicly took the idea off a tutorial... but i get this erro kinda thing...

you see..

Code:
include('Connections/solothemes.php');

$loggedinuser = $_SESSION['username'];
$get = mysql_query("SELECT COUNT(*) FROM pmessages WHERE touser = '$loggedinuser' AND unread='unread'");
$numbermessages = $get;
if ($numbermessages > 0)
{
echo "You have $numbermessages new messages<br><br>";
echo "<a href=\"account.php?pageID=inbox\"> Click Here to view your messages</a>";
}
else
{
echo ("You have 0 new messages !");
}


well actually there are a couple of things that look like errors.....

the first is that when i dont have any messages in my inbox... or if i dont even have any messages at all.. the script still echo's this part

Code:
{
echo "You have $numbermessages new messages<br><br>";
echo "<a href=\"account.php?pageID=inbox\"> Click Here to view your messages</a>";
}


and the second thing is that...even if it echos this part... it does not show the number.... its shows thins on the page

"You have Resource id #15 new messages"

and i dont know where "Resource id #15" comes from.... what can be the reason for this...

thnx in advance yet again ! Very Happy
rfwrangler
Here is a way to do what your talking about in a tutorial form that is from a site that I have had a lot of luck with these guys are great.

Here is the link to the tutorial:

http://www.phpfreaks.com/tutorials/62/0.php

Good luck with your site and there are alot of great tutorials on that site as well that you may find helpful.
hexkid
Increase the error reporting level in your scripts.

Add
Code:
error_reporting(E_ALL);
to your scripts, right after the first <?php tag

Or, if you can change php.ini, do it there.
salman_500
hey,

that report error thing does not show the error.. i still get that "You have Resource id #15 new messages" thing... and no error report is given..... still dont know how to det it show the number of mesages.. Crying or Very sad


als can you plz tell me how to do that memberliss thing in my second last post???

thnx...
hexkid
salman_500 wrote:
that report error thing does not show the error.. i still get that "You have Resource id #15 new messages" thing... and no error report is given..... still dont know how to det it show the number of mesages..

mysql_query() never returns a value.
It always returns a collection of rows, each of which with as many values as defined in the SQL command. The collection of rows can have 0 (zero) or more elements, but it will always have exactly one element when the SQL command was "SELECT COUNT(*) FROM ...".
To get the rows you usually would use mysql_fetch_*() in a loop, but in your case it is absolutely certain you will get only one row; use mysql_result() instead.
Code:
$get = mysql_query("SELECT COUNT(*) FROM pmessages WHERE touser = '$loggedinuser' AND unread='unread'");
$numbermessages = mysql_result($get, 0);




salman_500 wrote:
als can you plz tell me how to do that memberliss thing in my second last post???

You have to include the memberID in the link printed from members.php
Code:
// get data
// loop () {
    // ...
    echo '<a href="account.php?member_id=', 17, '">member_name</a>';
    // ...
// }

and fetch it in account.php with
Code:
$id = (int)$_GET['member_id'];
if ($id <= 0) {
  exit('Invalid member id in URL.');
}
$sql = "select ... from ... where id=$id";
salman_500
hexkid wrote:
salman_500 wrote:
that report error thing does not show the error.. i still get that "You have Resource id #15 new messages" thing... and no error report is given..... still dont know how to det it show the number of mesages..

mysql_query() never returns a value.
It always returns a collection of rows, each of which with as many values as defined in the SQL command. The collection of rows can have 0 (zero) or more elements, but it will always have exactly one element when the SQL command was "SELECT COUNT(*) FROM ...".
To get the rows you usually would use mysql_fetch_*() in a loop, but in your case it is absolutely certain you will get only one row; use mysql_result() instead.
Code:
$get = mysql_query("SELECT COUNT(*) FROM pmessages WHERE touser = '$loggedinuser' AND unread='unread'");
$numbermessages = mysql_result($get, 0);



man your a php Genious !!! its fixed and works GREAT !!! Very Happy


Quote:

salman_500 wrote:
als can you plz tell me how to do that memberliss thing in my second last post???

You have to include the memberID in the link printed from members.php
Code:
// get data
// loop () {
    // ...
    echo '<a href="account.php?member_id=', 17, '">member_name</a>';
    // ...
// }

and fetch it in account.php with
Code:
$id = (int)$_GET['member_id'];
if ($id <= 0) {
  exit('Invalid member id in URL.');
}
$sql = "select ... from ... where id=$id";



well about this.... you see that i have both pages .. one showin lil info and link to the detailpage, where detail page has all info... so i waant the both pages as a case in account.php file....

like i have like this atm..

Code:
switch($_GET['pageID'])
{
default:
include('includes/default.php');
break;

case 'profile_edit':
include('includes/profile_edit.php');
break;

case 'avatar_edit':
include('includes/avatar_edit.php');
break;

case 'inbox':
include('messages/inbox.php');
break;

case 'view':
include('messages/view.php');
break;

case 'delete':
include('messages/delete.php');
break;

case 'write':
include('messages/write.php');
break;

case 'pass_change':
include('includes/pass_change.php');
break;

case 'memberslist':
include('includes/memberslist.php');
break;

case 'members_detail':
include('includes/members_detail.php');
break;
}


so the case representing "memberslist" is the student page.... something like on this site...

i made this using dreamweaver...but i want my own code in this project.. Cool

http://www.isbrealestate.com/realestate/index.php

see that whwn u click the image... your taken to a detail page...

i want the same on this project.... but there is a problem... you see in that i just link to "detail.php?recordID=1" for axample... but on this iste i want it to be in cas of a page... so i dont know how the link be will be... like maybe its this "account.php?pageID=members_detail&recordID=1"

i dont know how to put this.....


also im not really sure how to fetch data from the databse and show it up as a repeat...like on the other site... it auto include the rows.... i dont know how to do that myself.....

sorry... but im still a noob at php Crying or Very sad Sad
hexkid
In the memberlist script you want this
Code:
// ...
$sql = "select ... from ...";
$resource = mysql_query($sql) or die(mysql_error());
while ($member = mysql_fetch_array($resource)) {
  $URL = 'account.php?pageID=members_detail&recordID=' . $member[$member_id];
  echo $member[$member_id], ' <a href="', $URL, '">';
  echo member['member_name'], '</a><br>';
}
mysql_free_result($resource);
// ...


and in the members_detail you want this
Code:
$id = (int)$_GET['recordID'];
if ($id < 1) {
  exit('Wrong record ID.');
}
$sql = "select ... from ... where member_id=$id";
$resource = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($resource) == 1) {
  $member = mysql_fetch_array($resource);
  echo '<b>', $member['member_name'], ' details</b><br>';
  echo 'detail1: ', $member['detail1'], '<br>';
  echo 'detail2: ', $member['detail2'], '<br>';
} else {
  exit('Wrong record ID.');
}
salman_500
hexkid wrote:
In the memberlist script you want this
Code:
// ...
$sql = "select ... from ...";
$resource = mysql_query($sql) or die(mysql_error());
while ($member = mysql_fetch_array($resource)) {
  $URL = 'account.php?pageID=members_detail&recordID=' . $member[$member_id];
  echo $member[$member_id], ' <a href="', $URL, '">';
  echo member['member_name'], '</a><br>';
}
mysql_free_result($resource);
// ...


and in the members_detail you want this
Code:
$id = (int)$_GET['recordID'];
if ($id < 1) {
  exit('Wrong record ID.');
}
$sql = "select ... from ... where member_id=$id";
$resource = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($resource) == 1) {
  $member = mysql_fetch_array($resource);
  echo '<b>', $member['member_name'], ' details</b><br>';
  echo 'detail1: ', $member['detail1'], '<br>';
  echo 'detail2: ', $member['detail2'], '<br>';
} else {
  exit('Wrong record ID.');
}


koooool !!!! Cool Cool Cool

it works !!!!!!

Thnx !!!!!!! Very Happy Very Happy Very Happy Very Happy

man your great !!!!
salman_500
hey again,


can you tell me how i can include an advance search feature into the memberslist thingy.....

i will appreiciate it as ever...

thnx in advance !
hexkid
salman_500 wrote:
can you tell me how i can include an advance search feature into the memberslist thingy.....


Code:
function unmagic($x) {
  return (ini_get('magic_quotes_gpc')) ? (stripslashes($x)) : ($x);
}

// ...
$userInput = mysql_real_escape_string(unmagic($_POST['search']));
$sql = "select ... from ... where `name` like '%$userInput%'";
// ...
salman_500
hey,

thnx for the above code ! didnt use it yet... so i dont quite know if it will work.....

weel im stuck yet again !

you see i have made this PM system... but am having trouble doing this litle thing.... well i wana do so that once a person is reading the personal message, the infor about the sender appears in the column to the left... like on any phpbb forum.... but i dont quite seem to get it....

you i use this code to show my personal message to the users they are sent to :


Code:
<?php

include('Connections/solothemes.php');
$loggedinuser = $_SESSION['username'];
if (!$_GET[msgid])
{

echo ("Invalid message!");
}
else
{

$getmsg= mysql_query("SELECT * from pmessages where id = '$_GET[msgid]'");
$msg = mysql_fetch_array($getmsg);

if ($msg[touser] == $loggedinuser)
{


you see after checking for errors... i sent the details into an array and easily show them up using this little code...e.g....

Code:
$msg[title]


but when i try to use the same method to get the detail of the sender.... i cant.. i use this code

Code:
$getuser= mysql_query("SELECT * from user where username = '$msg[from]'");
$usermsg = mysql_fetch_array($getuser);


but on the page i get this error... and no data shows up...

Code:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\messages\view.php on line 31



plz help !

thnx in advance .... just like i always doo Razz Cool Surprised Very Happy
hexkid
salman_500 wrote:
on the page i get this error... and no data shows up...


Check that $msg really has what you expect (from is a reserved SQL keyword)
Code:
// ...
$getmsg= mysql_query("SELECT * from pmessages where id = '$_GET[msgid]'");
$msg = mysql_fetch_array($getmsg);

echo '<pre>'; print_r($msg); echo '</pre>';

// ...


I also like to separate the query from the mysql_query() call. This allows me to print the query separately and debug it
Code:
// ...
$sql = "SELECT * from pmessages where id = '$_GET[msgid]'";
$getmsg= mysql_query($sql);
if (!$getmsg) {
  echo 'error in query! The query was: <tt>', $sql, '</tt>';
  echo ' and the error was: <tt>', mysql_error(), '</tt>.';
  // for simplicity sake we just exit now
  exit();
}

$msg = mysql_fetch_array($getmsg);
salman_500
hexkid wrote:
salman_500 wrote:
on the page i get this error... and no data shows up...


Check that $msg really has what you expect (from is a reserved SQL keyword)
Code:
// ...
$getmsg= mysql_query("SELECT * from pmessages where id = '$_GET[msgid]'");
$msg = mysql_fetch_array($getmsg);

echo '<pre>'; print_r($msg); echo '</pre>';

// ...


I also like to separate the query from the mysql_query() call. This allows me to print the query separately and debug it
Code:
// ...
$sql = "SELECT * from pmessages where id = '$_GET[msgid]'";
$getmsg= mysql_query($sql);
if (!$getmsg) {
  echo 'error in query! The query was: <tt>', $sql, '</tt>';
  echo ' and the error was: <tt>', mysql_error(), '</tt>.';
  // for simplicity sake we just exit now
  exit();
}

$msg = mysql_fetch_array($getmsg);


well... you see the query you mentioned above... it works perfectly fine ! i even confirmed it buy using you lill code and i get no error at all....

but when i did it with e second query that gets the user info.... it returned that the query was empty...

i even tried removgin the "from" thingy and replaced it with the actual name of the username i used to send the pm.... i ven tried the query...to match firstname instead.... but still i get this :

Code:
error in query! The query was: and the error was: Query was empty.


and i dont quite get what the error mught be... coz the query doesnt seem to be working..... even though i used a similar query, as i mentioned above, for another perpose... it works perfectly... and all the info shows up on just requesting it... but this one returns empty....

and im clueless..... Crying or Very sad Crying or Very sad Shocked Confused
hexkid
salman_500 wrote:
and im clueless.....

Have you added the error_reporting(E_ALL); to your script?

Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

// ...

$sql = "SELECT * from pmessages where id = '$_GET[msgid]'";
$getmsg= mysql_query($sql);
if (!$getmsg) {
  echo 'error in query! The query was: <tt>', $sql, '</tt>';
  echo ' and the error was: <tt>', mysql_error(), '</tt>.';
  // for simplicity sake we just exit now
  exit();
}

$msg = mysql_fetch_array($getmsg);
// ...
?>
salman_500
hexkid wrote:
salman_500 wrote:
and im clueless.....

Have you added the error_reporting(E_ALL); to your script?

Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

// ...

$sql = "SELECT * from pmessages where id = '$_GET[msgid]'";
$getmsg= mysql_query($sql);
if (!$getmsg) {
  [code]echo 'error in query! The query was: <tt>', $sql, '</tt>';[/code]
  echo ' and the error was: <tt>', mysql_error(), '</tt>.';
  // for simplicity sake we just exit now
  exit();
}

$msg = mysql_fetch_array($getmsg);
// ...
?>



yes..... but i only get info that says the query was empty.....

and this part of the code you gave me :
Code:
echo 'error in query! The query was: <tt>', $sql, '</tt>';


it doesn't even echo the query......

maybe its me that is going wrong... well here is the code that i use for the page... maybe there is somthing causing the errors....

plz check...

thnx !

ill pay you for this one !

Code:


<?php

include('Connections/solothemes.php');
$loggedinuser = $_SESSION['username'];
if (!$_GET[msgid])
{

echo ("Invalid message!");
}
else
{

$getmsg= mysql_query("SELECT * from pmessages where id = '$_GET[msgid]'");
$msg = mysql_fetch_array($getmsg);


if ($msg[touser] == $loggedinuser)
{


if (!$_POST[message])
{
 

$markread = mysql_query("Update pmessages set unread = 'read' where id = '$_GET[msgid]'");

$msg[message] = nl2br(stripslashes("$msg[message]"));


$getuser= mysql_query("SELECT * from user where username = '$msg[from]'");
$usermsg = mysql_fetch_array($getuser);
 
echo "
  <table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#B39B9B\">
  <tr>
    <td width=\"20%\" height=\"25\" align=\"center\" valign=\"middle\" background=\"images\forms\image_10.gif\"><strong>User Info</strong></td>
    <td width=\"35%\" align=\"center\" valign=\"middle\" background=\"images\forms\image_10.gif\">Title : <b>".ucwords("".$msg[title]."")."</b></td>
    <td width=\"25%\" align=\"center\" valign=\"middle\" background=\"images\forms\image_10.gif\">Date : <b>".ucwords("" .$msg[date]."")."</b></td>
  </tr>
  <tr>
    <td align=\"left\" valign=\"top\"><br>&nbsp;Username : <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=\"account.php?sub_section=memberslist_detail&recordID=" . $usermsg[userid]."\">".ucwords("".$msg[from]."")."</a></b><br>
      <br>
       &nbsp;User Level : <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>". $usermsg[user_level]."</b><br><br>
     
       &nbsp;Avatar :<br>
      <blockquote><img src=\"images/avatars/". $usermsg[avatar]."\" border=\"3\"></blockquote></td>
    <td colspan=\"2\" align=\"left\" valign=\"top\"><br><blockquote><b>". ucfirst("".$msg[message]."") ."</b></blockquote></td>
    </tr>
</table>
<br><br>
--------------------------------------------------------------------------------------------------
<br>
<form method=\"POST\" style=\"margin: 0px;\">
<br><br><b>Reply</b><br><br>
<textarea rows=\"6\" name=\"message\" cols=\"45\" style=\"background-color:#3D6D67; border-width:1px; border-color:#B39B9B; border-style:solid\"></textarea>
<br>
<input type=\"submit\" value=\"Submit\" name=\"send\">
 
</form><br>
-------------------------------------------------------------------------------------------------
<br><br><div align=\"center\"><b><a href=\"account.php?sub_section=inbox\">Inbox</a> &middot; <a href=\"account.php?sub_section=write\">Write A New Message</a></b>";
}
if ($_POST[message])
{

$message = htmlspecialchars(addslashes("$_POST[message]"));
$do = mysql_query("INSERT INTO `pmessages` ( `title` , `message` , `touser` , `from` , `unread` , 
`date`, `reply`) VALUES
('$msg[title]', '$message', '$msg[from]', '$loggedinuser',
 'unread', NOW(), 'yes')");
echo "Your message has been sent.<br><br>-------------------------------------------------------------------------------------------------
<br><br><div align=\"center\"><b><a href=\"account.php?sub_section=inbox\">Inbox</a> &middot; <a href=\"account.php?sub_section=write\">Write A New Message</a></b>";
}
}
else
{
echo"<b>Error</b><br />";
echo "This message was not sent to you!";
}}
?>


thnx alot Pro !! Cool Very Happy
hexkid
salman_500 wrote:
plz check...


Try the changes I uploaded to frihost.pastebin.

The lines I replaced are prefixed with a single #; there are some comments prefixed with ###.
salman_500
hexkid wrote:
salman_500 wrote:
plz check...


Try the changes I uploaded to frihost.pastebin.

The lines I replaced are prefixed with a single #; there are some comments prefixed with ###.


well ..... i really appreciate you doing all this code editing for me... but i dont think your quite getting what the problem is.....

i will explain once more.... maybe it become clear this time...

ok .... you see, i have this PM system... it works perfectly fine with sending, reading, writing, viewing etc.... But i was going through it again, i planned to add a nother feature to it.... before i added this feature.. the pm table had a field "from" where the name of the sender was stored (By the way "touser" is "to user"), so when i added the new feature, i tried to use this "from" field to get the name of the sender. i then put it in an mysql query like this :

Code:
$getuser= mysql_query("SELECT * from user where username = '$msg[from]'");
$usermsg = mysql_fetch_array($getuser);


here the " $msg" comes from this similar code, i use to get the Pm details..

Code:
$getmsg= mysql_query("SELECT * from pmessages where id = '$_GET[msgid]'");
$msg = mysql_fetch_array($getmsg);


now when i use this query to get Pm details, they work perfectly fine and all of the info is pulled into arrays
Code:
$msg = mysql_fetch_array($getmsg);


when i was adding the new feature... i used the same code... and the same way... but the query returns empty... i treid all the error reporting techniques that you mentioned above... but i got no results.... always got and error saying the query wasn empty...

i even tried the
Code:
"echo '<pre>'; print_r($msg); echo '</pre>';"


thing you mentioned above..... it worked for the first part that gets data from pmessages (table) but it returns nothing for the other......

and i dont know what is the cause ....

Embarassed Crying or Very sad Confused Shocked Sad Question Crying or Very sad
sonam
I am creating one script (Butterfly) what count online visitors. You can visit my site and download zip.

Sonam
hexkid
salman_500 wrote:
and i dont know what is the cause ....

I don't understand why the query is empty ...
mysql_query("string here"); should NEVER say it's an empty query!

Maybe you have a mismatched quote or curly brace ????

EDIT: Removed my email address.
salman_500
er.....

well... i fixed the script.....

and the problems was a very simple one...

but im to shamed to tell it to you... Embarassed Embarassed Embarassed

thnx for all the help though... u though me many new things... im sendin u some frih$'s.. Wink
hexkid
salman_500 wrote:
i fixed the script.....

and the problems was a very simple one...

Glad I could help Smile

Thank you for the FRIH$
salman_500
hey,,

im back yet again... Razz Razz Razz Razz

well this time i got somthin different...

you see i got this code :

Code:
<?php

echo "
<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#4C1515\">
  <tr>
    <td height=\"36\" align=\"left\" valign=\"top\">";

$sql = "select * from updates order by 'date' desc";
$resource = mysql_query($sql) or die(mysql_error());
while ($update = mysql_fetch_array($resource)) {

echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
         <tr>
        <td><b><font color=\"\">".$update['date']."</font></b></td>
      </tr>
      <tr>
        <td>".$update['title']."</td>
      </td>
      <tr>
        <td>-----</td>
      </td>
  </tr></table>

";

}

mysql_free_result($resource);
echo "</tr>
    </table>";
?>


now what i want it to do is to show only 3 - 4 rows... like i want it to be that it only shows the latest ones... i did the latest part by ordering time desc.. but i dont know how to make it show only 3 at a time... i want it so, so that i can make the rest of it available as an archive...

and also... i use the " now() " command to enter data in the database.... is it possible that when it gets the time.. it shows it like this.. e.g .

Sun 1st October 10:53 am

Thnx ! Laughing Very Happy
hexkid
salman_500 wrote:
well this time i got somthin different...
Why not a different post then?

salman_500 wrote:
i want it to do is to show only 3 - 4 rows
SELECT ... LIMIT 3

salman_500 wrote:
is it possible that when it gets the time.. it shows it like this.. e.g .

Sun 1st October 10:53 am

SELECT UNIX_TIMESTAMP() ...
// ...
echo date('D jS F h:i a', $dbdate);
salman_500
hexkid wrote:
salman_500 wrote:
well this time i got somthin different...
Why not a different post then?

salman_500 wrote:
i want it to do is to show only 3 - 4 rows
SELECT ... LIMIT 3

salman_500 wrote:
is it possible that when it gets the time.. it shows it like this.. e.g .

Sun 1st October 10:53 am

SELECT UNIX_TIMESTAMP() ...
// ...
echo date('D jS F h:i a', $dbdate);


ohh... thnx !!

i guess ill start a new post for further queries... Very Happy Shocked Very Happy Very Happy Very Happy Very Happy

Thnx !! Cool
carllester29
Im using dreamweaver cs5.5 I don't know how to show if Recordset is empty pls I need for my thesis
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.