AutoPlay5
AutoPlay5

Reputation: 45

PHP if statements being ignored

I'm new to PHP and SQL. I'm trying to make a rule so that it will only show certain information for certain pages. The code I'm using is include 'dbh-login.php';

$id = $_GET['id'];

$i = 1;

while ($i != 100) {
    $sql = "SELECT * FROM ui_off WHERE id='$i'";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    if ($row['link'] = $id) {
      echo $row['title']."<br>";
    }
    $i++;
 }

The if statement seems to have no effect on weather the script echoes the title or not.

Upvotes: 0

Views: 185

Answers (2)

Mario Rawady
Mario Rawady

Reputation: 871

Your code does not make any sense.

You are using a while loop and looping in it 100 times just to check if 1 row have the given id.

Why don't you search directly for the id? Your code will be cleaner and you will free some memory on the server by deducting 100 queries each time the page is opened.

$id = $_GET['id'];

$sql = "SELECT * FROM ui_off WHERE id!='100' AND link='$id'" ;
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row['link'] != '') {
  echo $row['title']."<br>";
}

Upvotes: 0

Srikanta Gouda
Srikanta Gouda

Reputation: 521

You are missing == assignment. Here is the working code.

$id = $_GET['id'];

$i = 1;

while ($i != 100) {
    $sql = "SELECT * FROM ui_off WHERE id='$i'";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    if ($row['link'] == $id) {
      echo $row['title']."<br>";
    }
    $i++;
 }

Upvotes: 1

Related Questions