Make a SELECT COUNT(*) FROM table

Asked

Viewed 457 times

2

Make a (SELECT COUNT('id') FROM 'imagem'), take this value and pass it as a parameter in a function. How do?

<?php
    require("Configs/connection.php");
?>

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Fotos do Casamento</title>

        <link href="css/sl-main.css" rel="stylesheet">

    </head>

    <body onload="iniciaSlider("Colocar aqui o valor COUNT")">

        <div id="moldura"></div>
        <input id="abutton" type="button" value="Anterior"
onclick="troca(-1)"/>
        <input id="pbutton" type="button" value="Próximo" onclick="troca(1)"/>
        <div>
        <input id="vbutton" type="button" value="Voltar" onclick="voltar()"/>
        </div>
        <script src="js/sl-main.js"></script>


    </body>
</html>
  • 2

    can you give more information? You have some code?

  • 1

    Could I show you the full code please ?

1 answer

3


Your file

<?php
    require("Configs/connection.php");
  $host = 'localhost'; // endereço do servidor de banco de dados.
    $dbname = 'tabela'; //nome de sua base de dados
    $user   = 'root'; // seu usuário do banco
    $pass   = 'senha'; // sua senha do banco
    $pdo = new PDO('mysql:host='.$host.';dbname='.$dbname,$user,$pass);

    $lista = $pdo->prepare('SELECT count(*) total from imagem') or trigger_error($pdo->error);
    $lista->execute();
    $item = $lista->fetchAll(PDO::FETCH_ASSOC);
    $total = $item[0]['total'];
?>

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Fotos do Casamento</title>

        <link href="css/sl-main.css" rel="stylesheet">

    </head>

    <body onload="iniciaSlider(<?php echo $total; ?>)">

        <div id="moldura"></div>
        <input id="abutton" type="button" value="Anterior"
onclick="troca(-1)"/>
        <input id="pbutton" type="button" value="Próximo" onclick="troca(1)"/>
        <div>
        <input id="vbutton" type="button" value="Voltar" onclick="voltar()"/>
        </div>
        <script src="js/sl-main.js"></script>


    </body>
</html>
  • I’ll try it, thank you very much.

  • You’re gonna have an easy fix "iniciaSlider("<?php echo $total; ?>")" should be "iniciaSlider('<?php echo $total; ?>')" or "iniciaSlider(<?php echo $total; ?>)"

  • 1

    Thanks @Virgilionovic corrected ^^

  • This giving error here "$list = $db->prepare('SELECT Count(*) total from image') or trigger_error($db->error);"=> Fatal error: Call to a Member Function prepare() on string

  • where you have written DB, exchange for PDO => $list = $Pdo->prepare('SELECT Count(*) total from image') or trigger_error($Pdo->error);

  • It worked perfectly. Thank you for your attention.

Show 1 more comment

Browser other questions tagged

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