PaulB3434 PaulB3434 - 6 months ago 6
SQL Question

PHP/SQL - Setting SQL Result to a list

I was wondering how to turn a SQL Query into a variable like this one:

$in = "Lambo 1; Trabant 2; Car 3;";

$result=mysqli_query($conn,$sql_query);


The query currently comes back with
Lambo 1; Trabant 2; Car 3;
but how would I change it into $in? As if I currently run it through another func I get
explode() expects parameter 2 to be string,


Full code:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$host = 'localhost';
$username = '';
$password = '';
$database = '';
$conn = mysqli_connect($host,$username,$password,$database);

$SteamID = "STEAM_0:0:81010017";
$sql_query="SELECT _Inventory FROM Players Where _SteamID='$SteamID'";

$result=mysqli_query($conn,$sql_query);


$in = $result;
foreach (explode(";", $in) as $element) {
$element = trim($element);
if (strpos($element, " ") !== false ) {
list($car, $number) = explode(" ", $element);
echo $car;
}
}


?>

Answer

change your $in variable to:

$in = mysqli_fetch_array($result)[0];
Comments