Ne öğrenmek ve gerekir

4 Cevap php

Ben sadece PHP web geliştirme yapmak nasıl göstermek için bazı kod göstereceğim.

<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. Ben bu gibi her dosyayı yazın. Son zamanlarda, ben OOP öğrendim ve sınıfları ve kullanmaya başladı PHP nesneleri.

Ben birçok çerçeveler PHP için orada olduğunu duydum. Onlar bir bu kitaplıkları kullanmanız gerektiğini söylüyorlar. Ama onlar sadece karmaşık şeyler yapıyoruz hissediyorum.

Her neyse, bu benim web geliştirme yapıyorum nasıl. Şimdi, bu geliştirmek istiyorum. ve profesyonel olun. Ayrıca ben Python taşımak istiyorum. SO arşivleri arandı ve Django düşündüren herkesi bulundu. Ancak, herhangi biri bana Python web geliştirme nasıl çalıştığı hakkında biraz fikir verebilir?

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

Şimdi, bunun yerine PHP yorumlayıcısı python tercüman & olmasıdır yerine php script hem HTML & içeren python komut dosyası var piton (python etiketleri çeşit gömülü). Python komut veritabanı sunucusu bağlanır ve HTML olarak basılacak bazı verileri getirir. ya da piton dünyanın farklı?

PHP için çerçeveler gibi bu Django şey mi? Django kullanmadan Python bir kod olamaz. Django olmadan herhangi bir yazı hiç karşılaşmadım, çünkü

Bana biraz tekme başlangıç ​​verin.

4 Cevap

PHP web sayfaları yapmak için inşa edilmiştir, bu nedenle altyapı bir sürü zaten sizin için ayarlanır. Istekleri işlemek ve web sayfaları üretmek için ekstra bir altyapı biraz ihtiyacınız kadar Python genel amaçlı bir betik dili olarak daha inşa edilmiştir.

Python için birden çerçeveler vardır. Django en popüler ancak Pylons iyi bir tanesidir. Bakabilirsiniz 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

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

sayfa için kullanıcı (istemci) isteği ---> web sunucusu (-> gömülü PHP yorumlayıcı) ----> Sunucu tarafı (PHP) Komut ---> 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?

İşte Django nasıl işliyor:

  • Kullanıcı istekleri sayfası.
  • URL'ye bağlı olarak, sunucu (Düzenli ifadeler ile bu yapılandırma) bir Python işlevini çağırır.
  • Python fonksiyonu genelde bir veritabanından bazı verileri alma, yapılması gereken herhangi bir çalışma gerçekleştirir.
  • 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.
  • Bu sayfa istemciye gönderilir.

Gördüğünüz gibi, bir temel fark veri mantık ve sunum açıkça (aynı zamanda PHP mümkün, ama hiç zorlanmaz) ayrılmış olmasıdır.

Eğer kod örneğinde getirebileceği bir şey: don't repeat yourself.

Eğer yukarıdaki gibi her dosyayı yapmak demek? Eğer öyleyse, o zaman her dosya veritabanı şifre var, ve bir bağlantı oluşturmak için kod tekrar tekrar yinelenir. Bu dağınıklığı, ve veritabanı şifre hiç değiştirirse, güncelleştirmek için bir sürü dosya var!

Bu bir gelişme olacaktır:

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 de (sanırım) Size basit sorguları için güzel bir sözdizimi sağlar:

$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 />';
}