Jump to content

SQL Query to Select a Row By an ID Number "Out Of" a Column?


Recommended Posts

The Script:

<?php

    include("connect.php");

?>
<?php

    echo "<h1>The Hashtag: </h1>";
    if(isset($_GET['hashtag'])){
        echo $_GET['hashtag'];
    }


    // Select the ID of the given hashtag from the "hashtags" table.
    $tqs = "SELECT `id` FROM `hashtags` WHERE `hashtag` = '" . $_GET['hashtag'] . "'";
    $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
    $row = mysqli_fetch_assoc($tqr);

    // This prints e.g.:
    // 100
    echo "<br/><br/>";
    print_r($row['id']);

    // Select the threads from the "thread" table which contains the hashtag ID number.
    $tqs_two = "SELECT * FROM `thread` WHERE `hashtag_id` IN ('" . $row['id'] . "')";
    $tqr_two = mysqli_query($dbc, $tqs_two) or die(mysqli_error($dbc));
    $row_two = mysqli_fetch_assoc($tqr_two);

    echo "<br/><br/>";
    print_r($row_two);

?>

The script should select the rows by that ID number of the hashtag. It should look in the "hashtag_id" column of the table and see if that ID number can be found there, if it can be found there, then it should select that row.

 

The ID numbers are inside that "hashtag_id" column separated by commas.

 

Example:

98, 99, 100

My Question:

How to do the SQL query so it selects the rows by the hashtag ID number?

Edited by glassfish

It was precisely to avoid this problem when I advised you to normalize you data correctly here

http://forums.phpfreaks.com/topic/291641-store-the-id-number-inside-one-array-inside-for-loop/?do=findComment&comment=1493641

Edited by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.