views:

162

answers:

5

Hello,

I'll just show some code to show how I do web development in PHP.

<html>

<head>
<title>Example #3 TDavid's Very First PHP Script ever!</title>
</head>
<? print(Date("m/j/y")); 
require_once("somefile.php"); 
$mysql_db = "DATABASE NAME";
$mysql_user = "YOUR MYSQL USERNAME";
$mysql_pass = "YOUR MYSQL PASSWORD";
$mysql_link = mysql_connect("localhost", $mysql_user, $mysql_pass);
mysql_select_db($mysql_db, $mysql_link);

$result = mysql_query("SELECT impressions from tds_counter where COUNT_ID='$cid'", $mysql_link);
if(mysql_num_rows($result)) {
   mysql_query("UPDATE tds_counter set impressions=impressions+1 where COUNT_ID='$cid'", $mysql_link);
   $row = mysql_fetch_row($result);
   if(!$inv) {
       print("$row[0]");
   }
}

?>

<body>
</body>
</html>

Thats it. I write every file like this. Recently, I learnt OOP and started using classes & objects in PHP.

I hear that there are many frameworks there for PHP. They say that one must use these libraries. But I feel they are just making things complicated.

Anyway, this is how I've been doing my web development. Now, I want to improve this. and make it professional. Also I want to move to Python. I searched SO archives and found everyone suggesting Django. But, can any one give me some idea about how web development in Python works?

user (client) request for page ---> webserver(->embedded PHP interpreter) ----> Server side(PHP) Script ---> MySQL Server.

Now, is it that instead of PHP interpreter there is python interpreter & instead of php script there is python script, which contains both HTML & python (embedded in some kind of python tags). Python script connects to database server and fetches some data which will be printed as HTML. or is it different in python world?

Is this Django thing like frameworks for PHP? Can't one code in python without using Django. Because, I never encountered any post without django

Please give me some kick start.

+3  A: 

PHP was built for making web pages, so a lot of the infrastructure is already set up for you. Python was built more as a general-purpose scripting language so you need a bit of extra infrastructure to handle requests and produce web pages.

There are multiple frameworks for Python. Django is the most popular but Pylons is another good one. Have a look at this list of Web Frameworks for Python:

A web application may use a combination of a base HTTP application server, a storage mechanism such as a database, a template engine, a request dispatcher, an authentication module and an AJAX toolkit. These can be individual components or be provided together in a high-level framework.

These are the most popular high-level frameworks. Many of them include components listed on the WebComponents page.

  • Django (1.1 Released 2009-07-29) a high-level Python Web framework that encourages rapid development and clean, pragmatic design

  • Grok (1.0 Released 2009-10-07) is built on the existing Zope 3 libraries, but aims to provide an easier learning curve and a more agile development experience. It does this by placing an emphasis on convention over configuration and DRY (Don't Repeat Yourself).

  • Pylons (0.9.7 Released 2009-02-23) a lightweight Web framework emphasizing flexibility and rapid development. It combines the very best ideas from the worlds of Ruby, Python and Perl, providing a structured but extremely flexible Python Web framework. It's also one of the first projects to leverage the emerging WSGI standard, which allows extensive re-use and flexibility but only if you need it. Out of the box, Pylons aims to make Web development fast, flexible and easy. Pylons is built on top of Paste (see below).

  • TurboGears (2.0 Released 2009-05-27) the rapid Web development megaframework you've been looking for. Combines CherryPy, Kid, SQLAlchemy and MochiKit. Create a database-driven, ready-to-extend application in minutes. All with designer friendly templates, easy AJAX on the browser side and on the server side, with an incredibly powerful and flexible Object Relational Mapper (ORM), and with code that is as natural as writing a function. After reviewing the website check out: QuickStart Manual

  • web2py (1.72.3 Released 2009-11-10) All in one package with no further dependencies. Development, deployment, debugging, testing, database administration and maintenance of applications can be done via the provided web interface. web2py has no configuration files, requires no installation, can be run off a USB drive. web2py uses Python for the Model, the Views and the Controllers, has a built-in ticketing system to manage errors, an internationalization engine, works with MySQL, PostgreSQL, SQLite , Oracle, MSSQL and the Google App Engine via an ORM abstraction layer. web2py includes libraries to handle HTML/XML, RSS, ATOM, CSV, RTF, JSON, AJAX, XMLRPC, WIKI markup. Production ready, capable of upload/download of very large files, and always backward compatible.

  • Zope (2.10.7 Released 2008-10-25, 2.11.2 Released 2008-25-10, Zope 3.4.0 Released 2009-01-29) Being the grandaddy of Python web frameworks, Zope has grown into a family of frameworks over the years. Zope 1 was released in 1999. Zope 2 is both a web framework and a general purpose application server, today it is primarily used by ContentManagementSystems

Skilldrick
A: 

There are many PHP frameworks resembling Django. I suggest you try one of them. Like Zend or CodeIgniter.

Sinan
+1  A: 

One thing you could improve in your code sample: don't repeat yourself.

You say you do every file like the above? If so, then you have your database password in every file, and the code to create a connection is repeated over and over. That's clutter, and if your database password ever changes, you've got a lot of files to update!

This would be an improvement:

require_once("database_connection.php");
//Write function in above file that returns database connection.
//That will be the only place you need to give the database name and login.
//Use PDO so that you can use a different database type later if you like, and
//so you get better capabilities
$dbc = default_db_connection();
//If using PDO, you can use parameterized queries 
$query = "INSERT INTO `ninjaturtles` (`name`, `weapon`, `color`) ";
$query .= "VALUES (:name, :weapon, :color)";
//OO style, tells database "I'm going to send a query that looks like the above;
//I'll tell you what data to plug in for those variables in a second."
$st = $dbc->prepare($query);
//Now you tell the database to plug in the following values and run the query.
//User-entered values are automatically escaped and can't possibly screw with
//the query structure - a huge security benefit - see bobby-tables.com
//Also, you can re-run this command by calling execute again with different
// values - faster than starting a new query from scratch
$st->execute(array(
  ':name' => $_POST['name'],
  ':weapon' => $_POST['weapon'],
  ':color' => $_POST['color']
 ));

PDO also gives you (I think) a nice syntax for simple queries:

$query = "SELECT `speed`, `tenacity` FROM `badgers`";
$st=$dbc->query($query);
foreach($st->fetchall() as $row){
  echo 'Speed: ' . $row[`speed`] . '<br />';
  echo 'Tenacity: ' . $row[`tenacity`] . '<br />';
}
Nathan Long
+1 for `ninja turtles` :) (and PDO of course)
Skilldrick
+2  A: 

But, can any one give me some idea about how web development in Python works?

user (client) request for page ---> webserver(->embedded PHP interpreter) ----> Server side(PHP) Script ---> MySQL Server.

Now, is it that instead of PHP interpreter there is python interpreter & instead of php script there is python script, which contains both HTML & python (embedded in some kind of python tags). Python script connects to database server and fetches some data which will be printed as HTML. or is it different in python world?

Here is how Django works:

  • User requests page.
  • Depending on the URL, the server calls a Python function (you configure this with regular expressions).
  • The Python function performs any work that needs to be done, typically fetching some data from a database.
  • It then calls a template, which basically is a HTML page with placeholders for actual data. The template engine generates a HTML page from the template and the data.
  • This page is sent to the client.

As you can see, a main difference is that data logic and presentation are clearly separated (it's possible in PHP as well, but not enforced at all).

Bastien Léonard