1

I got this error, times per row:

Notice: Undefined index: enviopre in /opt/lampp/htdocs/pruebax/pruebaxone.php on line 34

Notice: Undefined index: enviofra in /opt/lampp/htdocs/pruebax/pruebaxone.php on line 35

Notice: Undefined index: enviofec in /opt/lampp/htdocs/pruebax/pruebaxone.php on line 36

Notice: Undefined index: envioval in /opt/lampp/htdocs/pruebax/pruebaxone.php on line 37
.
.
.

Which means is returning all the values from the table but It can't be stored in the array. I tried changing the way each $row receive the parameter, instead of the field name using the position in number of the array for example: $row[1] $row[2] $row[3] ... Still doesn't work...

this is my code:

<?php


class pruebax{
private static $cn = null;

    public static function conectar(){
        if (self::$cn !==null){
            return self::$cnn;          
        }

        try{
            $cn = new PDO("informix:host=localhost; service=30000;database=mrroot; server=mrserver;protocol=onsoctcp;EnableScrollableCursors=1", "mrtony", "");

            return $cn;

        }catch (PDOException $ex){

            die($ex->getMessege());

        }
    }
}

public static function consulta(){

$query = "SELECT * FROM fr_envio";

$cn = pruebaxone::conectar();

$resultado = $cnx->prepare($query);

$resultado->execute();


$rows = $query->fetch(PDO::FETCH_ASSOC);
echo '<table>';
foreach ($rows as $row){

        echo '<tr>';
        echo '<td>'.$row['enviopre'].'</td>';
        echo '<td>'.$row['enviofra'].'</td>';
        echo '<td>'.$row['enviofec'].'</td>';
        echo '<td>'.$row['envioval'].'</td>';
        echo '</tr>';

       }
echo '</table>';
}

$prueba = new pruebaxone();
$prueba->consulta();

?>

Sorry if I made an obvious mistake, I'm learning...

  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – aynber Dec 11 '17 at 20:02
  • 2
    `fetch()` returns a single row, so your foreach is actually iterating through the columns of that row. Try `fetchAll()` instead. – aynber Dec 11 '17 at 20:03
  • tried... didn't work... TnT – Brayan Rodriguez Dec 11 '17 at 20:13
  • 2
    `var_dump($rows);` outside of your loop or `var_dump($row);` inside of your loop, see what it contains. – aynber Dec 11 '17 at 20:17
  • hey, it throws all of the rows from my table... all the 9796 values... – Brayan Rodriguez Dec 11 '17 at 21:54
  • array(2449) { [0]=> array(4) { ["ENVIOPRE"]=> string(1) "A" ["ENVIOFRA"]=> string(6) "855211" ["ENVIOFEC"]=> string(10) "2015-07-07" ["ENVIOVAL"]=> string(8) "27233.00" } [1]=> array(4) { ["ENVIOPRE"]=> string(1) "A" ["ENVIOFRA"]=> string(6) "855847" ["ENVIOFEC"]=> string(10) "2015-07-09" ["ENVIOVAL"]=> string(9) "141752.00" }................ – Brayan Rodriguez Dec 11 '17 at 21:56
  • 2
    Your columns are uppercase, so your keys should be, too. PHP is case-sensitive in most cases. – aynber Dec 12 '17 at 03:09
  • 1
    'Tis curious that the column names are showing up in upper-case. Most of the time, Informix treats them as lower-case, as you tried in the question. It would be mildly interesting to know where the case-conversion occurs, but at least you now know how to deal with it. – Jonathan Leffler Dec 12 '17 at 03:52
  • Didn't know that case sensitive.... hehe thanks :d – Brayan Rodriguez Dec 12 '17 at 14:05

1 Answers1

1

try specifying the columns in upper case:

root@irk:/usr3/products/php53# cat i.php
<?php

$db = new PDO("informix:host=irk;service=3046;database=stores7;server=irk1210;protocol=onsoctcp;EnableScrollableCursors=1;client_LOCALE=en_Us.utf8;db_locale=en_us.819;OPTIMIZEAUTOCOMMIT=1;", "informix", "ximrofni");

print "Connection Established!\n\n";

    $resultado = $db->query("select first 5 * from systables");
    $resultado->execute();
        echo '<table>';
        while ($row = $resultado->fetch(PDO::FETCH_ASSOC))
            {
                echo '<tr>';
                echo '<td>'.$row['TABID'].'</td>';
                echo '<td>'.$row['TABNAME'].'</td>';
                echo '<td>'.$row ['OWNER'].'</td>';
                echo '</tr>';
            }
        echo '</table>';
?>
root@irk:/usr3/products/php53# php i.php
Connection Established!

<table><tr><td>1</td><td>systables</td><td>informix                        </td></tr>
<tr><td>2</td><td>syscolumns</td><td>informix                        </td></tr>
<tr><td>3</td><td>sysindices</td><td>informix                        </td></tr>
<tr><td>4</td><td>systabauth</td><td>informix                        </td></tr>
<tr><td>5</td><td>syscolauth</td><td>informix                        </td></tr></table>
root@irk:/usr3/products/php53# 
jsagrera
  • 1,978
  • 6
  • 8