1

Here is my coding I am new to php i often use codigneter there I use to write queries in models but i have no idea where to write the query. This code generates the error of

Notice: Undefined offset: 0 in C:\wamp\

and is unable to fetch data from DB

                  <div class="table">

             <table cellspacing="0" cellpadding="0">
            <thead>
             <tr>
                <th width="50" class="first">Id</th>
                <th width="117">Name</th>
                <th width="145">E.mail</th>
                <th width="63">Phone</th>
                <th width="145">Address</th>
                <th width="145">Payer Email</th>
                <th width="45">Amount</th>
                <th width="45" class="last">Status</th>
            </tr>
            </thead>
            <tbody class="table_body">
              <?php  
              $result = mysql_query("SELECT * FROM myorders ");
             $fields = mysql_fetch_assoc( $result );
               if(isset($fields)){
        if(count($fields)>0){
      for($j=0; $j<count($fields); $j++){ ?>
    <tr>
        <td class="first style2"><?php echo $fields[$j]->id?></td>
        <td><?php echo $fields[$j]->name?></td>
        <td><?php echo $fields[$j]->email?></td>
        <td><?php echo $fields[$j]->phone?></td>
        <td><?php echo $fields[$j]->address?></td>
        <td><?php echo $fields[$j]->payer_email?></td>
        <td><?php echo $fields[$j]->amount?></td>
        <td><?php echo $fields[$j]->status?></td>

        </tr>
    <?php } 
        }
  }
?>  
        </tbody></table>
        </div>
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/17630869#17630869 – samayo Sep 08 '13 at 14:02
  • As a note, `if(isset($fields)){ if(count($fields)>0){` can become `if(isset($fields) && count($fields)>0){` – Mr.Web Sep 08 '13 at 14:05
  • see if this works `` i think that you are getting a array instead of an object back – MZaragoza Sep 08 '13 at 14:07
  • You should not use `mysql_query` ([`This extension is deprecated as of PHP 5.5.0, and will be removed in the future`](http://de1.php.net/manual/en/function.mysql-query.php)) – t.niese Sep 08 '13 at 14:08
  • actually my page works find the only thing is that the values in the database are not displaying i think there is problem in query plz check my query specially thanx – user2753860 Sep 08 '13 at 14:33

2 Answers2

1

Before you can retrieve data from a database you need to connect to it, this can be done in different ways as an example I will use mysqli new

$mysqli = new mysqli("localhost", "database username", "database user password");
mysqli_query($mysqli, "query here");

I use mysqli in this example since mysql commands are deprecated and will be removed in the next version of PHP.

0

You are using mysql_fetch_assoc() which returns an associative array

$fields['id'] 

and you are only using it once not it a loop, so it will never return more than 1 row (Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead)

$fields = mysql_fetch_assoc( $result );

But you are trying to access your array as an object

$fields[$j]->id

so you need to use mysql_fetch_object() instead of mysql_fetch_assoc()


as with the other comments/answer, you should switch to either mysqli or PDO - http://php.net/manual/en/mysqlinfo.api.choosing.php

Sean
  • 12,443
  • 3
  • 29
  • 47