Jump to content

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


glassfish

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.