Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Finally found a way to do this and it has to be done with multiple queries.</p> <p>The tables are set up so that when you enter a new YouTube video reference into <strong>vids</strong> table it gets an autoincrement <strong>vid_id</strong>. The junction table, <strong>list_vid_junc</strong>, is MyISAM and has 3 columns: <strong>list_id</strong>, <strong>vid_id</strong> and <strong>sort</strong>. The primary key is made up of <strong>list_id</strong> and <strong>sort</strong>, with <strong>sort</strong> set to autoincrement. This allows multiple entries of the same video in a list, which can be sorted by the <strong>sort</strong> autoincrement.</p> <p>Note that in a MyISAM table because the primary key index is split this way, mySql starts the <strong>sort</strong> autoincrement from 1 for each new list that is created rather than making every sort a unique number, see <a href="http://dev.mysql.com/doc/refman/5.5/en/example-auto-increment.html#id583323" rel="nofollow">http://dev.mysql.com/doc/refman/5.5/en/example-auto-increment.html#id583323</a>. List 1 can contain sort ids of 1,2,3 and list 2 can also have sort ids of 1,2,3. The pairs are unique not the individual numbers.</p> <p>This is not a production version, just the basic solution:</p> <pre><code>class DbService { var $username = "user"; var $password = "password"; var $server = "localhost"; var $port = "3306"; var $databasename = "database"; var $connection; public function __construct() { $this-&gt;connection = mysqli_connect( $this-&gt;server, $this-&gt;username, $this-&gt;password, $this-&gt;databasename, $this-&gt;port ); $this-&gt;throwExceptionOnError($this-&gt;connection); } // returns array of (vidID, sort); adds duplicate vids to list if run 2+ times public function addToPlaylist($ytRef, $listID) { $stmt = mysqli_prepare($this-&gt;connection, "INSERT IGNORE INTO vids(yt_ref) VALUES (?)"); mysqli_bind_param($stmt, 's', $ytRef); mysqli_stmt_execute($stmt); $vidID = mysqli_stmt_insert_id($stmt); // 0 if video already exists mysqli_stmt_free_result($stmt); if ($vidID == 0) { $stmt = mysqli_prepare($this-&gt;connection, "SELECT vid_id FROM vids WHERE yt_ref = ? LIMIT 1"); mysqli_bind_param($stmt, 's', $ytRef); mysqli_stmt_execute($stmt); mysqli_stmt_bind_result($stmt, $vidID); mysqli_stmt_fetch($stmt); mysqli_stmt_free_result($stmt); } $stmt = mysqli_prepare($this-&gt;connection, "INSERT IGNORE INTO fr_list_vid_junc(vid_id, list_id) VALUES(?, ?)"); mysqli_stmt_bind_param($stmt, 'ii', $vidID, $listID); mysqli_stmt_execute($stmt); mysqli_stmt_free_result($stmt); mysqli_close($this-&gt;connection); $arr = array($vidID, mysqli_stmt_insert_id($stmt)); return $arr; } } </code></pre> <p>I am sure there are many ways this can be improved so any suggestions would be welcome.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload