Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Array Issue

Array Issue

Scheduled Pinned Locked Moved Database
helpdatabasedata-structurestutorialquestion
6 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MekaC
    wrote on last edited by
    #1

    Hey!!! Error: Fatal error: Uncaught Error: Call to a member function fetch_array() on bool Code: ```

    query("SELECT username FROM users where id =".$_GET['id']);
    foreach($username->fetch_array() as $k =>$v){
    $meta[$k] = $v;
    }
    }
    ?>

    Have been wrecking my brain for some days to figure out how to correct this. Can anyone help with rewriting this so it would work properly? Thank ya!!!

    Richard DeemingR L J 3 Replies Last reply
    0
    • M MekaC

      Hey!!! Error: Fatal error: Uncaught Error: Call to a member function fetch_array() on bool Code: ```

      query("SELECT username FROM users where id =".$_GET['id']);
      foreach($username->fetch_array() as $k =>$v){
      $meta[$k] = $v;
      }
      }
      ?>

      Have been wrecking my brain for some days to figure out how to correct this. Can anyone help with rewriting this so it would work properly? Thank ya!!!

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. PHP: SQL Injection - Manual[^] PHP: Prepared statements and stored procedures - Manual[^] Beyond that, the error suggests that your query didn't work. When the query fails, it returns false rather than a result set, as described in the documentation[^]. There are many possible reasons your query might fail. You will need to debug your code to find out what the problem is and fix it. We can't do that for you.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • M MekaC

        Hey!!! Error: Fatal error: Uncaught Error: Call to a member function fetch_array() on bool Code: ```

        query("SELECT username FROM users where id =".$_GET['id']);
        foreach($username->fetch_array() as $k =>$v){
        $meta[$k] = $v;
        }
        }
        ?>

        Have been wrecking my brain for some days to figure out how to correct this. Can anyone help with rewriting this so it would work properly? Thank ya!!!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        The query command is returning a boolean "false", to indicate no record was found. You need to test for that possibility before assuming that you have a valid record. See PHP: mysqli::query - Manual[^].

        M 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. PHP: SQL Injection - Manual[^] PHP: Prepared statements and stored procedures - Manual[^] Beyond that, the error suggests that your query didn't work. When the query fails, it returns false rather than a result set, as described in the documentation[^]. There are many possible reasons your query might fail. You will need to debug your code to find out what the problem is and fix it. We can't do that for you.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          M Offline
          M Offline
          MekaC
          wrote on last edited by
          #4

          Thank ya for the insight I greatly appreciate it ;)

          1 Reply Last reply
          0
          • L Lost User

            The query command is returning a boolean "false", to indicate no record was found. You need to test for that possibility before assuming that you have a valid record. See PHP: mysqli::query - Manual[^].

            M Offline
            M Offline
            MekaC
            wrote on last edited by
            #5

            Thank ya for the insight..I greatly appreciate it ;)

            1 Reply Last reply
            0
            • M MekaC

              Hey!!! Error: Fatal error: Uncaught Error: Call to a member function fetch_array() on bool Code: ```

              query("SELECT username FROM users where id =".$_GET['id']);
              foreach($username->fetch_array() as $k =>$v){
              $meta[$k] = $v;
              }
              }
              ?>

              Have been wrecking my brain for some days to figure out how to correct this. Can anyone help with rewriting this so it would work properly? Thank ya!!!

              J Offline
              J Offline
              jkirkerx
              wrote on last edited by
              #6

              Your pretty far off with your code, and I'm not sure which version of PHP your using. Plus your code is very primitive and not object oriented like for PHP versions 7 and up. You have to create a db connector as a class and call that db connector. Then create a query. Next run that query with the db connector which creates a result. The result can be false if nothing comes back, or can be a array of records in which you fetch them. Finally you call that array that was returned and convert them to rows. I packed the rows into an object that I created, and return the object instead of an array. The code your writing is very the year 2000, and is quite old and outdated. Nobody in western democracies are writing code like that anymore. PHP 7.4 example, what you should be learning. Hope that helps you, and PHP Storm by Jet Brains is the preferred editor or IDE for PHP.

              public static function getUsers(): Users {

                  $users = new Users();
              
                  $query = "
                  SELECT 
                         uid, 
                         Username               
                  FROM \[user\]
                  WHERE User\_type <> 'xxxxxxx' 
                  ORDER BY Username";
              
                  $conn = DbConnectRepository::createConn();
                  $result = sqlsrv\_query($conn, $query) or die(" getUsers " . \_\_LINE\_\_ . " - " . $query . ' - ' . print\_r(sqlsrv\_errors()));
                  while ($row = sqlsrv\_fetch\_array($result)) {
              
                      $user = new User();
                      $user->setUserId($row\[0\]);
                      $user->setUserName($row\[1\]);            
                      $users->add($user);
              
                  }
              
                  return $users;
              
              }
              

              If it ain't broke don't fix it Discover my world at jkirkerx.com

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups