PHP variable title

Asked

Viewed 104 times

2

Good afternoon, I’m trying to leave each page with a different title based on the id. I have to say that I am very beginner in the area and probably not a big deal, my code is 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 you to return the title of product 34, someone gives me a strength here ?

  • This is not enough to extract the result of the database, ie lacked the connection code and query.

  • And how would it look ?

  • You can see some examples, PDO or Mysqli

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>  
  • Thank you so much for the solution and the full explanation. I’m very grateful, really dear.

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>
  • Gave this: Warning: mysqli_query() expects Parameter 1 to be mysqli, Object Given in C: xampp htdocs mist critica_conteudo.php on line 9 Warning: mysqli_fetch_object() expects expects Parameter 1 to be mysqli_result, null Given in C: xampp htdocs mist critica_conteudo.php on line 11

0

Oops. You need to run and return the query. You can do it like 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 the Cakephp or Laravel. Or you can 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];
  • Returned this on page : Warning: mysql_fetch_assoc() expects Parameter 1 to be Resource, Boolean Given in C: xampp htdocs mist critica_conteudo.php on line 10

  • The functions mysql_ were deprecated in php5.x and removed in php7. It is not a good idea to recommend them.

Browser other questions tagged

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