1

I have this sql table

username | coins| id | date
test     | 1    | 5  | 05-26-2020
test     | 1    | 4  | 05-26-2020
test     | 1    | 2  | 05-26-2020
test     | 1    | 5  | 05-26-2020
test     | 1    | 7  | 05-26-2020
test     | 1    | 5  | 05-26-2020
test     | 1    | 1  | 05-26-2020

and I have to do SELECT username FROM coins_logs WHERE id =5 and save the results in an array, so I'll can do echo $results[4] and print the 4th username

but how can I do it?

update

this is my code:

$idprodotto = '5';

$query = mysqli_query($con,"SELECT username FROM coins_logs WHERE   id= '$idprodotto';");

$utentiUltimi = mysqli_fetch_array($query);

echo $utentiUltimi[1];
echo $utentiUltimi[0];
echo $utentiUltimi[3];

I've tried to do print_r($utentiUltimi) and this is the result: Array( [0] => test ) so I think that don't take all the data

3 Answers 3

2

mysqli_fetch_array will only return the next result row or NULL if there is no more row.

So, you have to read it in a loop to get all usernames:

$usernames= array();
while( $row = mysqli_fetch_array($query) ) {
  $usernames[] = $row[0];
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use fetchAll() with PDO

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll(\PDO::FETCH_ASSOC);
print_r($result);

or mysqli_fetch_array

$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = $mysqli->query($query);

/* numeric array */
$row = $result->fetch_array(MYSQLI_NUM);

5 Comments

I've tried to do $results = mysqli_fetch_array($query) and echo $results[1]; echo $results[2]; echo $results[4]; but it print only one username
try with this mysqli_fetch_array($query, MYSQLI_NUM);
doesn't work, I've tried to do print_r($utentiUltimi) and this is the result: Array( [0] => test ) so I think that don't take all the data
Do you have all the results on $query ?
I don't know, how can I see it?
0

you can try this way.

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>

output

Fetch all of the remaining rows in the result set:
Array
(
    [0] => Array
        (
            [name] => apple
            [0] => apple
            [colour] => red
            [1] => red
        )

    [1] => Array
        (
            [name] => pear
            [0] => pear
            [colour] => green
            [1] => green
        )

    [2] => Array
        (
            [name] => watermelon
            [0] => watermelon
            [colour] => pink
            [1] => pink
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.