Language Selection

English French German Italian Portuguese Spanish

Learn PHP - Getting dynamic

Filed under
Software

The whole point of coding in PHP instead of just using HTML files is to make everything dynamic. You want the pages to make themselves, so you can spend more time counting your loot. The ideal way to get content onto your pages is from a database. First of all, databases are, as the name suggests, great places to store data. Secondly, clients pay more if you use the words "database" and "dynamic" in the quote and invoice.

First off, we need to get our PHP program talking to the database. The primary time-waster when querying databases is waiting for the darn thing to establish a connection, so we'll do that right at the start and get it over with. We'll do this in a function so that you can type all this out once and just use it over and over, thus saving more time for sending out bills for your web development work:

function dbconnect($dbserver,$dbusername,$dbpassword,$dbname) {
$connection=mysql_connect($dbserver,$dbusername,$dbpassword) or die(mysql_error);
$db=mysql_select_db($dbname,$connection) or die(mysql_error());
}

The first line establishes the connection, and the second line selects the tectonic database. The die(mysql_error()) will display any errors and stop the program from executing further if something goes terribly, terribly wrong.
When you want to call this function, you'd use something like this:


dbconnect('localhost','tectonicdbuser','topsecret','tectonic');

Every time you call this function you will be instantly and magically connected to your database until you either explicitly disconnect or the page finishes processing. I don't bother to disconnect because, well, I'm lazy.

Right, now we want to get some info out of the database. We'll use the unique id number to identify which story we want, and go grab it before anyone notices.

Full Article.

More in Tux Machines

U.S. government releases open source gamification software

The United States' National Geospatial-Intelligence Agency has made some of its internally-developed gamification software available for free on GitHub under the MIT free software license. Developers may find it useful as a tool for configuring a server to track "gamification" systems like points or badges against user accounts on apps or websites; at the very least, it offers interesting insight into how the NGA is using game design tenets in its training programs. Read more

Let's Pay for Open Source with a Closed-Source Software Levy

This column has often explored ways in which some of the key ideas underlying free software and open source are being applied in other fields. But that equivalence can flow in both directions: developments in fields outside the digital world may well have useful lessons for computing. A case in point is a fascinating post by James Love, Director of Knowledge Ecology International (KEI), a non-governmental organisation concerned with public health and other important issues. It is called "The value of an open source dividend", and is a discussion of the problems the world of pharma faces because of the distorting effect of patents - problems it shares with the world of computing... Read more

Features Of The Linux 3.18 Kernel

With Linux 3.18-rc1 arriving one week early I didn't have a chance to write a feature overview of Linux 3.18 prior to this first development release that marked the close of the merge window. For those that didn't stay up to date with our dozens of Linux 3.18 kernel articles about changes and new features, here's a concise overview. Read more

Norway closes its open source resource centre

The government of Norway will no longer fund its open source resource centre, Friprog. Activities are wound down and the centre will be closed at the end of the year, Friprog reports. The GoOpen conference, planned for last September but postponed to May 2015, is now cancelled. Read more