PaulB3434
PaulB3434

Reputation: 27

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 getexplode() 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;
        }
}


?>

Upvotes: 0

Views: 38

Answers (2)

Mantis Support
Mantis Support

Reputation: 344

mysqli_query returns false or a mysqli_result object.

You need to add something to actually get the results - either by calling fetch_object, fetch_array, fetch_assoc as shown below:

while ($row = $result->fetch_array()){
    $group_arr[] = $row;
}

Upvotes: 1

Mumen Yassin
Mumen Yassin

Reputation: 512

change your $in variable to:

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

Upvotes: 0

Related Questions