1

これが私のコーディングです。私はphpを初めて使用します。モデルでクエリを記述するために使用するcodignterをよく使用しますが、クエリをどこに書くべきかわかりません。このコードは次のエラーを生成します

注意: 未定義のオフセット: C:\wamp\ の 0

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>
4

2 に答える 2

1

接続する必要があるデータベースからデータを取得する前に、さまざまな方法でこれを行うことができます。例として、mysqli new を使用します。

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

この例では mysqli を使用します。これは、mysql コマンドが非推奨になり、PHP の次のバージョンで削除されるためです。

于 2013-09-08T14:33:44.787 に答える
0

mysql_fetch_assoc()連想配列を返すを使用しています

$fields['id'] 

ループではなく一度しか使用していないため、複数の行が返されることはありません(Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead

$fields = mysql_fetch_assoc( $result );

しかし、配列にオブジェクトとしてアクセスしようとしています

$fields[$j]->id

mysql_fetch_object()そのため、代わりに使用する必要がありますmysql_fetch_assoc()


他のコメント/回答と同様に、次のいずれかに切り替える必要がありmysqliますPDO- http://php.net/manual/en/mysqlinfo.api.choosing.php

于 2013-09-08T18:32:27.193 に答える