6 replies [Last post]
Kevstar
Kevstar's picture
Offline
Enthusiast
England
Last seen: 4 years 4 weeks ago
England
Timezone: GMT+1
Joined: 2012-07-20
Posts: 81
Points: 107

I am trying to add a database to my site using php.

I have followed tutorials and have had no luck with it. No matter what I do it does not sort the problem.

I have 3 pages to test this out.

index.php

    <?php
 
include_once('test.php');
include_once('article.php');
 
$article = new Article;
$articles = $query->fetchAll();
 
print_r($articles);
 
?>
<html>
    <head>
       <title>Testing Testing 1,2,3</title>
    </head>
        <body>
 
<ol>
<?php foreach ($articles as $article) { ?>
<li>
<a href="article.php?id=<?php echo $article['article_id'];?>
<?php echo $article['$article_title']?></a>
 
posted <?php echo date('l JS', $article['article_timestamp']); ?>
 
</li>
 
</ol>
        </body>
</html>

article.php

<?php
 
class article {
public function fetch_all(){
    global $pdo;
      $query = $pdo->prepare("SELECT * FROM articles");
      $query->excute();
return $query->fetchAll();
              }
 
}
 
?>

and test.php

<?php
 
try {
$PDO = new PDO('mysql:host=localhost:dbname=****DBNAME****', '****DBUSER****', '****DBPASS****');
} catch (PDOException $e) {
exit('database error.');
}
 
?>

** replaces private info that has been double checked and are correct as to myphpadmin and my mysql.

the trouble is it does not seem to work. although it works well on the tutorials. I just get the database error as featured in the 3rd code. Please can someone help me.

thank you.

Tags:
gary.turner
gary.turner's picture
Offline
Moderator
Dallas
Last seen: 13 hours 50 min ago
Dallas
Timezone: GMT-5
Joined: 2004-06-25
Posts: 9743
Points: 3822

HTML error

It may or may not be the issue, but it seems you have not closed the quote on the link's href attribute value.

<a href="article.php?id=<?php echo $article['article_id'];?>
<?php echo $article['$article_title']?></a>
 
posted <?php echo date('l JS', $article['article_timestamp']); ?>
 
</li>
 
</ol>
        </body>
</html>

I am not a fan of integrating the structural markup into the logic, so I may be misreading something.

cheers,

gary

If your web page is as clever as you can make it, it's probably too clever for you to debug or maintain.

Kevstar
Kevstar's picture
Offline
Enthusiast
England
Last seen: 4 years 4 weeks ago
England
Timezone: GMT+1
Joined: 2012-07-20
Posts: 81
Points: 107

Even without any html and

Even without any html and just a call to it still does not work.

roytheboy
roytheboy's picture
Offline
Guru
North Wales, UK
Last seen: 6 years 8 weeks ago
North Wales, UK
Timezone: GMT+1
Joined: 2004-09-18
Posts: 2233
Points: 41

In test.php you have captured

In test.php you have captured the exception (the problem) but you are not doing anything with it. You also need to use the same case for $pdo. Try this and if it doesn't work, you should see exactly what the problem is...

<?php
 
try {
     $pdo = new PDO('mysql:host=localhost:dbname=****DBNAME****', '****DBUSER****', '****DBPASS****');
} catch (PDOException $e) {
     echo $e->getMessage();
}
 
?>

May I suggest that you do a bit of reading > http://uk1.php.net/manual/en/class.pdo.php and > http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers

Life's a b*tch and then you die!

Kevstar
Kevstar's picture
Offline
Enthusiast
England
Last seen: 4 years 4 weeks ago
England
Timezone: GMT+1
Joined: 2012-07-20
Posts: 81
Points: 107

Hi Roy. thank you for this.

Hi Roy.

thank you for this. adding that code does not show up anything but a blank page.

in my error log it says:

PHP Parse error: syntax error, unexpected $end in index.php on line 30

Line 30 of the index page above is </html> now I really am stuck.

mod edit: If you're going to post any html markup, it needs to be wrapped in [code][/code] tags. Fixed. ~gt

Kevstar
Kevstar's picture
Offline
Enthusiast
England
Last seen: 4 years 4 weeks ago
England
Timezone: GMT+1
Joined: 2012-07-20
Posts: 81
Points: 107

Ok.. So I have edited these 3

Ok.. So I have edited these 3 pages to as follows:

index.php

<?php
 
include_once('test.php');
include_once('article.php');
 
$article = new Article;
$articles = $query->fetchAll();
 
print_r($articles);
 
 
?>

test.php

<?php
 
try {
$PDO = new PDO('mysql:host=localhost;dbname=xclocouk_mobile', 'xclocouk_mobi', 'xclomobile2012');
} catch (PDOException $e) {
echo $e->getMessage();
}
 
?>

article.php

<?php
 
class article {
public function fetch_all(){
    global $pdo;
      $query = $pdo->prepare("SELECT * FROM `articles`");
      $query->excute();
return $query->fetchAll();
              }
 
}
 
?>

now my error log is telling me:

[22-Jun-2013 18:09:11 Europe/London] PHP Fatal error: Call to a member function fetchAll() on a non-object in index.php on line 7

why am I having such great difficulty doing this? Should database creation be so hard?

roytheboy
roytheboy's picture
Offline
Guru
North Wales, UK
Last seen: 6 years 8 weeks ago
North Wales, UK
Timezone: GMT+1
Joined: 2004-09-18
Posts: 2233
Points: 41

I don't know where you got

I don't know where you got your code from, but it's a mess in so many ways. You REALLY need to sit down and spend some time going through a php tutorial, else you're forever going to be relying on people like me to do things for you. I don't mind helping out, but not with the 'lesson 1' stuff.

The blank page was because once we'd fixed the original snag, the parser made it to the next snag, which was no end to the loop at the bottom of index.php. You also had other snags in there, and why your code was made up as it was, I'll never understand.

I've put all your code together neatly, as I would have done it in the first place, with notes for you. It is totaly untested, so I might have made an error or two, but if it works, study it, look up each part and work out for yourself why I have done what I've done and how you might improve on it.

<?php
 
try {
     $pdo = new PDO('mysql:host=localhost:dbname=*DBNAME*', '*DBUSER*', '*DBPASS*'); // login to the db.
} catch (PDOException $e) {
     echo $e->getMessage(); // print any errors.
}
 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // throw all errors.
try {
	$stmt = $pdo->prepare("SELECT * FROM 'articles'"); // form the sql statement.
	$results = array(); // initialise an array for the results.
	if ($stmt->execute()) { // execute the call to the db.
		while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // loop through the resultset.
			$results[] = $row; // load each row into the array.
		}
	}
} catch (PDOException $e) {
	echo $e->getMessage(); // print any errors.
}
 
?>
 
<html>
<head>
<title>Testing Testing 1,2,3</title>
</head>
<body>
<ol>
 
<?php
 
foreach ($results as $result) { // loop through the resultset and print each row.
	echo "<li>\n";
	echo "<a href=\"article.php?id=".$result['article_id']."\">".$result['article_title']."</a>\n";
	echo " posted ".date('l JS', $result['article_timestamp']);
	echo "</li>\n";
}
 
?>
 
</ol>
</body>
</html>

By the way, when I mentioned 'lesson 1' stuff, I was referring to the understanding of variable names and closing loops, not PDO, which is beyond the basics Wink

Life's a b*tch and then you die!