PHP Query a table column based on value from another column in same table

I have a database I have to query using PHP. There are 3 different columns I'm dealing with. What I have to do require a handful of steps so I'll list the steps 1 by 1...... 1) I have to go through each value in the 'nid' column. However, some of the 'nids' have duplicate value. Therefore I have to choose the 'nid' with the highest 'vid' value. 2) Once I select the 'nid' with the highest 'vid' value I then have to get the value of the 'title' column that's in the same row of the highest 'vid'. For example if I have a 'vid' of 1253 I have to select the content that's in column title that corresponds with 'vid' 1253. I have a lot of the steps. However, I'm getting stuck on once I grab the highest vid, being able to grab the content in the title column. Below is my code

<?php
    // Establish all database credential variables
    $serverName = "localhost";
    $username = "root";
    $password = "root";
    $databaseName = "redesign_static";
    // Create Database Connection
    $connection = new mysqli($serverName, $username, $password, $databaseName);
    // Check Database Connection
    if ($connection->connect_error) {
      die("Connection failed:" . $connection->connect_error);
    } // line ends if statement
    $queryNodeRevision = "SELECT nid, vid, title FROM node_revision";
    // line above creates variable $queryNodeRevision > selects column "nid" from table "node_revision"
    $results = mysqli_query($connection, $queryNodeRevision) or die("Bad Query: $results");
    // line above creates variable $results > actually queries that database and passes in variable "$queryNodeRevision"
    $storeNIDAndVIDValues = []; // empty array to store max 'vid' values
for ($i = 0; $i &lt; 8000; $i++) {
  $storeNIDAndVIDValues[$i] = 0;
  // line above assigns initial 'vid'; starts at 0
}

while ($row = mysqli_fetch_array($results)) {
  $currentNID = $row['nid'];
  // line above creates variable that represents the current 'nid' of row (aka the key)
  $currentVID = $row['vid'];
  // line above creates variable that represents the current value of the 'vid' (the number you want to compare)
if ($currentVID &gt; $storeNIDAndVIDValues[$currentNID]) {
    // if the value of'$currentVID' is greater than what's stored in array '$storeNIDAndVIDValues' at current nid position
    // $storeNIDAndVIDValues[$currentNID] = goes into array $storeNIDAndVIDValues and gets the value of nid key (in this case value represents what position the nid is at)
       $storeNIDAndVIDValues[$currentNID] = $currentVID;
       // line above &gt; becomes max 'vid' at that time 
       $titleOfSelectedVID = $row['title'];
      // $row['title'] = gets the value of the current 'title'
    $queryTitle = "SELECT title FROM node_revision WHERE $currentVID ";
    // line above is query variable that targets 'title' column row that has highest 'vid' value
 } // line ends if statement
} // line closes while loop

?>

$queryTitle = "SELECT title FROM node_revision WHERE

The $queryTitle line is where I’m getting stuck. This is where I want to get the content of title column but only the title that corresponds with the highest vid.

#php #mysql #sql #database

1 Likes2.20 GEEK