Retrieve data from MYSQL database using PHP, it appears as question marks

Error
When i retrieved data from MYSQL database, it appears as question marks











Solution
"SET NAMES 'utf8' "  Add this code right after connecting to database

Example 01

 <?php   
  // CREATE CONNECTION TO DATABASE    
  define("DB_HOST", "localhost");   
    define("DB_USER", "stv_wrhp5");   
    define("DB_PASSWORD", "PGbnd05854199Dav");   
    define("DB_NAME", "stv_wrhp5");   
  // Create connection   
  $conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME);   
   $conn->query("SET NAMES 'utf8'");  
  // 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 DESC LIMIT 8";   
  $result = $conn->query($sql);   
  if ($result->num_rows > 0) {   
   // output data of each row   
   while($row = $result->fetch_assoc()) {   
    echo $row["post_title"]. "&nbsp;&nbsp; | &nbsp;&nbsp;";   
   }   
  } else {   
   echo "0 results";   
  }   
  $conn->close();   
  ?>   


Example 02

 <?php  
   // Open a new MySQL connection  
   $DB = new mysqli('localhost', 'user', 'root', 'dbname');  
   // Execute a query that sets the charset to be used in the following queries.  
   $DB->query("SET NAMES 'utf8'");  
   // Checks if a POST value named 'ta' was passed or not.  
   if (!empty($_POST['ta'])) {  
     // Update a row in the 'document' table using the passed data.  
     $DB->query("UPDATE document SET unicodeText='{$_POST['ta']}' WHERE ID=1");  
   }  
   // Fetch a row from the 'document', the same row we just updated.  
   $result = $DB->query("SELECT unicodeText FROM document WHERE ID=1");  
   // Assign the result from the previous query to the $return variable.  
   // I assume it will be used later in the code. Otherwise this line is pointless.  
   $return = $result->fetch_object();  
   // Close the result set from the previous query.  
   $result->close();  
 ?>  

100% work for me

Not the answer you're looking for? Please comment

Retrieve data from MYSQL database using PHP, it appears as question marks Retrieve data from MYSQL database using PHP, it appears as question marks Reviewed by Unknown on 1:02 AM Rating: 5

No comments:

Powered by Blogger.