Trecere de la mysql la mysqli
Scris: Mar Iul 01, 2014
Salut,
Acum ceva timp am gasit pe net un script de genul wall asemanator cu facebook,
Problema e ca acel script e in mysql si eu vreau sa il fac sa mearga cu mysqli
Am facut la el dar cand il inlocuiesc cu mysqli nu imi functioneaza.
Am sa pun aici partea de update cand un utilizator posteaza ceva pe perete.
Acesta este fisierul care contine toate functiile(adaugare comentariu, adaugare mesaj, afisare mesaje, like mesaje etc):
Iar aici apelez functia Save_Update:
E ceva gresit in codul asta?Nu imi adauga nici un mesaj.
Multumesc
Acum ceva timp am gasit pe net un script de genul wall asemanator cu facebook,
Problema e ca acel script e in mysql si eu vreau sa il fac sa mearga cu mysqli
Am facut la el dar cand il inlocuiesc cu mysqli nu imi functioneaza.
Am sa pun aici partea de update cand un utilizator posteaza ceva pe perete.
Acesta este fisierul care contine toate functiile(adaugare comentariu, adaugare mesaj, afisare mesaje, like mesaje etc):
Cod: Selectaţi tot
<?php
class FbWall {
public $postcount = 10;
public function Updates($user_ids,$lastid)
{
if($lastid==0)
{
$loadmore='';
}
else
{
$loadmore=" and M.msg_id<$lastid ";
}
$query = mysqli_query($db_conx,"(SELECT M.msg_id, M.uid_fk, M.message, M.created, U.username,M.uploads,M.profile_uid FROM messages M, users U WHERE M.uid_fk=U.id and M.uid_fk IN ($user_ids) $loadmore) UNION (SELECT M.msg_id, M.uid_fk, M.message, M.created, U.username,M.uploads,M.profile_uid FROM messages M, users U WHERE M.uid_fk=U.id and M.profile_uid IN ($user_ids) $loadmore) order by msg_id desc limit " .$this->postcount) or die(mysqli_error());
while($row=mysqli_fetch_array($query))
$data[]=$row;
return $data;
}
public function Total_Updates($user_ids)
{
$query = mysqli_query($db_conx,"(SELECT M.msg_id, M.uid_fk, M.message, M.created, U.username,M.uploads,M.profile_uid FROM messages M, users U WHERE M.uid_fk=U.id and M.uid_fk IN ($user_ids) $loadmore) UNION (SELECT M.msg_id, M.uid_fk, M.message, M.created, U.username,M.uploads,M.profile_uid FROM messages M, users U WHERE M.uid_fk=U.id and M.profile_uid IN ($user_ids) $loadmore) order by msg_id desc") or die(mysqli_error());
$data=mysqli_num_rows($query);
return $data;
}
public function Comments($msg_id,$second_count)
{
$query='';
if($second_count)
$query="limit $second_count,2";
$query = mysqli_query($db_conx,"SELECT C.com_id, C.uid_fk, C.comment, C.created, U.username FROM comments C, users U WHERE C.uid_fk=U.id and C.msg_id_fk='$msg_id' order by C.com_id asc $query") or die(mysqli_error());
while($row=mysqli_fetch_array($query))
$data[]=$row;
if(!empty($data))
{
return $data;
}
}
public function Gravatar($uid)
{
$query = mysqli_query($db_conx,"SELECT email FROM `users` WHERE id='$uid'") or die(mysqli_error());
$row=mysqli_fetch_array($query);
if(!empty($row))
{
$email=$row['email'];
$lowercase = strtolower($email);
$imagecode = md5( $lowercase );
$data="http://www.gravatar.com/avatar.php?gravatar_id=$imagecode";
return $data;
}
else
{
$data="default.jpg";
return $data;
}
}
public function Save_Update($uid, $update,$uploads,$profile_uid)
{
$update=mysqli_real_escape_string($db_conx, $update);
$time=time();
$ip=$_SERVER['REMOTE_ADDR'];
$query = mysqli_query($db_conx,"SELECT msg_id,message FROM `messages` WHERE uid_fk='$uid' order by msg_id desc limit 1") or die(mysqli_error());
$result = mysqli_fetch_array($query);
if ($update!=$result['message']) {
$uploads_array=explode(',',$uploads);
$uploads=implode(',',array_unique($uploads_array)) ;
$query = mysqli_query($db_conx,"INSERT INTO `messages` (message, uid_fk, ip,created,uploads,profile_uid) VALUES (N'$update', '$uid', '$ip','$time','$uploads','$profile_uid')") or die(mysqli_error());
$newquery = mysqli_query($db_conx,"SELECT M.msg_id, M.uid_fk, M.message, M.created, U.username FROM messages M, users U where M.uid_fk=U.id and M.uid_fk='$uid' order by M.msg_id desc limit 1 ");
$result = mysqli_fetch_array($newquery);
return $result;
}
else
{
return false;
}
}
public function Delete_Update($uid, $msg_id)
{
$query = mysqli_query($db_conx,"DELETE FROM `comments` WHERE msg_id_fk = '$msg_id' and uid_fk='$uid' ") or die(mysqli_error());
$query = mysqli_query($db_conx,"DELETE FROM `messages` WHERE msg_id = '$msg_id' and uid_fk='$uid'") or die(mysqli_error());
return true;
}
public function Image_Upload($uid, $image)
{
//Base64 encoding
$path="uploads/";
$img_src = $path.$image;
$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
$img_base = base64_encode($imgbinary);
$ids = 0;
$query = mysqli_query($db_conx,"insert into user_uploads (image_path,uid_fk)values('$image' ,'$uid')") or die(mysqli_error());
$ids = mysqli_insert_id($db_conx);
return $ids;
}
public function Get_Upload_Image($uid,$image)
{
if($image)
{
$query = mysqli_query($db_conx,"select id,image_path from user_uploads where image_path='$image'") or die(mysqli_error());
}
else
{
$query = mysqli_query($db_conx,"select id,image_path from user_uploads where uid_fk='$uid' order by id desc ") or die(mysqli_error());
}
$result = mysqli_fetch_array($query);
return $result;
}
public function Get_Upload_Image_Id($id)
{
$query = mysqli_query($db_conx,"select image_path from user_uploads where id='$id'") or die(mysqli_error());
$result = mysqli_fetch_array($query);
return $result;
}
public function Insert_Comment($uid,$msg_id,$comment)
{
$comment=mysqli_real_escape_string($db_conx, $comment);
$time=time();
$ip=$_SERVER['REMOTE_ADDR'];
$query = mysqli_query($db_conx,"SELECT com_id,comment FROM `comments` WHERE uid_fk='$uid' and msg_id_fk='$msg_id' order by com_id desc limit 1 ") or die(mysqli_error());
$result = mysqli_fetch_array($query);
if ($comment!=$result['comment']) {
$query = mysqli_query($db_conx,"INSERT INTO `comments` (comment, uid_fk,msg_id_fk,ip,created) VALUES (N'$comment', '$uid','$msg_id', '$ip','$time')") or die(mysqli_error());
$newquery = mysqli_query($db_conx,"SELECT C.com_id, C.uid_fk, C.comment, C.msg_id_fk, C.created, U.username FROM comments C, users U where C.uid_fk=U.id and C.uid_fk='$uid' and C.msg_id_fk='$msg_id' order by C.com_id desc limit 1 ");
$result = mysqli_fetch_array($newquery);
return $result;
}
else
{
return false;
}
}
public function Delete_Comment($uid, $com_id)
{
$query = mysqli_query($db_conx,"DELETE FROM `comments` WHERE uid_fk='$uid' and com_id='$com_id'") or die(mysqli_error());
return true;
}
}
?>
Cod: Selectaţi tot
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><?php
error_reporting(0);
include_once 'includes/db.php';
include_once 'includes/FbWall.php';
include_once 'includes/tolink.php';
include_once 'includes/textlink.php';
include_once 'includes/htmlcode.php';
include_once 'includes/time_stamp.php';
include_once 'includes/Expand_URL.php';
include_once 'session.php';
$FbWall = new FbWall();
if(isset($_POST['update']))
{
$update=$_POST['update'];
$update=str_replace("\n",'<br/>',$update);
$update=mysqli_real_escape_string($db_conx, "$update");
$uploads=$_POST['uploads'];
$profile_uid=$_POST['profile_uid'];
$data=$FbWall->Save_Update($uid,$update,$uploads,$profile_uid) ;
if($data)
{
$msg_id=$data['msg_id'];
$orimessage=$data['message'];
$message=tolink($data['message']);
$time=$data['created'];
$userid=$data['uid_fk'];
$profileid=$data['profile_uid'];
$username=$data['fullname'];
$face=$FbWall->Gravatar($userid);
if(textlink($orimessage))
{
$link =textlink($orimessage);
?>
<?php } ?>
<div class="stbody" id="stbody<?php echo $msg_id;?>">
<div class="stimg">
<img src="<?php echo $face;?>" class='big_face' alt='<?php echo $username; ?>'/>
</div>
<div class="sttext">
<?php
if($userid==$uid)
{ ?>
<a class="stdelete" href="#" id="<?php echo $msg_id;?>" title='Delete Update'></a>
<?php } ?>
<b><a href='profile.php?id=$userid'><?php echo $username;?></a></b> <?php echo clear($message);?>
<?php
if($uploads)
{
echo "<div style='margin-top:10px'>";
$uploads_array=explode(',',$uploads);
$uploads=implode(',',array_unique($uploads_array)) ;
$s = explode(",", $uploads);
foreach($s as $a)
{
$newdata=$FbWall->Get_Upload_Image_Id($a);
if($newdata)
echo "<img src='uploads/".$newdata['image_path']."' class='imgpreview'/>";
}
echo "</div>";
}
?>
<div class="sttime"><a href='#' class='' id='<?php echo $msg_id;?>' title='Like'>Like </a> | <a href='#' class='commentopen' id='<?php echo $msg_id;?>' title='Comment'>Comment </a> | <?php time_stamp($time);?></div>
<div id="stexpandbox">
<div id="stexpand<?php echo $msg_id;?>">
<?php
if(textlink($orimessage))
{
$link =textlink($orimessage);
echo Expand_URL($link);
}
?>
</div>
</div>
<div class="commentcontainer" id="commentload<?php echo $msg_id;?>">
<?php// include('load_comments.php') ?>
</div>
<div class="commentupdate" style='display:none' id='commentbox<?php echo $msg_id;?>'>
<div class="stcommentimg">
<img src="<?php echo $face;?>" class='small_face'/>
</div>
<div class="stcommenttext" >
<form method="post" action="">
<textarea name="comment" class="comment" maxlength="200" id="ctextarea<?php echo $msg_id;?>"></textarea>
<br />
<input type="submit" value=" Comment " id="<?php echo $msg_id;?>" class="comment_button"/>
</form>
</div>
</div>
</div>
</div>
<?php
}
}
?>
Multumesc