I'm using the Medoo database Framework. The following query returns a PDO object.
$datas=$database->query('
SELECT lethal_servers.LETHAL_ServerName AS "lethal_servers LETHAL_ServerName",
lethal_servers.LETHAL_ServerID AS "lethal_servers LETHAL_ServerID",
lethal_servers.LETHAL_ServerPort AS "lethal_servers LETHAL_ServerPort",
lethal_user.LETHAL_UserName AS "lethal_user LETHAL_UserName"
FROM (
( lethal_servers AS lethal_servers
INNER JOIN lethal_server_user AS lethal_server_user
ON (lethal_server_user.LETHAL_ServerID = lethal_servers.LETHAL_ServerID )
)
INNER JOIN lethal_user AS lethal_user
ON (lethal_server_user.LETHAL_UserID = lethal_user.LETHAL_UserID )
)
WHERE (lethal_user.LETHAL_UserName = $lethal_UserName)
';
Using the following doesn't work since it takes $datas
as array.
foreach($datas as $data)
{
echo "servername:" . $data["LETHAL_ServerName"] . " port:" . $data["lethal_servers.LETHAL_ServerPort"] . "<br/>";
}
I'm struggling to find an answer anywhere on the web. Either it uses deprecated mysql_*
functions or does not suit my needs.
With the code above it will return:
Undefined variable: datas
Invalid argument supplied for foreach()
Could someone take a minute and explain to me how I'd go about this? Thanks.
My bet is that the query is failing, and PDO is returning
FALSE
.If we don't want to check for PDO errors, we can have PDO throw an exception for us when an error occurs, by setting an attribute:
Otherwise, you need to test
$datas
. If it's FALSE, then some kine of error has occurred. If your code was working, it would be vulnerable to SQL Injection. I don't know how you can stand working with SQL like that, with unnecessary parens and humongous table aliases. I prefer to use backticks to escape identifiers. (I believe double quotes will work to escape identifiers ifsql_mode
includesANSI_QUOTES
.)When you loop through the resultset, the column names are going to be what was specified as a column alias.
For example, this is wrong:
Because there is no column named
LETHAL_ServerName
returned by the query.The query assigned the column name as "
lethal_servers LETHAL_ServerName
". (It's not clear why you need to have the column named with that long name with a space in it; I'm going to assume that it has something to do with the Medoo framework, and leave the column name as is. We'd need to reference the contents of the returned column with a string that matches the assigned column name, e.g.NOTE: I don't have any experience with the Medoo framework. So I'm just guessing that $database is a reference to the PDO connection object.