Getting and outputting the result of a function

I’m studying php and mysql from a book. Everything has worked so far! Now we have this function totaljokesfunction.inc.php:

<?php
// login to mysql
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

function totalJokes()
{
try
{
$result = $GLOBALS['pdo']->query('SELECT COUNT(*) FROM jokes');
}
catch (PDOException $e)
{
$error = 'Database error counting jokes!';
include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
exit();
}
$row = $result->fetch();

return $row[0];
}
?>

I have an index.php and firstjoke.html.php and totaljokesfunction.inc.php

I want to output the result of totaljokesfunction.inc.php in firstjoke.html.php but I can’t.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Jokes</title>
</head>
<body>
<p>
<h2>The first joke is:</h2>
<?php $output = $_SERVER['DOCUMENT_ROOT'] . '/includes/totaljokesfunction.inc.php';
echo $output; ?>
</p>
</body>
</html>

All I get is:

The first joke is:

/var/www/html/includes/totaljokesfunction.inc.php

How do I output the result of totaljokesfunction.inc.php please??

User written functions consist of two parts. The function definition and the function call. The function definition is contained in the totaljokesfunction.inc.php file. To cause the function definition to exist in your application, you need to ‘require’ the file holding the definition. You can then call the function when needed. The file holding the function definition should only be responsible for the function definition(s). It should NOT be responsible for creating the database connection. Your main application code should be responsible for causing the database connection to be created. Any function that needs a database connection should have a call-time parameter for the connection (don’t use any global keyword/variable to supply a database connection to a function.)

Next, don’t unconditionally output database statement errors onto a web page, as this only gives hackers useful information when they intentionally trigger errors. Instead, remove the current try/catch logic and let php catch and handle most database statement exceptions, where it will use its error related setting to control what happens with the actual error information (database statement errors will ‘automatically’ get displayed/logged the same as php errors.) The exception to this rule is for recoverable errors, such as inserting/updating user submitted data. In this case your code would catch the database statement exception, detect if the error number is for something your code is designed to handle, then setup a user error message telling the user what was wrong with the data they submitted.

2 Likes

In the book he put this, which in all the other examples logs me in to the mysql server:

include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

index.php has this:

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php'; include 'firstjoke.html.php'; ?>

How to get the result of totaljokesfunction.inc.php displayed in an html document??

I tried taking out the log in part. I am not getting any errors in the log file, but nothing is displayed.

You have to call the function. Simple example

// this is just the design 
function test() {
    return 'This is a test.';
}

// now call the function 
echo test();
3 Likes

Thanks, I got it to work with:

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/totaljokesfunction.inc.php';
echo 'This is the value totalJokes() ' . totalJokes();?>

If this was Python, I would just set:

answer = totalJokes()
print(answer)

This php is tricky!

You can do the same thing with most languages, php is no different, but it does need the file first, just like you would import a module in python.

$answer = totalJokes();
echo answer;
1 Like

Thanks, I tried and it worked like that, here’s what I get in my browser:

The number of jokes is:

This is echoed from the function totalJokes(): 7
This is the value of totalJokes() echoed from firstjoke.html.php: 7

I can see the potential of PHP + MySQL, it’s amazing! I just want to make my little homework page better!

What book is it, please?
I found it in another of your posts.

**EDIT:**Even though you have received many responses, I would assume that the book shows you about creating and calling functions before moving on.

It is so easy to ‘follow’ a book and ‘code-along’ but not learn from it. I recommend that once you have read about a concept in the book, try to create your own simple example and then build from it. I’ve been reading through ‘Practical PHP 7, MySQL 8, and MariaDB Website Databases’ by Adrian West and Steve Pettyman. I read through a chapter and then applied the information to building my own CMS. It’s a long way off being used as a production CMS, but I am learning more as I try to remember what I learnt.

Also, read other books, because different authors approach the same thing in different ways, which will allow you to have greater width of knowledge, and perhaps another book explains in a more accessible way. The book I mentioned above does not introduce heredoc, but another book does.

PHP & MySQL: Novice to Ninja by Kevin Yank

What I like about this book is, it is good for numbskulls like me. All the code is available on github:

If you go to sitepoint.com and join, about $100 a year, you can get many many books as pdf. I can highly recommend it.

I will never be a coding ninja, but you may be, once you’ve read all these books!! Good luck!

Thanks for the book title. Coding Ninjas do not exist anyway :slight_smile:. Hmm, maybe they do; we wouldn’t see them, would we? I edited my previous post.

Read a bit and then make something. Before long, you may know more that you realise.

Sponsor our Newsletter | Privacy Policy | Terms of Service