Search code examples
phpmysqlpdo

Fetch data from db using PDO


I'm new to PDO, I'm using it as advised by senior users in this website. I'm trying to get data from my table using pdo, using while, so I can get all the data "organized".

My query is working, but for some reason I can't even dump it.

Heres my code:

$sql = $conn->query("SELECT id, nivel, tipo, titulo, texto, ativa FROM account.quests_faq WHERE ativa='YES' ORDER BY nivel DESC");
        while($row = $conn->fetch(PDO::FETCH_ASSOC)){
            if ($tipo=='main'){
                echo '<li><a href="index.php?s=quest_info&id='$row['id']'"><font color="green">Nivel '$row['nivel']' - '$row['titulo']'</font></a></li><br>';
            }else{
                echo '<li><a href="index.php?s=quest_info&id='$row['id']'"><font color="red">Nivel '$row['nivel']' - '$row['titulo']'</font></a></li><br>';
            }
        }

So, in a resume. I have a table with titles, some text and an id. I want to get this data from it and echo it.

Hope you can help me, sorry for the newb doubt.

EDIT 1:

$username = 'sssss';
        $password = 'sssss';
        $conn = new PDO('mysql:host=xxxxxxxx;dbname=account', $username, $password);
            $sql = "SELECT id, nivel, tipo, titulo, texto, ativa FROM account.quests_faq WHERE ativa='YES' ORDER BY nivel DESC";
            $stmt = $conn->query($sql);
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                 echo '<li><a href="index.php?s=quest_info&id='.$row['id'].'"><font color="green">Nivel '.$row['nivel'].' - '.$row['titulo'].'</font></a></li><br>';
            }else{
                echo '<li><a href="index.php?s=quest_info&id='.$row['id'].'"><font color="red">Nivel '.$row['nivel'].' - '.$row['titulo'].'</font></a></li><br>';
            }
        }

Solution

  • Well, advise you were given is wrong.

    Not use but learn.
    You have to learn something before using it.
    There are many tutorials on PDO around (all of them crappy ones though) but at least you can learn proper syntax from there

        $sql = "SELECT id, nivel, tipo, titulo, texto, ativa FROM account.quests_faq WHERE ativa='YES' ORDER BY nivel DESC";
        // look this string contains SQL query. so, the variable is named $sql
        $stmt = $conn->query($sql);
        // in the next line we are getting a statement object from the function query()
        // this is why variable called $stmt
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        // and now we can start iterating this statement.
        // statement, Carl. Not connection to database
        // which is called $conn, if you get an idea
    

    also you have to enable error reporting for PDO.

    And yes, as it was said in the other answer, your PHP syntax is also wrong. You are supposed to learn it too, instead of banging together random lines of code and then asking others to fix it for you.

    Start from less complex syntax, from echoing one single variable without decoration. And ask one question per post. As for the PDO part you already got the answer