I can’t display all db - PHP and MYSQLI results

Asked

Viewed 25 times

-3

Oops, well I’m not being able to display all the data in my database. I don’t know why it’s because (maybe) there’s nothing wrong with my code

My code

<?php
$host = '';
$user = '';
$pass = '';
$db = '';

$conn = mysqli_connect($host, $user, $pass, $db);

$sql = 'SELECT * FROM 'cast_vbr' WHERE 1';
$query = mysqli_query($conn, $sql);
$r = mysqli_fetch_assoc($query);
if($r) {
   echo $r['title'];
}
?>

1 answer

0

Try this code:

$conn = mysqli_connect('localhost', 'usuario_do_banco', 'senha_do_usuario', 'nome_do_banco') or die("Connection failed: " . mysqli_connect_error());

    if (!mysqli_set_charset($conn, 'utf8')) {
        printf('Error ao usar utf8: %s', mysqli_error($conn));
        exit;
    }

$id = "1";

$busca_titulo = $conn->prepare("SELECT titulo FROM cast_vbr WHERE id=?");
    $busca_titulo->bind_param("i", $id);

    if(!$busca_titulo->execute()){
       echo $busca_titulo->error;
    }
    
    $busca_titulo->store_result();
    $registros = $busca_titulo->num_rows;
    $busca_titulo->bind_result($titulo);
    
    if ($registros != 0){
        $busca_cliente->fetch();
        echo $titulo;
    }

Browser other questions tagged

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