New question

Question:

Date: 09-03-2016 17:34:41 (In Spanish)

¿Cómo cambiar de MySQL clásico a MySQLi?[Resolved]

Siguiendo un tutorial comence a crear un blog usando una clase(primera vez tratando de usar POO en php) y asi dentro de ella usando funciones para cada modulito que yo necesitaba. Localmente todo bien, pero anoche intente subirlo a un hosting gratuito y al subirlo me marca desde el principio que "mysql_connect" ya es obsoleto y debo cambiar a MySQL PDO o MySQLi, busque en internet y encontre que dentro de cada query igual tengo que hacer cambios, intente hacerlos pero ya no me da nada, les comparto mi clase que original cree y espero que alguien me pueda orientar para asi mudar la clase a MySQLi, espero me puedan ayudar y que tengan buena noche.
 
<?php
    session_start();
    class Conectar
    {
        public static function con()
        {
            $con = mysql_connect("localhost","root","");
            mysql_query("SET NAMES 'utf8'");
            mysql_select_db("blog_php");
            return $con;
        }
        
        public static function corta_palabra($palabra,$num)
        {
            $largo = strlen($palabra);
            $cadena = substr($palabra,'0',$num);
            return $cadena;
        }
        
    }

    class Trabajo
    {
        private $cat=array();
        private $noticias=array();
        private $post=array();
        private $comentarios=array();
        private $ultimas=array();
        
        public function get_categoria()
        {
            $sql = "select * from categorias";
            $res = mysql_query($sql,Conectar::con());
            while($reg=mysql_fetch_assoc($res))
            {
                $this->cat[]=$reg;
            }
            return $this->cat;
        }
        public function get_noticia_por_categoria()
        {
            $sql = "select c.id_categoria, count(n.id_noticia) as num
  from categorias c left outer join noticias n 
    on c.id_categoria = n.id_categoria group by c.id_categoria;";
            $res = mysql_query($sql,Conectar::con());
            while($reg=mysql_fetch_assoc($res))
            {
                $this->cat[]=$reg;
            }
            return $this->cat;
            
        }
        
        public function get_paginacion_noticias($inicio,$c)
        {
            //$sql = "select * from noticias order by id_noticia desc limit $inicio,2";
            $sql = "select * from noticias where id_categoria=$c order by id_noticia desc limit $inicio,2";
            $res=mysql_query($sql,Conectar::con());
            while($reg=mysql_fetch_assoc($res))
            {
                $this->noticias[]=$reg;
            }
            return $this->noticias;
            
        }
        
        public function total_comentarios($id_noticia)
        {
            $sql = "select count(*) as cuantos from comentarios where id_noticia = '$id_noticia'";
            $res = mysql_query($sql,Conectar::con());
            if($reg = mysql_fetch_array($res))
            {
                $total=$reg["cuantos"];
            }
            return $total;
        }
        
        public function get_post_por_id()
        {
            $sql = "select * from noticias where id_noticia=".$_GET["id"];
            $res = mysql_query($sql,Conectar::con());
            while($reg=mysql_fetch_assoc($res))
            {
                $this->post[]=$reg;
            }
            return $this->post;
        }
        
        public function insertar_comentario()
        {
            //print_r($_POST);
            $sql = "insert into comentarios values
(null,'".strip_tags($_POST["nom"])."','".strip_tags($_POST["correo"])."','".strip_tags($_POST["web"])."','".strip_tags($_POST["mensaje"])."',now(),'".strip_tags($_POST["id_noticia"])."');
        ";
            //echo $sql;
            $reg=mysql_query($sql,Conectar::con());
            echo "
            <script type='text/javascript'>
                alert('El comentario se realizo con exito');
                window.location = '".$_POST["url"]."';
            </script>";
        }
        
        public function get_comentarios($id)
        {
            $sql = "select * from comentarios where id_noticia = '$id' order by id_comentario desc";
            $res = mysql_query($sql,Conectar::con());
            
            
                    while($reg = mysql_fetch_assoc($res))
                    {
                        $this->comentarios[]=$reg;
                    }
                    return $this->comentarios;
            
        }
        
        public function get_ultimas_10_noticias($id)
        {
            $sql = "select * from noticias where id_categoria > '1' order by id_noticia desc limit 5;";
            $res = mysql_query($sql,Conectar::con());
            
            while($reg = mysql_fetch_assoc($res))
            {
                $this->ultimas[]=$reg;
            }
            return $this->ultimas;
        }
        
        
        
    }
?>

Tags: Deprecated features - MySQL - OOP - PHP - PHP MySQLi - Question Votes: 2 - Answers: 3 - Views: 23 Share on: Google Facebook Twitter LinkedIn Link
 

Answers:

To actively participate in the community first must authenticate, enter the system.Sign In
 
frjcbbae garagebible.com