Problem in connection with database

0

I am changing a code from a news system that came with charset iso ... I now want to change it to utf8.

Now the problem is when I click on read more.

Ihavealreadyalteredthetablesforutf8andIworkwiththepagesinutf8,Ithinktheproblemisintheconnection.

Ihavetwofilesinsideafoldercalleddatabase.

database.conf

<?phpglobal$databases;$databases=array('local'=>array('host'=>'localhost','port'=>3306,'dbname'=>'noticias','user'=>'root','password'=>''));/*endfile*/

andmysql.php

<?phpClassmysql{public$query;public$data;public$result;public$rows;public$page=0;public$perpage=10;public$current=1;public$url;public$link='';public$total='';public$pagination=false;protected$config;protected$host;protected$port;protected$user;protected$pass;protected$dbname;protected$con;publicfunction__construct(){try{#arraycomdadosdobancoinclude'database.conf.php';global$databases;$this->config=$databases['local'];#Recuperaosdadosdeconexaodoconfig$this->dbname=$this->config['dbname'];$this->host=$this->config['host'];$this->port=$this->config['port'];$this->user=$this->config['user'];$this->pass=$this->config['password'];#instanciaeretornaobjeto$this->con=mysql_connect("$this->host", "$this->user", "$this->pass" );
            mysql_select_db( "$this->dbname" );
            if ( !$this->con )
            {
                throw new Exception( "Falha na conexão MySql com o banco [$this->dbname] em database.conf.php" );
            }
            else
            {
                return $this->con;
            }
            $this->url = $_SERVER['SCRIPT_NAME'];
        }
        catch ( Exception $e )
        {
            echo $e->getMessage();
            exit;
        }
        return $this;
    }

    public function query( $query = '' )
    {
        try
        {
            if ( $query == '' )
            {
                throw new Exception( 'mysql query: A query deve ser informada como parâmetro do método.' );
            }
            else
            {
                $this->query = $query;
                if($this->pagination == true){  
                    mysql_query("SET NAMES 'utf8'");
                    mysql_query('SET character_set_connection=utf8');
                    mysql_query('SET character_set_client=utf8');
                    mysql_query('SET character_set_results=utf8');
                    $this->result = mysql_query( $this->query );
                    $this->fetchAll();
                    $this->paginateLink();
                    $this->query .= " LIMIT $this->page, $this->perpage";
                    $this->pagination = false;
                }
                $this->result = mysql_query( $this->query );

            }
        }
        catch ( Exception $e )
        {
            echo $e->getMessage();
            exit;
        }
        return $this;
    }

    public function fetchAll()
    {
        $this->data = "";
        $this->rows = 0;
        while ( $row = mysql_fetch_array( $this->result, MYSQL_ASSOC ) )
        {

            $this->data[] = $row;
        }
        if ( isset( $this->data[0] ) )
        {
            $this->rows = count( $this->data );
        }
        return $this->data;
    }

    public function rowCount()
    {
        return @mysql_affected_rows();
    }    

    public function getUrl($perpage)
        {
            $this->url = $_SERVER['REQUEST_URI'];
            return $this;
        }   
        public function paginate($perpage)
        {
            $this->pagination = true;
            $this->perpage = $perpage;
            return $this;
        }
        public function paginateLink()
        {   
            if(!preg_match('/\?/',$this->url))
            {
                $this->url .= "?";
            }else{
                $this->url .= "&";
            }
            if ( isset( $_GET['page'] ) )
            {
                $this->current = $_GET['page'];
                $this->page = $this->perpage * $_GET['page'] - $this->perpage;
                if ( $_GET['page'] == 1 )
                {
                    $this->page = 0;
                }
            }
            $this->total = $this->rows;
            if ( $this->rows > $this->perpage )
            {                           
                $this->link = "<div class=\"pagination\"><ul>";
                $prox = "javascript:;";
                $ant = "javascript:;";
                if ( $this->current >= 2 )
                {
                    $ant = $this->url."page=" . ($this->current - 1);
                }
                if ( $this->current >= 1 && $this->current < ($this->total / $this->perpage))
                {
                    $prox = $this->url."page=" . ($this->current + 1);
                }
                $this->link .= '<li><a href="' . $ant . '">&laquo;</a></li>';
                $from = round( $this->total / $this->perpage );
                if($from == 1){$from++;}

                for ( $i = 1; $i <= $from ; $i++ )
                {
                    if ( $this->current == $i )
                    {
                        $this->link .= "<li class=\"active\"><a>$i</a></li>\n";
                    }
                    else
                    {
                        $this->link .= "<li><a href=\"".$this->url."page=$i\">$i</a></li>\n";
                    }
                }
                $this->link .= '<li><a href="' . $prox . '">&raquo;</a></li>';
                $this->link .= "</ul>\n";
                $this->link .= "</div>\n";
            }   
            return $this;
        }

        public function cut($str,$chars,$info=  '')
        {
            if ( strlen( $str ) >= $chars )
            {
                $str = preg_replace( '/\s\s+/', ' ', $str );
                $str = strip_tags( $str );
                $str = preg_replace( '/\s\s+/', ' ', $str );
                $str = substr( $str, 0, $chars );
                $str = preg_replace( '/\s\s+/', ' ', $str );
                $arr = explode( ' ', $str );
                array_pop( $arr );
                //$arr = preg_replace('/\&nbsp;/i',' ',$arr);
                $final = implode( ' ', $arr ) . $info;
            }
            else
            {
                $final = $str;
            }
            return $final;
        }

    }


    /* end file */

I had added these lines in the query function:

                mysql_query("SET NAMES 'utf8'");
                mysql_query('SET character_set_connection=utf8');
                mysql_query('SET character_set_client=utf8');
                mysql_query('SET character_set_results=utf8');

With these I got the news to be listed in utf8.

Now I intend that when I click on read more the news present also in utf8.

    
asked by anonymous 08.12.2014 / 22:10

2 answers

0

The error in your script is at the time of connection. Soon after connecting you should use the command SET NAMES utf8 ; This command is required only once per connection, so there is no need to use it for every query executed;

Notice as well the depreciation of mysql functions _ *

Use mysql i _ * whenever possible or else or PDO ;

See in this post because we should no longer use the mysql functions _ *

    
09.12.2014 / 11:47
2

As the sql language is sequential, it may be just a query problem positioning problem.

Try to reallocate the queries

mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');

shortly after include 'database.conf.php';

Or even better: include these queries WITHIN the connection file, right after mysql_select_db

    
08.12.2014 / 22:22