Array Issue
-
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!!!
-
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!!!
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
-
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!!!
-
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
-
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[^].
-
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!!!
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 thatquery
with the db connector which creates aresult
. Theresult
can be false if nothing comes back, or can be a array of records in which you fetch them. Finally you call thatarray
that was returned and convert them torows
. 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