Getting specific data from wordpress database table PHP, MYSQL

WordPress is written using PHP as its scripting language and MySQL as its database management system. If you want to retrieve some information from the WordPress database you have use php and SQL

  • You can get database connection details by editing wp-config.php file from your wp root
  • You can use phpMyAdmin  to view tables and contents 


 <?php  
 // CREATE CONNECTION TO DATABASE   
  define("DB_HOST", "localhost");  
      define("DB_USER", "sgs_wr6y");  
      define("DB_PASSWORD", "jkkihgd0o99Dav");  
      define("DB_NAME", "sgs_wr6y");  
 // Create connection  
 $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);  
 // Check connection  
 if ($conn->connect_error) {  
   die("Connection failed: " . $conn->connect_error);  
 }   
 $sql = "SELECT post_title FROM wp_posts where post_status = 'publish' ORDER BY post_date ASC LIMIT 0,4";  
 $result = $conn->query($sql);  
 if ($result->num_rows > 0) {  
   // output data of each row  
   while($row = $result->fetch_assoc()) {  
     echo "news: " . $row["post_title"]. "<br>";  
   }  
 } else {  
   echo "0 results";  
 }  
 $conn->close();  
 ?>  


If you can't connect to the server, make sure your password, username and hostname are correct.


Getting specific data from wordpress database table PHP, MYSQL Getting specific data from wordpress database table PHP, MYSQL Reviewed by Unknown on 11:52 PM Rating: 5

1 comment:

  1. I was looking all over for how to do this. Thanks for posting! CodCow

    ReplyDelete

Powered by Blogger.