Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

MySQL ROW_NUMBER

This is an advanced problem which has several solutions, and I'm asking for the most difficult one possible.

In a PHP script, I've got some MySQL queries:
PHP:
<div class="php" id="{CB}" style="font-family: monospace;"><ol>[url=http://www.php.net/mysql_connect]mysql_connect[/url]($host, $user, $pass);

$result = [url=http://www.php.net/mysql_db_query]mysql_db_query[/url]($db, "SELECT * FROM table WHERE key='value'");

$array = [url=http://www.php.net/mysql_fetch_assoc]mysql_fetch_assoc[/url]($result);

[url=http://www.php.net/if]<span style="color: #b1b100;">if[/url] (/* For whatever reason I'm not going to display it right now */)

  // Display a form to view this later

 

Here's some background: the 'key' column isn't UNIQUE, and neither is any other column in the table. Currently, the HTML form contains information as to what this 'key' is, but that doesn't allow it to handle any duplicate keys.

The simplest solution is to ALTER TABLE and create a new AUTO_INCREMENT value and assign it as the PRIMARY KEY. However, I'm working with many databases, and all of my PHP thus far is designed to work with very few constraints, all of which are already a part of these databases; I could add this key to these databases, but requiring it would be bad in this situation, so I'm going for a better solution.

This so-called "better solution" of mine is to simply pass an index to the mysql_result() function, like so:
PHP:
<div class="php" id="{CB}" style="font-family: monospace;"><ol>$result = [url=http://www.php.net/mysql_query]mysql_query[/url]("SELECT column FROM table");

[url=http://www.php.net/echo]<span style="color: #b1b100;">echo[/url] [url=http://www.php.net/mysql_result]mysql_result[/url]($result, $index, 'column');
However, I'm at a loss for how to get this index.

What I want is the index according to the entire table, not the initial WHERE condition, which makes this far too complicated to be pretty. I am looking for a pretty solution, after all. I've looked around on the internet and discovered that there's a ROW_NUMBER function in SQL servers other than MySQL, but I would not consider myself an SQL scripter so I can't quite fathom the ways people have tried to emulate it, nor can I be sure these solutions would work from inside PHP.

I can get around the problem, but I want your ideas on my prettier solution.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top