1

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?

1
  • Remember to fetch the result ^^ Commented Feb 24, 2015 at 13:16

2 Answers 2

1

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']

Sign up to request clarification or add additional context in comments.

3 Comments

I used the fetch_assoc() but I didn't realize that I'll have to use $idUser['id'] instead of $idUser. Many thanks!
Actually, the fetch displays the good value of the id with an echo but I still got a 0 in my database...
You are selecting id from database, and your result id is in $idUser['id']. Please clarify what is missing?
0

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']

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.