Trecere de la mysql la mysqli

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
Avatar utilizator
evident
Mesaje:168

Trecere de la mysql la mysqli

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):

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;

}





}


?>
Iar aici apelez functia Save_Update:

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
}
}
?>
E ceva gresit in codul asta?Nu imi adauga nici un mesaj.
Multumesc

MarPlo Mesaje:4343
Salut
Din ce am observat, folosesti $db_conx in mysqli_query() in acele functii, dar acea variabiila cu conexiunea nu exista in functie. Cred ca ar trebui sa mai adaugi la acele functii un parametru pt ea, si sa o transmiti cand apelezi functia.

Cod: Selectaţi tot

public function Updates($user_ids, $lastid,  $db_conx) {
  // ...
} 
In rest, daca e altceva gresit, nu stiu; prea mult cod de verificat.
Poate te va ajuta eventuale notificari de eroare php. Acest cod la inceputul fisierului ca sa le transmita pe toate.

Cod: Selectaţi tot

ini_set('display_errors',1);
error_reporting(E_ALL);
 

evident Mesaje:168
imi da eroarea:

Cod: Selectaţi tot

Notice: Undefined variable: db_conx in C:\wamp\www\test\wall_mysqli\includes\FbWall.php on line 21
linia 21:

Cod: Selectaţi tot

 $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()); 
Eu nu am inclus in acest fisier conectarea la baza de date $db_conx
O includ cand am nevoie de ea
Cand includ fisierul cu functii includ si conectarea...

Cod: Selectaţi tot

include_once 'includes/db.php';
include_once 'includes/FbWall.php';
Iar daca includ conectarea la db
nu imi mai apare nici un rezultat nimic...pagina este goala fara nimic pe ea...nici macar formularul care trimite la aceste funcii nu imi mai apare

MarPlo Mesaje:4343
Variabilele care nu sunt definite, sau nu sunt transferate la functii prin parametru, nu exista in cadrul acelei functii.
Degeaba se include fisierul cu acea variabila inainntea functiei. Cum e precizat in raspunsul anterior, trebuie transmisa variabila cu obiectul de conectare la functie.
Oricum, s-ar putea ca modificarea acelui script pentru MySQLi sa fie ceva mai complex, fiind vorba de clase si diferite alte fisiere incluse care poate trebuie editate. Nu e ceva de rezolvat doar prin intrebari pe forum, ci, daca nu cunosti destul de bine lucrul cu clase, functii si domeniu de definitie in php, solutia ar fi sa dai scriptul pentru modificare la cineva care se ocupa cu asa ceva contra cost.

evident Mesaje:168
Am rezolvat aici tot,
Am vazut pe un forum o problema asemanatoarea cu a mea.
Trebuia doar sa definesc $db_conex si $loadmore ca si variabile globale in fiecare functie.

Multumesc