0

The Problem

I am really struggling to get wordpress to generate a page dynamically based on the content of a database.

The Context

I have a sql database which contains unique slugs and Ids, and I am able to make a page on wp which pulls the data in from the sql database and presents it in a table of the items.

The bit I cannot work out how to do is generate a unique page based on the slug (or even the Id) for each table row.

My Code

This is the code on the index page of the devices, and it works fine; it displays what I need, each href is the slug from the database.

I am using a custom template php file for the specific wordpress pages in question.

page-206.php - this is for the 'index' page


<tbody style=>
          <?php foreach ($devices AS $device): 
                    //$newUrl = "http://example.com$device[slug]";
                    $newUrl = "page-device-repair-service.php?id=$device[slug]";

                    ?>
          <tr style=>
            <td style=>
              <a style=href="<?=$newUrl?>">
                <?php echo "$device[deviceName]" ?>
              </a>

            </td>
          </tr>
          <?php endforeach; ?>
        </tbody>

page-214.php - this is for the 'results' page

if (isset($_GET['id'])) 
            {
                $slug = $_GET ['id'];
            }
            else
            {
                $slug = "";
            }
            include 'sql_connect.php'; //db connection
            $connection = new PDO ($dsn, $username, $password);
            $connection -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo "connection successful";
            $sql = "SELECT * 
                FROM newdevices
                WHERE slug = $slug"; 
            $url = "http://18.168.90.222$slug";

The results

The result I am getting is the 'page not found', so something is missing, I just don't know enough to know what.

Further

Do I have to insert the php code into the page directly, rather than use a template? If so, how do I go about doing that on wordpress? https://stackoverflow.com/q/57643520/18227124 say they used Advanced Ads plugin, but I have looked and I am not sure thats what I want. I have tried header and footer insert but I don't want the php running on every page on the site, just this 'results' page.

Extra

Just in case its needed, my sql_connect.php looks like this

<?php

            $host = "localhost";
            $dbName = "myDatabase";
            $username = "root";
            $password = "xxxxxx";
            $dsn = "mysql:host=$host;dbname=$dbName";
            //specify if local or production
            //$liveUrl = "http://.....$device[slug]";
            //$newUrl = "http://18.168.90.222$device[slug]";
            //$localUrl="http://localhost$device[slug]";
            
            $devices = "";
            try {
                $connection = new PDO($dsn, $username, $password);
                $connection -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                echo "connection succesful";
                $sql = "SELECT * FROM newdevices";
                $devices = $connection -> query ($sql);
                
            } catch (PDOException $error){
                echo $error -> getMessage();

            }
            ?>

The comments are for when I'm testing on my local or my production site.

Please excuse me if I am missing something glaringly obvious, I am relatively new to PHP, SQL and PHP on wordpress.

Edit

I know there are similar questions on SO and SE, I have been searching, reading and trying to replicate various answers into my own code but nothing I do is working!

2
  • It's difficult to ascertain what you're attempting to accomplish here... Generally, I'd recommend just importing the contents of your other database into WordPress posts and letting WordPress manage the content. If that's not an option, then I guess you're trying to display the content from the other database as though it were WordPress content? Do you expect WordPress to provide archives and search functionality as it does for posts, or do you just want one route with some query parameters to display database rows within the theme template?
    – bosco
    Commented Jul 17, 2022 at 3:58
  • 1
    Its been so long since I posted this, and with no answers I completely forgot but I have managed to fix it myself, Ill post the answer and you'll see what I achieved.
    – Yenmangu
    Commented Jul 20, 2022 at 10:57

1 Answer 1

1

I managed to fix this myself with the following



<?php
    

    if (isset($_GET['id'])) 
    {
        $id = $_GET ['id'];
    }
    else
    {
        $id = "";
    }
    
    //$url = "http://18.168.90.222/device-repair-service/device?id=$id";
    
    require 'sql_connect.php';

    try {
        $connection = new PDO($dsn, $username, $password);
        $connection -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        //echo "connection succesful";
        } catch (PDOException $error){
        echo $error -> getMessage();
        }

    //prevent SQL injection
    $sql = "SELECT * FROM newdevices WHERE deviceId =?";

    $result = $connection -> prepare($sql);

    $result -> bindParam(1, $id);
    $result -> execute();

    $row = $result -> fetch()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.