Load PHP page via database ID

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Load PHP page via database ID

Post by chris98 »

Hi, this is something which for some time I have been unsure of.How do you load a php page by an ID?

For example, test.php?id=1

or:

test.php?id=523

How is this possible?

I know that you need records in a database for this, and I have some already, and I will post the script below I am using for the databse.I just want to know how, so it would save me from having to constantly create new pages for a seperate ID.

Could someone please explain this to me, as I really don't know.

Code: Select all

CREATE TABLE `pages` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `username` varchar(30) default NULL,
  `content` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`)
);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Load PHP page via database ID

Post by Celauran »

Check for $_GET['id']. If it's present, escape it and use it to query your database.

Something like this:

Code: Select all

<?php

$sql = new PDO('mysql:host=localhost;dbname=whatever', 'username', 'password');

if (isset($_GET['id'])) {
    $query = "SELECT name, username, content FROM pages WHERE id = :id";
    $stmt = $sql->prepare($query);
    $stmt->execute(array(':id' => $_GET['id']));
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Load PHP page via database ID

Post by chris98 »

Would this code make a difference on different sql versions, or not?
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Load PHP page via database ID

Post by chris98 »

I am not using PDO (I don't think).
I think I am also getting confused myself, as I am VERY new to all this; two months ago, I didn't even know what sql was.

I have however, managed to get the ID from the URL, and used an echo to get it onto the page.But that still isn't really much of an improvement, as I need it to go into the database, and select the correct data.Could you help me with that if PDO isn't what I am using, and there is more versions?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Load PHP page via database ID

Post by Celauran »

If you aren't using MySQL, then simply adjust the DSN accordingly (eg. replace mysql: with, say, pgsql: or sqlite: etc.). Nothing else needs to be changed. That's the beauty of PDO. The example I gave above should work perfectly, just substitute your actual username and password for the placeholders. This will query the database and store the relevant record in the $result array, which you can then display on the page.

Code: Select all

<?php if (isset($result)): ?>
<div><?= htmlspecialchars($result['content']); ?></div>
<?php endif; ?>
Or something along those lines.
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Load PHP page via database ID

Post by chris98 »

It keeps coming up with the error:

Fatal error: Call to a member function prepare() on a non-object

What does that mean?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Load PHP page via database ID

Post by twinedev »

Most likely that the connection failed. Use something like the following to catch any connection errors:

Code: Select all

try {
    $sql = new PDO('mysql:host=localhost;dbname=whatever', 'username', 'password');
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Load PHP page via database ID

Post by Celauran »

Check phpinfo() to ensure that PDO is enabled. If not, complain to your hosting provider.
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Load PHP page via database ID

Post by chris98 »

No, PDO isn't enabled, apart from PDO-OCI.Would that do the same? I am using a localhost though, so would that matter? Would it be easier if I uploaded them to the server now?

The actual message is below:

"--without-pdo-dblib" "--without-pdo-mssql" "--with-pdo-oci

Though it doesn't appear to be a connection issue from using the code there.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Load PHP page via database ID

Post by twinedev »

if you can't do PDO, look into mysqli (note the *I* at the end, the ones without I are gone next version). It does allow prepared statements as well. If you are not going to use prepared statements, you need to make sure you use mysqli_real_escape_string to protect your script against SQL injection.

If easy for you to do though, I would say go for the PDO install if that is what your production environment uses.
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Load PHP page via database ID

Post by chris98 »

Would a server have it automatically installed?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Load PHP page via database ID

Post by Celauran »

That information should be available through phpinfo(). You clearly have the Oracle PDO driver installed but I haven't seen you mention what type of database you're using.
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Load PHP page via database ID

Post by chris98 »

Mysql standard I think.Either that or Mysql improved.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Load PHP page via database ID

Post by Celauran »

Check phpinfo() for --with-pdo-mysql. I'd be both surprised and disappointed if it isn't there.
User avatar
chris98
Forum Contributor
Posts: 103
Joined: Tue Jun 11, 2013 10:47 am
Location: England, United Kingdom

Re: Load PHP page via database ID

Post by chris98 »

No, it isn't there.The only one is:


"--without-pdo-mssql
Post Reply