Make an SQL query with codeigniter

Asked

Viewed 1,036 times

0

I’d like to pull some information from my database called "website", inside it would connect to the table: "posts" and in it wanted to show the information as: "id, updated, title, Description, link, image" 3 limit and fit my code. I am 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 -->
Show 1 more comment

2 answers

2

The first step is to configure the file application/config/database.php with database information such as driver, user, password, database 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 database library (or if it already loads by default on all controllers via application/config/autoload.php, the code for consultation 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 a line or object itself.

  • I’m not succeeding, I’m one using a custom codeigniter page "custom.php", I have a page called "posts_list.php" would be like "Include" in "custom.php"?

  • @Evertongouveia you have a controller? it should make the query and pass the result to the view I imagine it is the posts_list.php there do the foreach. In the documentation has a hello world of how you should use it

0

it was not very clear your question, but come on, you need to connect in the database "website", do so, I will try to demonstrate in the simplest way:

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 in the posts table:

 $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 that you have to manipulate the array return where you need, 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 so:

<ul class="list-inline">

thereby adding:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.