0

As the title says I get an error where my database rows show the illegal string offset error in my rows if I try to output them from my SQL. Would like some help in resolving this.

enter image description here

This is the table I am trying to output:

Table Name: for_lease

enter image description here

I have three php files that connect to each other. Also this is already connected to the phpmyadmin so ill only show snippets.

for_lease.php (outputs the table)

<?php
  session_start();
  require_once('for_lease.vc.php');
?>

  <div class="row">
    <div class="col-md-12 form-group">
        <div class="col-md-12 bg-color-white shadow border-radius"><!-- List { -->
          <div class="row bg-color-dark-gold border-top-radius">
            <div class="col-md-12 color-white section-title-bar">
              <span class="ion-ios-list-outline"></span>&nbsp;&nbsp;FOR LEASE
            </div>
          </div>
          <div class="col-md-12 spacer">
            <a href="for_lease_add.php" class="color-dark-gold">
            <span class="ion-plus-round"></span> ADD FOR LEASE</a>
          </div>
          <div class="col-md-12 spacer">
          </div>

          <table class="table table-hover spacer">
            <thead align="center">
              <td></td>
              <td>NAME</td>
              <td>TYPE</td>
              <td>PRICE</td>
              <td>CONDITION</td>
              <td>CREATED</td>
            </thead>

            <?php foreach($lstProperty as $rowProperty) { ?>
              <tr align="center">
                <td>
                </td>
                <td>
                  <?php
                    echo($rowProperty['lease_name']);
                  ?>
                </td>
                <td>
                  <?php
                    echo($rowProperty['lease_type']);
                  ?>
                </td>
                <td>
                  <?php
                    echo($rowProperty['lease_price']);
                  ?>
                </td>
                <td>
                  <?php
                    echo($rowProperty['lease_condition']);
                  ?>
                </td>
                <td>
                  <?php
                    echo( date("Y-m-d", strtotime($rowProperty['createddate']) ));
                  ?>
                </td>
              </tr>
            <?php } ?>
          </table>

        </div><!-- List { -->
    </div>
  </div> <!--row-->

for_lease.vc.php (accessing the database and the class with the SQL)

<?php

    require_once($routePath . "_config/db.php");
    $dbConfig = new config_db();
    $db = $dbConfig->init();

    require_once($routePath . "_mc/Property.mc.php");
    $mcProperty = new Property_MC();

    $lstProperty = $mcProperty->SelectObj_ByLeaseId($db);
?>

Property.mc.php (contains the class with select SQL)

<?php

Class Property_MC {
    public function SelectObj_ByLeaseId($db) {
  $stmt = $db->prepare(
    " SELECT leaseid, lease_type, lease_name, lease_address, lease_price, lease_condition, lease_description, recstatus, createddate
      FROM for_lease"
  );

  $stmt->execute();
  $row = $stmt->fetch(PDO::FETCH_ASSOC);

  return $row;
}
}

?>

I checked that maybe somehow the output is read as a array, not a string hence the error, but I am not sure where in my code it is affected, or maybe theres something missing for the output to be read as a string.

Will appreciate any help, thank you.

1 Answer 1

2

As your method SelectObj_ByLeaseId returns only 1 property, the variable $lstProperty is an array of the fields associated with that property. So doing a foreach over that array is actually iterating over the individual values of the property (which are most likely strings) which is why you get the illegal offset.

Just remove the foreach loop.

Sign up to request clarification or add additional context in comments.

3 Comments

im trying to follow what you said, if i remove the foreach i cannot then use the Select SQL i have. also an error occurs if there i just remove it.
OK - so you want the SelectObj_ByLeaseId to return all the properties - then change fetch() to fetchAll() so it returns a list - and keep the foreach you have at the moment. Just as a comment though SelectObj_ByLeaseId would to me read as though it is meant to return 1 row, so perhaps change it to reflect it's true purpose.
wow what a quick fix, thanks for pointing out what i have overlooked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.