Jump to content

Mark1

New Members
  • Posts

    2
  • Joined

  • Last visited

Mark1's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Let me rephrase and simplify my question. I have a database `mydb` with the table `metatags` having 3 columns: `page_id`, `page_url`, `page_title`. When the pages are created dynamically-I'm using `page_id` as a base to supply different data, values from different rows of database to different webpages. $page_id = filter_input(INPUT_GET, 'page_id', FILTER_VALIDATE_INT); if (!$page_id) { include '../errors/page-not-found.php'; } $sql = "SELECT page_id, page_title FROM metatags WHERE page_id=:page_id;"; $metatags= pdo($pdo, $sql, [$page_id])->fetch(); if (!$metatags) { include '../errors/page-not-found.php'; } But, for static pages I have to use URL (I think) as a base, to make database to know which row of data to supply to the open webpage. And that's my problem: how to query those rows based on url. I'm started with connection: <?php try { $pdo = new PDO('mysql:host=mysql;dbname=mydb;charset=utf8mb4', 'myuser', 'mypassword'); } catch (PDOException $e) { $output = 'Unable to connect to the database server: ' . $e->getMessage(); } After connection script I need to rewrite my old script using obsolete mysqli_query (), mysqli_num_rows() and mysqli_fetch_assoc () in PDO format; also based on url, and I don't know how to do it. <?php $url = isset( $_SERVER[ 'REQUEST_URI' ] ) ? basename( $_SERVER[ 'REQUEST_URI' ] ) : false; $querysl="SELECT * from metatags where page_url='$url'"; $queryres=mysqli_query($myConnection,$querysl); $pagerow=mysqli_num_rows($queryres); $pagedata=mysqli_fetch_assoc($queryres); And of course display on the page. <?php echo $page_title; ?>
  2. I have this code, which supply my pages with metadata based on url: <?php $url = isset( $_SERVER[ 'REQUEST_URI' ] ) ? basename( $_SERVER[ 'REQUEST_URI' ] ) : false; $querysl="SELECT * from pslpages where page_url='$url'"; $queryres=mysqli_query($myConnection,$querysl); $pagerow=mysqli_num_rows($queryres); $pagedata=mysqli_fetch_assoc($queryres); $page_title=''; $page_description=''; $page_keywords=''; $page_canonical_url=''; $page_body=''; if($pagerow>0) { $page_title=$pagedata['page_title']; $page_description=$pagedata['page_description']; $page_keywords=$pagedata['page_keywords']; $page_canonical_url=$pagedata['page_canonical_url']; $page_body=$pagedata['page_body']; } else{ $page_title='Mark - '; $page_description='Mark - '; $page_keywords='Mark - '; $page_canonical_url=''; $page_body=''; } ?> <!DOCTYPE html> <html lang="pl"> <head> <title><?php echo $page_title;?></title> <meta charset="utf-8"> <meta name="description" content="<?php echo $page_description;?>"> <meta name="keywords" content="<?php echo $page_keywords;?>"> <meta name="author" content="Mark"> <link rel="canonical" href="<?php echo $page_canonical_url;?>"> ...which work so far fine, but I need to upgrade it to pdo (connection and querying). How to do it?
×
×
  • 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.