How advanced does you php for database coding need to be?

Andy

New member
So i know basic php, i can connect, create db's, tables, insert, select, drop record, tables, db...... do it all in a few lines of php really.

So i can search a db using what i assume is a basic "select from" in php and display the results in a table on the page or i verify a login etc,,, BUT i look at some php code say from the likes of php code generators and looks a good bit more advanced just to do the same thing i think.....i mean what else can it be doing that needs all that code.

What am i missing here, will my basic php manage what else do i need to know?

I am taking about really verifiyng logins, retrieving data for to dispaly on the web page, inserting new data, images, all data really, i just can't understand why i see all his advanaced php code for which i assume must be doing the same thing.

My php skills don't allow me to eaily see what this adavnced code is doing so...hence i am baffled.

ALTHOUGH this advanced code is not singly for working with db's it's part of an overall system of database + displaying the results so maybe most of the code is about displaying, i don't know.

Take this code for exmaple, this is what i would us,

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}

mysql_close($con);
?>
 
You are selecting everything when you only need to select one record. If your site was as popular as ebay with a few million users, then your code selects every user, then it uses a loop to act on the results.

You can't avoid some loops in code, but any long loop is going to slow down the response at the user end, remember that php is pre-processing, so their page hasn't even started to load while your code is being processed.

Instead narrow down as far as possible using SQL. The database engine is optimised for this sort of search, it can get to the required record or subset of records much more quickly then your php can loop through it.

So to add what you have learnt, start working with the 'where' statement, also be careful of injection attacts, no programmer allows user input directly into the database without first ensuring they are not injecting SQL.

A good way to do this is to make use of stored procedures, if you ever program for a bank you will find that all SQL is ran from stored procedures. There are a few reasons, first it is easier to make it safe, but second because it is much easier to maintain.
 
You are selecting everything when you only need to select one record. If your site was as popular as ebay with a few million users, then your code selects every user, then it uses a loop to act on the results.

You can't avoid some loops in code, but any long loop is going to slow down the response at the user end, remember that php is pre-processing, so their page hasn't even started to load while your code is being processed.

Instead narrow down as far as possible using SQL. The database engine is optimised for this sort of search, it can get to the required record or subset of records much more quickly then your php can loop through it.

So to add what you have learnt, start working with the 'where' statement, also be careful of injection attacts, no programmer allows user input directly into the database without first ensuring they are not injecting SQL.

A good way to do this is to make use of stored procedures, if you ever program for a bank you will find that all SQL is ran from stored procedures. There are a few reasons, first it is easier to make it safe, but second because it is much easier to maintain.
 
Back
Top