Make a SQL query with codeigniter

0

I would like to pull some information from my database called " site ", inside it would connect to the table: " posts " and in it wanted to show information such as: id, updated, title, description, link, image "with a limit of 3 and fit into my code. I'm using codeigniter.

<div class="blog-item col-md-4 col-sm-6 mb-30" style="position: absolute; left: 0px; top: 0px;">
    <article class="post-wrapper">

        <div class="thumb-wrapper">
            <a href="blog-single-fullwidth.html"><img src="link da imagem.jpg" class="img-responsive" alt=""></a>
        </div><!-- .post-thumb -->

        <div class="blog-content">
            <div class="case-studis-border"></div>
            <header class="entry-header-wrapper">
                <div class="entry-header">
                    <div class="entry-meta">
                        <ul class="list-inline">
                            <li>
                                <a href="Link">DATA</a>
                            </li>

                        </ul>
                    </div>

                    <h2 class="entry-title"><a href="link">Título</a></h2>

                </div><!-- /.entry-header -->
            </header><!-- /.entry-header-wrapper -->

            <div class="entry-content">
                <p>Descrição</p>
                <a href="link">ler mais</a>
            </div><!-- .entry-content -->

        </div><!-- /.blog-content -->

    </article>
</div> <!-- /.col-md-4 -->
    
asked by anonymous 17.08.2017 / 14:56

2 answers

2

The first step is to configure the application / config / database.php with the database information such as driver, user, password, bank name and other options

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'USUARIO',
    'password' => 'SENHA',
    'database' => 'MINHA_BASE',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

In the controller, remember to load the library database (or leave it loaded by default on all controls via the application code is as follows:

$query = $this->db->query('SELECT id, updated, title, description, link, image FROM posts');

foreach ($query->result() as $row)
{
        echo $row->id;
        echo $row->title;
        echo $row->description;
}

echo 'Total Results: ' . $query->num_rows();
  • result() returns an array of objects.

  • row() returns only one line or object itself.

17.08.2017 / 15:21
0

It was not very clear, but come on, you need to connect to the site database.

try {
        $usuario = 'site';
        $host = 'SEU HOST(se for interno é LOCALHOST)';
        $login = 'LOGIN(geralmente o ROOT)';
        $senha = 'SENHA';
        $pdo = new PDO("mysql:dbname=$usuario;host=$host", "$login", "$senha");
    } catch (Exception $exc) {
        echo $exc->getTraceAsString();
    }

To give the SELECT table in the posts:

 $sql = "SELECT id, updated, title, description, link, image FROM posts LIMIT 3";
    $sql = $pdo->prepare($sql);
    $sql->execute();
    $retorno = $sql->fetchAll();
    $quantidadeRegistros = $sql->rowCount();//TEM QUE SER 3, POR TER LIMIT 3

After this you have to manipulate the array return where you need it, for example in a list in your case in the list-inline for example, as you are basically using PHP and mysql I will demonstrate thus:

<ul class="list-inline">

adding like this:

   foreach($retorno as $valores){
$image = $valores['image'];  echo "<li>Sua imagem: $image</li>";  }
    
17.08.2017 / 15:20