Hi Gentlemen

Please help the following code shoots error, my purpose is to view the table value for checking purpose, but it fires error,

<?php
include ('main_config.php');
//get all web settings
$web_settings = array();
try {
    $db = new PDO($mysql_host_db, $mysql_user, $mysql_pass);
    $query = "SELECT * FROM t_lfs_web_settings"; 
    //$result = $db->query($query);
    $result = $db->prepare($query);
    $result->execute();

    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        $web_settings[] = $row;
    }    
         foreach($web_settings as $key=>$val) {
            echo $val["gname"];
            echo $val;
         }

         return(0);
         }
catch(PDOException $e)
{    
    //echo $e->getMessage();
    echo "Error while retrieving web settings.";
}

$result->closeCursor();
$db = null;
Error messages as follows

Notice: Undefined index: gname in F:\xampp\htdocs\lfs2\site_globalfn.php on line 16

Notice: Array to string conversion in F:\xampp\htdocs\lfs2\site_globalfn.php on line 17

table fields

gld int
gname varchar(200)
gvalue text

These are notices, not severe errors. You can change your error reporting to E_ALL & ~E_NOTICE if you don't want to see them. Or you can simply check for the existence of values before you try to display them. E.g. if (!empty($var)) { echo $var; }.

Also, there's a "Reply to post" button so that you can add a reply to your question, so that you don't have to start a whole new post :).

why could you not do like this

 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
   echo $row['columnname'];
} 

//remove the foreach

if you want to use foreach

 while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
   $web_settings[] = $row;
  }
  foreach($web_settings as $key) {
    echo $key["gname"];

}
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.