Mysql PDO syntax to Select Data from mysql database

$dbname = DB_NAME; // your database name
$servername = DB_HOST ; // your host name
$username = DB_USER ; // your user name
$password = DB_PASSWORD ;
try {
    $conn = new PDO(“mysql:host=$servername;dbname=$dbname”, $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo “Connected successfully”;
$sql=”SELECT  post_content,ID  FROM wp_posts WHERE  post_type=’resources’  AND post_status=’publish'”;
 
$page=$conn->prepare($sql);
$page->execute();
$row = $page->fetchAll(PDO::FETCH_ASSOC);
foreach($row as $values)
{
print_r($row); print each row  from table here
}
}
catch(PDOException $e)
 {
    echo “Connection failed: ” . $e->getMessage();
 }

Leave a Reply

Your email address will not be published. Required fields are marked *