0

I am trying to fetch text from a specific column of a database (column is called "paste") relative to an url ID. Every column has an ID associated with it in a column called "id". As an example, I have a a data entry for the letter B in column "paste" with an ID of 7 in column "id".

So when the url is extended with "?id=7" the website is supposed to echo out the letter B in my textarea.

I've tried using mysql_fetch_array and echoing $row. Specifically I tried following the code from the "Getting a Row of Data using mysql_fetch_array" example on this link.

The sql query statement works, as I've tried it directly on my database via PHPMyAdmin. I've also verified that the ID in the url is able to be read by echoing $id.

<textarea type="text" name="paste" required autofocus rows="35" cols="150" wrap="off" maxlength="20000">
    <?php
        // Login information and database
        $servername = "xxx";
        $username = "xxx";
        $password = "xxx";
        $dbname = "xxx";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);

        // Check connection
        if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
        }

        $id = ($_GET['id']);

        if(isset($_GET['id']))
        {
            $query = "SELECT paste FROM pastebin WHERE id = $id";

            $result = $mysql_query($query) or die(mysql_eror());

            $row = mysql_fetch_array($result) or die(mysql_error());

            echo $row['paste'];

            $conn->close();
        }

    ?>
</textarea><br>
  • It's not related to the problem, but you should have the `$id = $_GET['id']` assignment in the `if` block. Otherwise you'll try to access an undefined index. – Barmar Nov 05 '16 at 20:19
  • Your code is vulnerable to SQL injection attack – mike510a Nov 05 '16 at 20:20
  • You're connecting with `mysqli`, but querying with `mysql`. You need to use `mysqli_query`, `mysqli_fetch_array`, and `mysqli_error`. You should also learn to use prepared queries when using mysqli. – Barmar Nov 05 '16 at 20:20
  • @Barmar You failed to signal the `$` in `$mysql_query` which is another fail in their syntax and `mysql_eror()` another typo. If I do recall from not long ago, you reopened a question I closed because of additional errors. – Funk Forty Niner Nov 06 '16 at 00:16
  • @Fred-ii- Not going to bother when they're just typos, since we'd just close it with the typo close reason. – Barmar Nov 06 '16 at 00:26

0 Answers0