1

I'm trying to do a loop in php where i have a variable "i" what's value is "1" and if in my database the value of my variable exist it will execute a script and add 1 to my variable and etc. to the time that the value of my variable does not exist in my database. There is my code :

config.php :

<?php
/* Database connection */

$sDbHost = 'localhost';
$sDbName = 'techtronik.pl';
$sDbUser = 'root';
$sDbPwd = '';
$dbcon = mysqli_connect ($sDbHost, $sDbUser, $sDbPwd, $sDbName);
?>

anotherfile.php :

<?php

include('config.php');

$sqlget = "SELECT * FROM pcstacjonarne";
$sqldata = mysqli_query($dbcon, $sqlget)or die("Nie udalo sie polaczyc z baza danych");
$row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC);
$i = 4;
$result = $dbcon->query("SELECT * FROM pcstacjonarne WHERE idpcstacjonarne = '$i'");

while ($result->num_rows == 1) {               
    echo 'Number of post is '.($i++);
}
?> 

But this code don't work i am trying to fix this for 3 hours but i really don't now where is the problem. If someone would help me it will be great.

6
  • Your while loop doesn't really make any sense in this context. Maybe you need some sort of recursive function here. Most likely it will be an infinite loop since if $result->num_rows is 1 then it will always remain 1. Commented Nov 17, 2016 at 22:54
  • That while loop doesn't make any sense in any context. If you only get 1 result from your query, you have an infinite loop (the value in $result->num_rows will never change). If you have more than 1 result, the while loop will never iterate at all. Commented Nov 17, 2016 at 22:59
  • You need to compare an actual column or something Commented Nov 17, 2016 at 22:59
  • Why don't you just get the value directly from the database? SELECT MAX(idpcstacjonarne) FROM pcstacjonarne Commented Nov 17, 2016 at 23:06
  • What are you using the first query result ($row) for? Commented Nov 17, 2016 at 23:12

2 Answers 2

1

This problem is solved more easily with a do while, rather than a while:

<?php

include('config.php');
$sqlget = "SELECT * FROM pcstacjonarne";
$sqldata = mysqli_query($dbcon, $sqlget)or die("Nie udalo sie polaczyc z baza danych");
$row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC);

$i = 4;

do {
    $result = $dbcon->query("SELECT * FROM pcstacjonarne WHERE idpcstacjonarne = '$i'");
    if ($result->num_rows === 1) {
        echo 'Number of post is '.($i++);
    }

} while ($result->num_rows === 1);
?>
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is that you are not making your loop properly. You are using num_rows, which is not what you want exactly. num_rows === 1 is probably never going to happen, so your loop is never happening (maybe only once). Tell us exactly what you want to do, and we can help you out a lot more.

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.