write to database(mysql)?

from the follwoing scripts, could you please show me how i can save to database instead of using the file.

****users*****

<?php

function saveUsers($onlineusers_file){

$file_save=fopen("onlineusers_".$_POST['room'].".txt","w+");

flock($file_save,LOCK_EX);

for($line=0;$line<count($onlineusers_file);$line++){

fputs($file_save,$onlineusers_file[$line]."\n");

};

flock($file_save,LOCK_UN);

fclose($file_save);

}

$onlineusers_file=file("onlineusers_".$_POST['room'].".txt",FILE_IGNORE_NEW_LINES);

if (isset($_POST['user'],$_POST['oper'])){

$user=$_POST['user'];

$oper=$_POST['oper'];

$userexist=in_array($user,$onlineusers_file);

if ($userexist)$userindex=array_search($user,$onlineusers_file);

if($oper=="signin" && $userexist==false){

$onlineusers_file[]=$user;

saveUsers($onlineusers_file);

echo "signin";

exit();

}

if($oper=="signin" && $userexist==true){

echo "userexist";

exit();

}

if($oper=="signout" && $userexist==true){

array_splice($onlineusers_file,$userindex,1);

saveUsers($onlineusers_file);

echo "signout";

exit();

}

if($oper=="signout" && $userexist==false){

echo "usernotfound";

exit();

}

}

$olu=join("<br>",$onlineusers_file);

echo $olu;

?>

*****send*******

<?php

$message=strip_tags($_POST['message']);

$message=stripslashes($message);

$user=$_POST['user'];

$touser=$_POST['touser'];

$room="room_".$_POST['room'].".txt";

$room_file=file($room,FILE_IGNORE_NEW_LINES);

$room_file[]=time()."<!@!>".$user."<!@!>".$touser."<!@!>".$message;

if (count($room_file)>20)$room_file=array_slice($room_file,1);

$file_save=fopen($room,"w+");

flock($file_save,LOCK_EX);

for($line=0;$line<count($room_file);$line++){

fputs($file_save,$room_file[$line]."\n");

};

flock($file_save,LOCK_UN);

fclose($file_save);

echo "sentok";

exit();

?>

*******receive*********

<?php

$lastreceived=$_POST['lastreceived'];

$user=$_POST['user'];

$room="room_".$_POST['room'].".txt";

$room_file=file($room,FILE_IGNORE_NEW_LINES);

$pattern = '/:(\w+):/';

$replacement = "<img src='./emotions/$1.png' />";

$hasMessage=false;

for($line=0;$line<count($room_file);$line++){

$messageArr=explode("<!@!>",$room_file[$line]);

if(count($messageArr)>1){

$touser=$messageArr[2];

if(in_array($user,explode(",",$messageArr[2])) || $user==$messageArr[1] || $touser==""){

if($messageArr[0]>$lastreceived){

echo $messageArr[1].": ".preg_replace($pattern,$replacement,$messageArr[3])."<br>";

$hasMessage=true;

}

}

}

}

echo ($hasMessage)?"<SRVTM>".$messageArr[0]:"";

?>

Update:

thanks for responding,

so far i know how to create tables and database.

or should i say i should i querry from that script to the tables.?

Update 2:

@Joshua, would you prefer i post to you the whole script, to your personal mail?

1 Answer

Relevance
Still have questions? Get your answers by asking now.