ahbon
ahbon

Reputation: 512

store result of sql request in a variable php

With a POST request, I get the name of someone that I want to link with its id the mySQL database. For that, I create a query as you can see below and I need to put the result (a simple number) into a variable to use it for other queries.

I know that I could make joints with SQL but I'm trying to do it this way.

$owner = $_POST['owner'];

$queryUser = "SELECT id FROM collections WHERE name = '$owner'";
$idUser = $mysqli->query($queryUser) or die($mysqli->error.__LINE__);

With this code, I can't use $idUserbecause it appears as an 0, a 1 (recogniezd as an array?) or nothing when I try to write it somewhere in my database. I tried to use the intval() function with no results...

Any help?

Upvotes: 1

Views: 128

Answers (2)

kidA
kidA

Reputation: 1377

You can use the fetch_array method which fetches the result as an associative array

$query = $mysqli->query($queryUser) or die($mysqli->error.__LINE__);

$row = $query->fetch_array(MYSQLI_ASSOC);

echo $row['id']

Upvotes: 0

taliezin
taliezin

Reputation: 916

You have to fetch result from query:

$res = $mysqli->query($queryUser) or die($mysqli->error.__LINE__);
$idUser = $res->fetch_assoc();

so your id will be $idUser['id']

Upvotes: 1

Related Questions