Page 1 of 2
Load PHP page via database ID
Posted: Sun Aug 11, 2013 10:40 am
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`)
);
Re: Load PHP page via database ID
Posted: Sun Aug 11, 2013 11:03 am
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);
}
Re: Load PHP page via database ID
Posted: Sun Aug 11, 2013 11:22 am
by chris98
Would this code make a difference on different sql versions, or not?
Re: Load PHP page via database ID
Posted: Sun Aug 11, 2013 12:50 pm
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?
Re: Load PHP page via database ID
Posted: Sun Aug 11, 2013 1:25 pm
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.
Re: Load PHP page via database ID
Posted: Sun Aug 11, 2013 2:05 pm
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?
Re: Load PHP page via database ID
Posted: Sun Aug 11, 2013 3:56 pm
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();
}
Re: Load PHP page via database ID
Posted: Sun Aug 11, 2013 4:09 pm
by Celauran
Check phpinfo() to ensure that PDO is enabled. If not, complain to your hosting provider.
Re: Load PHP page via database ID
Posted: Mon Aug 12, 2013 2:49 am
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.
Re: Load PHP page via database ID
Posted: Mon Aug 12, 2013 10:11 pm
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.
Re: Load PHP page via database ID
Posted: Wed Aug 14, 2013 6:17 am
by chris98
Would a server have it automatically installed?
Re: Load PHP page via database ID
Posted: Wed Aug 14, 2013 8:43 am
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.
Re: Load PHP page via database ID
Posted: Wed Aug 14, 2013 11:36 am
by chris98
Mysql standard I think.Either that or Mysql improved.
Re: Load PHP page via database ID
Posted: Wed Aug 14, 2013 12:57 pm
by Celauran
Check phpinfo() for --with-pdo-mysql. I'd be both surprised and disappointed if it isn't there.
Re: Load PHP page via database ID
Posted: Sat Aug 24, 2013 3:04 am
by chris98
No, it isn't there.The only one is:
"--without-pdo-mssql