简体   繁体   中英

PHP pulls URL from MySQLi data table

I'm pulling a 'name' and 'url' from the database (this works) but pinging the pulled 'url' doesn't work unfortunately.

<tbody>

<?php
require_once "config/config.php";

$sql = "SELECT * FROM deployments";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<tr>";
echo "<th>Deployment</th>";
echo "<th>URL</th>";
echo "<th>Status</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['url'] . "</td>";
if($socket =@ fsockopen($sql = "SELECT url FROM deployments", 80, $errno, $errstr)) {

echo "<td><span class='badge badge-success'>LIVE</span></td>";
fclose($socket);

}
else {

echo "<td><span class='badge badge-danger'>DOWN</span></td>";

}
echo "</tr>";
}
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>

</tbody>

The badge bit should show 'LIVE' if ping is successful but 'DOWN' if no ping could be done.

I see what you are trying to do but you're just entering a string into the fsockopen function, you are not actually executing the SQL within that string. You are already running the loop of the result of that same query, so just pick the URL value from the row and insert it in.

Change this:

if($socket =@ fsockopen($sql = "SELECT url FROM deployments", 80, $errno, $errstr)) {

echo "<td><span class='badge badge-success'>LIVE</span></td>";
fclose($socket);

}

To this:

$url = $row['url'];
if($socket =@ fsockopen("'$url'", 80, $errno, $errstr)) {

echo "<td><span class='badge badge-success'>LIVE</span></td>";
fclose($socket);

}

Alternatively:

$url = "'" . $row['url'] ."'";     
if($socket =@ fsockopen($url, 80, $errno, $errstr)) {

echo "<td><span class='badge badge-success'>LIVE</span></td>";
fclose($socket);

}

I'm pulling a 'name' and 'url' from the database (this works) but pinging the pulled 'url' doesn't work unfortunately.

That is not actually what this code is doing:

$socket =@ fsockopen($sql = "SELECT url FROM deployments", 80, $errno, $errstr)
  • This assigns to the $socket variable the result of the fsockopen call.
  • Sadly, someone put an @ before the call, silencing the error that is thrown because
  • $sql = "SELECT url FROM deployments" is not doing what you think it does. That statement assigns the string SELECT url FROM deployments to the variable $sql . That is all. Using it in a function call actually passes the value of the assignment (a true boolean in this case) to the function.
  • You almost assuredly want to pass $row['url'] to the function.

Let me make this clear: never ever use @ in your code. In theory, one could make a case for silencing errors, but in practice, I've never seen a single case where this was not simply the programmer being lazy, and/or not understanding what the code does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM