-1

I have the next problem:

My code is this one:

$id=$_SESSION["id"];
$query="select id_period from periods where id_group=$id and end=NULL";
$result=mysqli_query($conn,$query);

if(mysqli_num_rows($result)==0){
$insert=mysqli_query($conn,"insert into periods(id_group) values($id)");
}

while($row=mysqli_fetch_row($result)){
   $period=$row['id_period'];
}

$query="insert into times(id_period,id_group) values($period,$id)";
echo $query;
$insert=mysqli_query($conn,$query);

This code should search if exists a 'period' with field 'end'=NULL. If not, then insert a new one. That works fine. However, the next part doesn't working. The insert into 'times' query needs the id_period, given by the var $period, but the WHILE never runs.

Any idea? Thank you.

2
  • WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST, $_GET or any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented Jun 3, 2017 at 15:33
  • Possible duplicate of Comparing with NULL values Commented Jun 3, 2017 at 15:36

1 Answer 1

0

I find problem in below while code

while($row=mysqli_fetch_row($result)) {
   $period=$row['id_period'];
}

In this for fetching data from result set you have use mysqli_fetch_row as per php documentation http://php.net/manual/en/mysqli-result.fetch-row.php mysqli_fetch_row — Get a result row as an enumerated array.

If you are interested in getting data using name as an index then please use http://php.net/manual/en/mysqli-result.fetch-assoc.php it mysqli_fetch_assoc — Fetch a result row as an associative array.

Hopes it helps you.

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

1 Comment

Working! I had already trying to use fetch_assoc, but surely I had another problem in that moment, so I discarded that. Thank you a lot!

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.