New to PHP and need some help with errors?

Wondering what’s going on with your variables?

Not seeing what you are expecting?

If you are a newbie learning PHP, one of the most useful functions to know is var_dump().

It dumps the contents of a variable, so you can see it in your web browser, it’s simple to use for example:

var_dump($my_variable);

Then you can see what $my_variable contains. The data type and in the case of an array or an object. The structure will then be visible.
You will then know how to access the fields within the array / object.

Although I’m far from a newbie, var_dump() is an ever useful function. Use it to determine the value of variables and debugging your code.

var_dump() is your friend in many scenarios, remember it and use it while developing.

Example usage, if you want to see all the variables in a GET or POST request:

var_dump($_POST);
var_dump($_GET);

To all the variables in the SERVER global variable:

var_dump($_SERVER);

To check what’s stored in the session:

var_dump($_SESSION);

How to join three MySQL database tables together in a simple SELECT statement

Following on from my post of how to join two MySQL tables together, adding a third table is just as easy, but if you need an example this one extends the previous post by adding a third table.

For example I have a table of products, table of product categories and table of product suppliers, how to return the category description of each product and the supplier of the product?

Products
———–
id
category_id
supplier_id
product_name

Categories
————-
id
description

Suppliers
—————
id
supplier_name
supplier_address

In it’s simplest form you would join three tables based on the relationship between a key and it’s foreign key stored in another table:

SELECT p.id, p.product_name, c.description AS ‘cat_description’, s.supplier_name
FROM products p
INNER JOIN categories c on p.category_id = c.id
INNER JOIN suppliers s ON p.supplier_id = s.id

Using a well designed relational database and JOINed queries will make your PHP code simpler and more efficient by returning the data from two or more tables in one query.

Like this post? please leave a comment below:

Still using the old MySQL driver? What you need to know about MySQLi…

Its time to upgrade from the PHP MySQL extension

The PHP MySQL extension was deprecated in PHP 5.5.0!

You should upgrade your code to use MySQLi at least, even better would be upgrading to PDO.

This post about selecting data uses PDO and has the full source code available for you.

But back to MySQLi

The “i” in mysqli stands for IMPROVED, its not only new it’s improved too.

If you are still using the plain old mysql driver you should check to see if you can use mysqli.

All you need is PHP 5.4 or greater and add that “i”!

Once you get to PHP 5.5 or above mysql will no longer be supported.

Your queries will run faster, be more efficient and you can make use of more great features like prepared statements and it’s object oriented interface.

If you are using a PHP framework like CodeIgniter, switching should be a breeze, you just need to add the “i”.

For example you might have this in your code:

<?php
$db['default']['dbdriver'] = 'mysqli';
?>

If you aren’t using a framework, please consider upgrading your code, depending on your hosting company they might upgrade their PHP version without warning!

It has happened before!

Then you will need to be fixing and upgrading your code under pressure while your website is down!

Google Charts review

If you are looking to add charts to your PHP pages and haven’t already checked out Google Charts, I recommend you do, I’ve used them in a number of projects to great effect.

Features & Benefits:

  • They are free
  • HTML5 / SVG  based for cross-browser compatibility
  • Well documented
  • Lots of examples in the Chart Gallery, with new types of charts appearing regularly
  • Lots of different configuration options to customize them as much as you should need

Pro tip on PHP usage:

Below is some example PHP code I used to create the chart on this page: http://trabest.com/social-tracking-automotive-brands.  To view the chart just click on the “Graph” to show it.

<script type=”text/javascript” src=”https://www.google.com/jsapi”></script>
<script type=”text/javascript”>
google.load(“visualization”, “1”, {packages:[“corechart”]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
[‘Channel’, ‘Facebook’, ‘Twitter’, ‘Youtube’, { role: ‘annotation’ } ],
<?php echo $chart_output;?>
]);

var options = {
width: 950,
height: 950,
legend: { position: ‘right’, maxLines: 3 },
colors:[‘#3b5998′,’#4099FF’, ‘#c4302b’],
annotations: {},
vAxis: {format: ‘decimal’},
hAxis: {format: ‘decimal’, slantedText: ‘true’},
isStacked: true,
title: “Social Media Brand Audiences”,
};

var view = new google.visualization.DataView(data);

var chart = new google.visualization.ColumnChart(document.getElementById(“columnchart_values”));
chart.draw(view, options);
}
</script>
<div id=”columnchart_values” style=”width: 100%; height: 1000px;”></div>

The $chart_output variable contains data from the database outputted to a formatted string in this format, for example

“[‘Acura’, 921115,119938,20327, ”],[‘Alfa Romeo’, 722300,58712,4745, ”],[‘Aston Martin’, 6471567,825688,62064, ”],…..etc”

Let me know what you think of Google Charts? Is there a better free alternative out there that you use?

4 Free PHP PDF Libraries you can use to convert HTML to PDF

Use any of these 4 libraries below to easily covert your HTML to PDF files with PHP.

I’ve always used TCPDF for converting HTML, including JPEG and SVG images into PDF files, which you can download inline in the browser, save on the server or force a “save as” dialog.

  1. Free PDF – http://www.fpdf.org/
  2. TCPDF – http://www.tcpdf.org/
  3. MPDF – http://www.mpdf1.com/
  4. dompdf – https://github.com/dompdf/dompdf

 

How to join two MySQL database tables together in a simple SELECT statement

How to join two MySQL database tables together in a simple SELECT statement?
For example I have a table of products and a table of product categories, how to return the category description of each product?
Products
———–
id
category_id
product_name

Categories
————-
id
description

A. In it’s simplest form you would join two tables based on the relationship between a key and it’s foreign key stored in another table:
SELECT p.id, p.product_name, c.description AS ‘cat_description’
FROM products p
INNER JOIN categories c on p.category_id = c.id

Using a well designed relational database and JOINed queries will make your PHP code simpler and more efficient by returning the data from two or more tables in one query.

Like this post? please leave a comment below:

How do you include frequently used code in another PHP file?

Q. How do you include PHP files in other PHP files?

– Useful for including your database connection across all pages that need it.
– If you have written some functions once and want to reference them in other pages.
– Also commonly used to keep consistent header and footer parts of your pages, by including them.

A. You can use the include and include_once statement, you can also use the require or require_once statement, all have the same effect of including your code from one PHP file into another PHP file.

<code>include (“my_file_to_include.php”);</code>

(PHP Manual)

This saves you time by re-using your PHP code and makes it easier to maintain.