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

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

<a href="https://i.stack.imgur.com/NNyIW.png" target="_blank"><img src="https://i.stack.imgur.com/NNyIW.png"></a>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

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

Best MySQL DigitalOcean Performance – ScaleGrid vs. DigitalOcean Managed Databases

Compare ScaleGrid MySQL vs. DigitalOcean Managed Databases - See which offers the best MySQL throughput, latency, and pricing on DigitalOcean across workloads.

Introduction to Structured Query Language SQL pdf

SQL stands for Structured Query Language. SQL is a scripting language expected to store, control, and inquiry information put away in social databases. The main manifestation of SQL showed up in 1974, when a gathering in IBM built up the principal model of a social database. The primary business social database was discharged by Relational Software later turning out to be Oracle.

PHP Code for Update Data in MySQL Database - Tuts Make

PHP code for updating data in mysql database. Here we'll show you how to fetch and update form data into mysql database using PHP code. &demo

Country State City Dropdown list in PHP MySQL PHP

Country state city dropdown using ajax in php. You'll learn how to populate country city state dropdown based on previous selection in php using ajax