I need help on this php pdo error:PHP PDO - Uncaught Error: Call to a member function prepare() on null

Recommended Answers

All 4 Replies

Show your code. It means you did not (correctly) create a PDO object.

It should look something like this:

$a=new PDO("mysql:host=localhost;dbname=database;","root","");
$b=$a->prepare("UPDATE `users` SET `user`='me'");
$b->execute();

Taken from:
https://www.php.net/manual/en/pdo.prepare.php

<?php
                                    //$select_stmt=$db->prepare("SELECT * FROM tbl_file");  //sql select query
                                    //$select_stmt->execute();
                                    //while($row=$select_stmt->fetch(PDO::FETCH_ASSOC))

                        $sql = $con->prepare("SELECT * FROM tbl_file");
                        $sql->execute();
                        while($fetch = $sql->fetch())

                                    {
                                    ?>
                                        <tr>
                                            <td><?php echo $fetch['name']; ?></td>
                                            <td><img src="upload/<?php echo $fetch['image']; ?>" width="600px" height="350px">
                                            <?php $text="Photo de la ceremonie de:"; echo"$text";?>
                                            <marquee behavior="alternate" direction="right" scrollamount="1">
                                            <?php  echo $fetch['name'] ; ?> </marquee></td>

                                        </tr>
                                    <?php
                                    }
                                    ?>
** $sql = $con->prepare("SELECT * FROM tbl_file");** where it shows error

I'm not seeing where in your code you declare the variable $con that holds the connection. You can't prepare a MySQL statement if the connection doesn't exist.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.