0

I'm using this simple PHP code below to read data from my module's table, but can't do the same thing using Magento codes.

Tried to do that using different articles but was either getting errors or the whole table structure was displayed in the page instead of just the information.

How can I do that?

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "";
    $dbname = "dbname";

   // Create connection
   $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
   if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
   } 

   $sql = "SELECT comment_txt, name, comment_date FROM mytablename where productcomment_increment_id='$productId' && comment_status='Approved'";
   $result = $conn->query($sql);

   if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<i><b>Author Name:</b></i>";
        echo $row["name"];
        echo "<br /><i><b> Comment: </i></b>";
        echo $row["comment_txt"];
        echo "<br /><i><b>Date: </i></b>";
        echo $row["comment_date"];
        echo "<br><br>";
    }
   } else {
   //echo "0 results";
   }
   $conn->close();
   ?>

I will greatly appreciate helpful answers.

1 Answer 1

2

You can use below mentioned code

//database read adapter 

$read = Mage::getSingleton('core/resource')->getConnection('core_read'); 

$results = $read->fetchAll("select * from your_tablename"); 
foreach($results as $row)
{
    echo "<i><b>Author Name:</b></i>";
    echo $row["name"];
    echo "<br /><i><b> Comment: </i></b>";
    echo $row["comment_txt"];
    echo "<br /><i><b>Date: </i></b>";
    echo $row["comment_date"];
    echo "<br><br>";
}

For more read here.

6
  • Thanks @Arunendra , but it displays the database structure, not just the values. I'll attach a screenshot, please take a look. How can I fix that? Commented Mar 28, 2017 at 17:00
  • @bestwebdevs It's working fine as you can see the output in the given picture.
    – Arunendra
    Commented Mar 29, 2017 at 9:09
  • Yes, but I need to display values separately, not the whole table structure. Like "Name" : "Ben", "Comment" : "Some comment text...". Could you help me do that? Thanks @Arunendra Commented Mar 29, 2017 at 17:17
  • How do I get a particular column value instead of the whole table structure and values? Commented Mar 29, 2017 at 20:35
  • @bestwebdevs I have updated the answer plz check .
    – Arunendra
    Commented Mar 30, 2017 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.