I am trying to understand what the difference is between these two lines of code.
I have two pages ACR.php
and webPage1.php
. ACR.php
is included on any page I need to have a database connection. The goal is to obtain the value from the database and display it on the webpage.
// ACR.php //
<?php
// Est. Connection
$dbc = mysqli_connect($ACR_host, $ACR_user, $ACR_pass, $ACR_tablename)
or die('Error communicating to MySQL server');
// Select value from row in table
$thisPort = $dbc->query("SELECT activePort FROM table1 ")->fetch_row()[0];
// Select value from row in table
$thatPort = "SELECT activePort FROM table1";
// Displays result
$result1 = mysqli_query($dbc, $thisPort);
$result2 = mysqli_query($dbc, $thatPort);
?>
webpage1.php
will not display the correct value when echoing $result1
but rather echo 'result not found'
.
// webPage1.php //
<?php include 'ACR.php';?>
<!--html-->
<tr>
<th>Port<span id="portDisplay"></span><sup></sup>:</th>
<td id="showPort" style="text-align:left;width:75%;">
<?php
session_start();
if (mysqli_num_rows($result1) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result1)) {
echo " " . $row["activePort"];
// echo $row;
}
} else {
echo "result not found";
}
mysqli_close($dbc);
?>
</td>
</tr>
However, if I echo in $result2
, the correct value displays from the database table.
Why is this? What makes this so? Are not both $thisPort
and $thatPort
calling the same row in the table?
UPDATE: I use $thisPort
, in part, when fetching a local and remote address. So i would prefer using the current syntax for $thisPort
as it works but changing it to the syntax of $thatPort
crashes the page. Why is it the value can be pulled from the Database when using this method, but not when i need to display the port on the page.
I don't want to initiate an extra variable ($thatPort
), if I don't have to, to simply echo the value from the database when the original var ($thatPort
) should and is already doing this.