Sunday, August 14, 2011

Playing with PHP

I'm not sure who w3schools are, but they have a tutorial on PHP. They offer a list of things you need, including MySQL, Apache and PHP. I'm going to ignore this, because I am tired of getting things to work at home and then scratching my head to make them work on a production server.

I do note however that my web host claims to offer MySQL, Apache and PHP on its server. I shall therefore do all of my experimenting in a live environment. Unprofessional perhaps, but how many people look at my web site anyway?

The first code sample is a good old "Hello World" message:

<html>
<body>

<?php
echo "Hello World";
?>

</body>
</html>

I popped this in a text file, called it hello.php, and uploaded it to my web host. I then typed:

http://www.interactived.com/hello.php

into my browser, and what do you know, the following page appeared:

So far so good.

There followed a number of boring chapters on linguistics, most of which I tried out but will omit here. The next more interesting one was an example of a form:

<html>
<body>

<form action="welcome.php" method="post">
<p align=right>
Name: <input type="text" name="fname" /><br>
Age: <input type="text" name="age" /><br>
<input type="submit" />
</p>
</form>

</body>
</html>

I saved this as hello.htm, uploaded it and typed:

http://www.interactived.com/hello.htm

into my browser. A nice neat form appeared on the screen (see below) and I typed Jonathan into the name field and 24 into the age field.

Before clicking the Submit button, I also saved the following code as welcome.php and uploaded it:

<html>
<body>

Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html>

Then, when I clicked the Submit button, the following text appeared in the browser:

Most interestingly (for me at least), when I tried to view the source code, it came up as shown below.

So the source code displayed was the rendered HTML, and NOT the PHP source code. This is quite good news, although I am still not quite sure how secure that PHP file is.

After skipping more linguistics chapters the time came to try a data connection. As I had deleted nearly everything from my table in a previous exercise, I decided to try an insert:

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

mysql_select_db("dbAMJ", $con);

mysql_query("INSERT INTO Items (Partid, OpCode, Itemdet, Raw, Rate)
VALUES (1, 1, '2+2=', 1, 1), (1, 1, '3+3=', 1, 1)");

mysql_close($con);
?>

I substituted my credentials, saved it as dbtestin.php, uploaded it and tried it in the browser. That code displays nothing on success (or failure of the sql query), so I visited my web host again and used their phpMyAdmin facility to check that the extra lines had been inserted, and sure enough they had.

I then tried the following code to apply a simple query:

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

mysql_select_db("dbAMJ", $con);

$result = mysql_query("SELECT * FROM Items") or die(mysql_error());

echo "<table border='1'>
<tr>
<th>Itemdet</th>
<th>Raw</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Itemdet'] . "</td>";
echo "<td>" . $row['Raw'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>

The following table was displayed:

So it seems that I can run PHP on my live web site, and I can use it to connect to my database. Furthermore, the PHP source code does not appear to be accessible for viewing in the browser. This would appear to be a step in the right direction.

1 comment:

Superbowl2012 said...

Excellent pieces. Keep posting such kind of information on your blog. I really impressed by your blog.
SEO tools