Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times

    another php error. probably syntax

    im trying to iterate through an array with a foreach loop but i cant get it to echo out anything other than an error

    PHP Code:
    <?php

      
    include 'view/header.php';
      include 
    'includes/connect.php';

      
    $db = new database();

      
    $sql "SELECT `first_name`, `last_name` FROM `pratts_db`
              WHERE `first_name` = 'clive';"
    ;

      
    $result $db->select($sql);

      if (
    sizeof($result) >= 1){
        echo 
    "query successful";
          foreach(
    $result as $value){
            echo 
    "<p>{$value[0]['first_name']} </p>";
          }
      }

      include 
    'view/footer.php';

    ?>
    this is the error

    Warning: Invalid argument supplied for foreach() in C:\wamp64\www\final\index.php on line 15

    it echo's query successful so there is a row there.

    seemingly according to the error there is an invalid arguement, i dont know how that could be and another thing is i dont think im selecting the array correctly, i have tried the following ways

    {$value[first_name]} (tried this with double quotes, single quotes and backticks
    {$value[0][first_name]} also tried this with different quotes but still not working

    i also put the code into phpstorm and it didnt show anything

    SOMEONE HELLLPPPPPPP

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    at a couple lines to test $result ...

    You will see if $result is an array or not.

    $result = $db->select($sql);
    print_r($result);
    exit;




  4. #3
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times
    it didnt print anything. so that means the result isnt an array even though i used fetch_assoc and the sizeof query returned successful. whats the problem?

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    Repost the class script again.
    I assume things are different now than the previous post on the other thread.

    Try an experiment ... to simplify the result so it is something we can see (a JSON array).

    Add this function to your class:
    Code:
    public function myTest($query){
    $connection = $this->connect(); 
    $result = $connection->query($query); 
    return json_encode($result->fetchAll(PDO::FETCH_ASSOC), JSON_NUMERIC_CHECK);
    }
    Run this simple PHP script ... you can name it "test.php" if you want:
    Code:
    <?php
      include 'view/header.php';
      include 'includes/connect.php';
    
      $db = new database();
    
      $sql = "SELECT `first_name`, `last_name` FROM `pratts_db`
              WHERE `first_name` = 'clive';";
    
    $result = $db->myTest($sql); 
    echo $result;
    ?>

    See if you get a JSON array.
    Make sure you know you'll get at least 1 row from the query (since it is not checking for it).

    I'm just winging it, since I can't test anything myself.
    I like working with JSON arrays because PHP likes JSON and works well with it.

    EDIT:
    I guess I always use PDO ... don't know anything about what you're using.
    So maybe nothing I post here will work anyhow. Maybe your WAMP won't even do PDO?
    Last edited by mlseim; Apr 19th, 2018 at 03:19 PM.


  6. #5
    Senior Member
    Join Date
    Apr 2016
    Posts
    717
    Member #
    53891
    Liked
    11 times
    I'll try what you posted. I can't post any code I'm on my phone but if I can't get results by midday tomorrow I'm just gonna cut the whole class and go back to basics. Thanks dude

  7. #6
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    I agree. I use this CRUD class and it's easy. I'm not sure what you're doing.

    PHP PDO CRUD Tutorial using OOP with Bootstrap | Coding Cage



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 02:54 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com