Variable Title PHP

2

Good afternoon, I'm trying to leave each page with a different title picking based on the id. I have to tell you that I'm a good beginner in the field and probably should not be anything special, my code looks like this:

<?php
    include 'conta/config.php';

    $codigo = $_GET["id"];
        //echo $id;
        //exit;
    $titulo = "SELECT titulo FROM tbl_criticas WHERE id = $codigo;";

    ?>

    <!DOCTYPE html>
    <html lang="pt-br">

    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

        <title>
            <?php echo $titulo; ?>
        </title>

In the title it returns " SELECT title FROM tbl_criticas WHERE id = 34; ". But I would like it to return the product title 34, does anyone give me a strength here?

    
asked by anonymous 11.10.2016 / 19:09

3 answers

1
<?php
$host    = 'localhost';  
$usuario = 'root';  
$senha   = '';  
$banco   = 'sua_database';  
$porta   = 3306;  
$charset = 'utf8'; // ou pode ser latin1 - depende do charset que vc está usando  

/**  
 * Conecta com o banco de dados  
 */  
$pdo = new PDO("mysql:host={$host};dbname={$banco};port={$porta}", $usuario, $senha, array(  
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,  
));  

/**  
 * Configura o charset  
 */  
$pdo->exec("SET NAMES {$charset} COLLATE {$charset}_general_ci");  

/**  
 * Pega o ID via GET e executa a query  
 */  
$codigo = (int)$_GET['id'];  
$stm    = $pdo->query("SELECT titulo FROM tbl_criticas WHERE id = {$codigo};");  

/**  
 * Pega a linha retornada pela consulta  
 */  
$linha = $stm->fetch(PDO::FETCH_ASSOC);  
?>  

....  
    <title>  
        <?php echo $linha['titulo']; ?>  

        OU ....  
        <?= $linha['titulo']; ?>  
    </title>  
...  

<b>Esse é um script utilizando PDO e conectando ao MYSQL</b>  
    
11.10.2016 / 19:41
0
<?php
include 'conta/config.php';

$codigo = $_GET["id"];
    //echo $id;
    //exit;
$titulo = "SELECT titulo FROM tbl_criticas WHERE id = $codigo;";
// $connection é a variavel de ligação ao seu banco de dados. que deve estar no seu config.php

$resultado = mysqli_query($connection,$titulo);

while ($row = mysqli_fetch_object($resultado)) {

$titulo_pagina = $row->$seu_campo_titulo_do_banco_de_dados;

}

<!DOCTYPE html>
<html lang="pt-br">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

    <title>
        <?php echo $titulo; ?>
    </title>
    
11.10.2016 / 19:23
0

Oops. You need to execute and return the query. You can do this:

$query = mysqli_query("SELECT titulo FROM tbl_criticas WHERE id = {$codigo} LIMIT 1");//Executa a query

$titulo = mysqli_fetch_assoc($query)['titulo'];//Retorna o título

But the ideal is that you use some framework. Like CakePHP or Laravel . Or you can still use the PDO extension.

$pdo = new PDO('mysql:host=host;dbname=banco', 'user', 'senha');
$query = $pdo->query("SELECT titulo FROM tbl_criticas WHERE id = {$codigo} LIMIT 1");
$titulo = $query->fetch(PDO::FETCH_ASSOC)['titulo];
    
11.10.2016 / 19:28