Ok i have a variable that increases everytime a user is added to the chat, but i would like to share that variable to another page to show how many people are in the chat
ps, i dont think sessions will work due to the fact that sessions are local data isnt it?
I tried to follow this link, and it works, but i want it to not be a lable, i want to make it a variable, how would i do that?
https://groups.google.com/forum/#!searchin/ajax-chat/count/ajax-chat/YlXEfLxPhBQ/RSDcjOKIGscJ
ps: http://gyazo.com/130ac519c54e0e318de330153d4a45d6 is the one that works
A session is a way to store information (in variables) to be used across multiple pages.Unlike a cookie, the information is not stored on the users computer.
If you’re looking to share variables between pages without using a PHP Session you could use a MySQL table, store the variable there and select it on the other page. However, sessions are really simple to implement compared to starting a database and storing/selecting data in/out of it.
so… based off the script I gave you, theres no way other than making it a session or sql? I guess ill do sql data cuz im used to it
Well, not that I know of. AJAX is a way of reloading data on your page without actually reloading the page, making it seemless, exactly what a chat application requires. However, if you want to post number of people on chat or message or anything like that you need to use SESSION variables, it is much simpler to implement than actually creating a database table and writing the code for inserting and selecting the values.
For sessions all you need really is:
session_start()
session_destroy()
One page will have $_SESSION['number_of_people"] = 0;
everytime a person logs in you update that variable $_SESSION['number_of_people"]++
On the other page you just do echo $_SESSION['number_of_people"];
Simple as that!
I don’t know about your chat application’s constraints but if you, for some reason, need those values stored over time you want to use MySQL. This solution is very simple as well but relatively harder than a PHP Session.
EDIT: Oh sorry, if you want EVERYBODY to be updated of how many people are on chat I would use MySQL for that.
-_- lol thanks for the help
You could also do:
[php]
require_once(“page_with_variable.php”);
[/php]
or
[php]
include_once(“page_with_variable.php”);
[/php]
ok im having an issue with the sql updating
[php]<?php
$mysql_host = “”;
$mysql_database = “”;
$mysql_user = “”;
$mysql_password = “”;
$db = new mysqli($mysql_host,$mysql_user,$mysql_password,$mysql_database);
if($db->connect_errno){
die(“you broke it”);
}
$result = $db->query("SELECT * FROM usersinchat") or die($db->error);
if($result->num_rows)
{
$rows = $result->fetch_assoc();
$testusers = $rows['inchat'] + 1;
$db->query("UPDATE usersinchat SET inchat = "+$testusers+"") or die($db->error);
print_r($testusers);
print_r($rows['inchat']);
}
?>
[/php] for some reason it saysYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3' at line 1. Is there a better way to update the inchat variable?
It seems like you have a problem with your UPDATE query syntax
[php]$db->query(“UPDATE usersinchat SET inchat = “+$testusers+””) or die($db->error);[/php]
Why did you add the + sign for?
Personally I just use PhpMyAdmin to do the syntax query tests and when I get it right I copy paste. When ever I need to insert a PHP variable I just close the quotes like so:
[php]“UPDATE userinchat SET inchat=”.$testusers[/php]
Also, I think that when you’re doing the SELECT query you get an object and not an array, check that as well by using debug:
[php]
echo ‘
’;’[/php]
var_dump($result);
echo ‘
i am getting an object. plus i tried using the . instead of + and it still had the same issue
Here’s an example from php.net
[php]<?php
$mysqli = new mysqli(“localhost”, “my_user”, “my_password”, “world”);
/* check connection */
if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}
$query = “SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5”;
if ($result = $mysqli->query($query)) {
/* fetch object array */
while ($obj = $result->fetch_object()) {
printf ("%s (%s)\n", $obj->Name, $obj->CountryCode);
}
/* free result set */
$result->close();
}
/* close connection */
$mysqli->close();
?>[/php]
WOW IM SUCH A NUB… I FORGOT TO CLOSE A }
[size=24pt]only thing is… is there a way to do something when the user closes the page via php or do i have to use javascript[/size]
No. If the die() function didn’t fire that means your syntax is correct.
-
Always echo the variables to see what result you’re getting, if everything works fine you can set the function to “production” mode by returning the variables. ECHO ANYWHERE AND EVERYWHERE, BEFORE AND AFTER
-
Why would you use two database connections?
-
You’re returning the “num_rows” instead of the actual field, that doesn’t make sense to me. I thought you had a table with a field that states how many people are online on chat, so why don’t you return that field?
[PHP]return $result->inchat;[/PHP] -
Jream has a pretty neat and simple chat app tutorial here.
-
If you want online help instead of this thread PM me and I will try to solve this issue with screen share.
EDIT:
6. die() is not a function you want to use in production mode, it might be good for development, however, if the site is in production mode and it doesn’t function properly, the users don’t need to see all those developer errors and the function will kill any code that comes after, they need to see something like “Sorry for the inconvenience” and if possible keep the app running without showing how many people are on chat.
- Actually, this tutorial is better imo.
sorry, it seems that there was a table already made that inputed the users into it that were in the chat, but now my only issue is with them leaving. if they leave, the database doesnt get updated unless they hit the logout button, so my question is is there any way to do a function onclose via php or do i have to figure out a way via javascript? (also lol i couldnt quote u cuz u had a link… had to remove the link from the quote -_-)
You have to use AJAX for that.
i am using ajax… this is an ajax chat -_-. what ever. thanks for the help, i have figured out all i needed