1

I tried to connect my MySQL database kuvarskir with my PHP code. Then inputted this into html:

<?php
        include "mysql.php";
        $recepti = execute_sql("SELECT * FROM recepti");
        foreach($recepti as $recept)
        {
        ?>
            <article>
                <h2><?=$recept["naziv"]?></h2>
                <i><?=$recept["sastojci"]?></i>
                <p><?=$recept["uputstvo"]?></p>
            </article>

        <?php
        }
        ?>

but its not working.

i think there is error with mysql.php file, here's the code:

<?php
function init_sql()
{

    global $conn;
    $host = "localhost";
    $db = "kuvarskir";

    $username = "root";
    $password = "root";

    try
    {
        $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;

        $conn = new PDO("mysql:host=$host;dbname=$db", $username, $password, $pdo_options);
    }
    catch(PDOException $e)
    {
        die("Database connection failed!");
    }
}

function execute_sql($sql, $params=[])
{
    global $conn;

    if ($conn == null)
        init_sql();
    try
    {
        $stmt = $conn->prepare($sql);
        $stmt->execute($params);

        $data = array();

        if ($stmt->columnCount() == 0) //INSERT / UPDATE
            return $stmt->rowCount();

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            $data[] = $row;

        return $data;
    }
    catch(Exception $e)
    {
        die("somting glitched. error is: $e");
    }
}
?>

maybe, there is problem with my localhost, which is localhost:8080/phpmyadmin

also here is picture of database

db

user3783243
  • 5,368
  • 5
  • 22
  • 41

0 Answers0