0

My code executes but only for the most recent row stored in the database. It doesn't check through the other rows and keeps executing on the same data in the recent row. What function am I missing?

Adding while(true) condition, which now loops but only loops the most recent.

$db = mysqli_connect("" , "", "") or die("Check connection parameters!"); 
// Optionally skip select_db and use: mysqli_connect(host,user,pass,dbname)  
mysqli_select_db($db,"ds_main") or die(mysqli_error($db));

if (mysqli_connect_error()) {
    die ('Failed to connect to MySQL');
} else {
    /*SUCCESS MSG*/
    echo '';
}


$sqlCommand = "SELECT companyname, domainurl, expirationdate FROM domains WHERE expirationdate BETWEEN CURDATE() AND CURDATE()+INTERVAL 30 DAY";

$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));

//fetch the data from the database 
$domainnames = "domainurl";
$domaindate = "expirationdate";
while ($row = mysqli_fetch_array($query)) {

  $domainnames = $row['domainurl'];  // list of expiring URLs
  $domaindate = $row['expirationdate'];  // list of expiry dates
  } // that's it for the loop

if (count($domainnames) > 0 ) { 
//carrys out a task

}

// Free the results  
mysqli_free_result($query);

//close the connection
mysqli_close($db);
}
?>

I expect the code to execute across all rows, not just the most recent. I'm new to php.

Mindin
  • 3
  • 1
  • 3
    Every time you run the loop, you re init the `$domainnames` and `$domaindate` variable, if you want to add your domain info to an array, change them to: `$domainnames[] = $row['domainurl'];` and `$domaindate[] = $row['expirationdate'];` – catcon Sep 29 '19 at 00:02
  • Please read: https://stackoverflow.com/questions/15318368/mysqli-or-die-does-it-have-to-die – Dharman Sep 29 '19 at 00:15

1 Answers1

1

Your code is "executing" on all rows, but on each row it's overwriting the values of $domainnames and $domaindate with the latest row's values. I suspect you are probably intending to build an array of domain names and dates, more like so:

$domainnames = array();  // list of expiring URLs
$domaindate = array();  // list of expiry dates
while ($row = mysqli_fetch_array($query)) {

  $domainnames[] = $row['domainurl']; // add to list
  $domaindate[] = $row['expirationdate']; // add to list
} // that's it for the loop
Patrick Fay
  • 552
  • 3
  • 15
  • Thank you for the assistance. However, I now receive an error "Notice: Array to string conversion" which is pointing to line 45 (not included in OP) it is `$ticket->Title = '[Expiring] - ' .$domainnames;` – Mindin Sep 29 '19 at 00:07
  • 1
    https://stackoverflow.com/questions/7490488/array-to-string-php - although I'm not sure your exact intent, are you expecting `$domainnames` to be a string? Or are you expecting to get a particular signular domain name out of it, instead of the whole array? – Patrick Fay Sep 29 '19 at 00:10