I've been working on my first big website and encountered a problem while trying to create a PHP function that returns a value based on criteria given when the function is called.
I have been having a problem for using the following code, all I could get to return was "DATABASE: 1"
function data_r_user($request, $username ) {
include("login/dbconnect.php");
$sqli = "SELECT " . $request . " FROM users WHERE username = " . $username;
$result = mysqli_query($dbconnect,$sqli ) or die();
return $result;
}
After searching up for a while I appear to of tracked the problem to the fact that all SQL queries return an array and not a string (I believe this to be the case sorry if I'm wrong).
I thought returning a single result would be easy but it appears not to be.
So basically I want a way to return a single result from this function and I'm indifferent as to whether it's procedural or object.
$returnis not defined and your query is invalid and open to SQL injections. Strings need to be quoted. Once you get the query working use fetch$resultand set it to$return. Alsoor die()is kind of useless, output something there so you know why it died, or use the error reporting function so you get information.$sqliis still an invalid query so you will result in thedie. See the rest of the comment.mysqliyou should be using parameterized queries andbind_paramto add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put$_POSTor$_GETdata directly into a query, it can be very harmful if someone seeks to exploit your mistake.