0

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 "&nbsp;&nbsp;&nbsp;" . $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.

JustADrone
  • 18
  • 3
  • 1
    On a side note, `session_start();` should be called BEFORE you echo (send) anything or you will get errors about headers. see http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Louis Loudog Trottier May 08 '17 at 15:48
  • am I not already calling `session_start()` before I echo? (See `webPage1.php`) or are you referring to `ACR.php`? I am not echoing anything on `ACR.php`. – JustADrone May 08 '17 at 16:19
  • You are sending html code (table row and cell). If you echo, print, send or modify headers before you call session_start it will fail. Was just side note, you can ignore and wait until you hit the problem or you can argue with me. – Louis Loudog Trottier May 08 '17 at 19:20
  • Not an argument, just a question. I was unsure of what you were referring to. Are you saying I should just start the session call at the top of the page and close at the bottom? I have plenty of `sessionstart();`'s on the page and would it be best to just use one and be done? – JustADrone May 08 '17 at 19:27
  • Yes you could put all this in a file and do an include whenever you need a session. (just like you do for your DB). If you take a look at the link you will understand all the tricky part of headers and session. Whenever you feel like a break and read something. Keep it up. – Louis Loudog Trottier May 08 '17 at 20:05

1 Answers1

0

$thisPort already contains a row from the database. This is not a valid argument to supply to mysqli_query. If you look at your php error log, you'll probably see (depending on your log settings) that the line $result1 = mysqli_query($dbc, $thisPort); generates an error message; $result1 will actually contain FALSE.

$thatPort contains a SQL query string, which is a valid argument to the mysqli_query function, and creates a valid result set in $result2.

Brian A. Henning
  • 1,374
  • 9
  • 24