Hi Gentlemen

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

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);

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

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

$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

2 Years
Discussion Span
Last Post by ezekel

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"];


Edited by ezekel

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.