http___programlama.venus.gen.tr_kategori_asp_1.xml
1.1 1.2 1.3 1.4 1.5 1.6 2.1 3.1 3.2 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 4.12 4.13 4.14 4.15 4.16 4.17 4.18 4.19 5.1 6.1 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.1 9.2 9.3 10.1 10.2 10.3 10.4 10.5 11.1 11.2 11.3 12.1 12.2 13.1 14.1 15.1 15.2 16.1 16.2 17.1 17.2 17.3 17.4 17.5 17.6 18.1 18.2 18.3 18.4 18.5 19.1 19.2 19.3 19.4 20.1 20.2 20.3 20.4 20.5 20.6 20.7 20.8 20.9 20.10 20.11 20.12 20.13 20.14 21.1 22.1 22.2 22.3 23.1 24.1 25.1 26.1 27.1 28.1 29.1 30.1 31.1 32.1 33.1 34.1 36.1 36.2 36.3 36.4 37.1 37.2 37.3 38.1 38.2 38.3 39.1 39.2 40.1 40.2 41.1 41.2 41.3 41.4 41.5
Unless you've been living on Mars for the last six to eight months, you've heard of open source software (OSS). This movement has got so much momentum that even the big boys are taking notice. Companies like Oracle, Informix, and a host of others are releasing their flagship database products for that poster child of the OSS movement, Linux. Having a massively complex RDBMS (relational database management system) is all well and good if you know what to do with it. But perhaps you are just getting into the world of databases. You've read Jay's article and you want to put up your own data-driven Web site. Enough talk! Let's go over what we're going to cover in this tutorial. Let's jump straight in, grab ourselves a copy of these great packages, and get hacking! This isn't simple stuff. There are lots of options available to you for obtaining, compiling, and installing the software. Let's deal with MySQL first, as we'll need it before we get PHP going. MySQL central is http://www.mysql.com/. As befits a program of its stature, there are a zillion mirrors located all over the globe, so do the Internet a favor and pick the one closest to you. You've got plenty of choices at this point. If you're a do-it-yourselfer, then grab the source code. If you're not that brave, there are some precompiled binaries for other platforms already available for download. In addition, there is a shareware version of MySQL for Windows users. It is an older version of MySQL. If you want the latest version, you'll have to purchase a license. There are also ODBC drivers that let your applications talk to MySQL. Various other exciting bits and pieces are lurking about on the site, too, so take a look. The precompiled Unix versions and the Windows version are as simple as unpacking and going, and they don't require much explanation. So let's compile from the source code. Windows users, please keep in mind that you need to run mysqld in the mysql/bin directory. Download the compressed file into your source directory and uncompress and untar it using gzip and tar. The fast way of doing this is to type: gunzip $myvar = "Hello World"; echo $myvar; The first thing you'll notice about the script above are the delimiters. These are the lines that start indicates the end of the block. The power of PHP is that these can be plac ed anywhere - and I mean anywhere - in your code in any number of ways. Later we'll see some interesting uses for these, but for now let's keep it simple. If you wish, you can also configure PHP to use short tags, , but these are not XML compliant, so be careful. If you're making the switch from ASP, you can even configure PHP to use the  delimiters. Another thing you'll notice is the semicolon on the end of each line. These are known as separators and serve to distinguish one set of instructions from another. It is feasible to write an entire PHP script on one line and separate the portions with semicolons. But that would be a mess, so we'll add a new line after each semicolon. Just remember that each line must end in a semicolon. Finally, you see that the word myvar begins with a dollar sign. This symbol tells PHP that this is a variable. We assigned the words "Hello World" to the variable $myvar. A variable can also contain numbers or an array. Either way, all variables start with the dollar sign symbol. The real power of PHP comes from its functions. These are basically processing instructions. If you add up all of the optional add-ins to PHP, there are more than 700 functions available. So there's quite a bit you can do. Now let's add some MySQL to the picture. So now we're ready to plug in MySQL. One handy way of knowing what options are available in PHP and what's going on in your server is to use the phpinfo() function. Create a script with the following: Save and view this script through your Web server. You'll see a page filled with useful and interesting information like this. This info tells all about your server, internal Web server environment variables, the options that are compiled, and on and on. In the first section, Extensions, look for a line beginning with MySQL. If this is missing, then for some reason MySQL hasn't made it into PHP. Go back and review the installation steps and check the PHP documentation to see if you missed anything. If MySQL is there, then you're ready to roll. Before we can get data out of MySQL, we have to put data in it. There's really no easy way to do it at this stage. Most PHP scripts come with what's known as a dump file that contains all the data required to create and populate a MySQL database. The ins and outs of this process are really outside the scope of this tutorial, so I'll just do it for you. MySQL uses its own user permissions table. At setup, a default user (root) is automatically created with no password. It's up to the database administrator to add other users with various permissions, but I could write a whole other article on that, so we'll stick with using the root user. If you set up your own server and database, it's vital that you assign a password to the root user. Anyway, let's get on with the database. For Win32 users, I'm sorry, but this requires some DOS work. You'll have to use a DOS window or type everything in the Ru n window. Don't forget to type in the path to the location of the MySQL/bin directory with your commands. Unix users can work from the MySQL bin directory, but you may have to start each command with ./ so the programs run. The first thing we need to do is create the actual database. From the command line, type: mysqladmin -u root create mydb That creates a database called "mydb." The flag tells MySQL that we're doing this as the root user. Next we'll add some data using everyone's favorite example, the employees database. We're going to need that dump file I mentioned earlier. If you're interested in how it goes together, review the manual that comes with MySQL or check out http://www.turbolift.com/mysql/. Copy and paste the following text to a file and save it in MySQL's bin directory. (I'll call the file mydb.dump.) CREATE TABLE employees ( id tinyint(4) DEFAULT '0' NOT NULL AUTO_INCREMENT, first varchar(20), last varchar(20), address varchar(255), position varchar(50), PRIMARY KEY (id), UNIQUE id (id));INSERT INTO employees VALUES (1,'Bob','Smith','128 Here St, Cityname','Marketing Manager'); INSERT INTO employees VALUES (2,'John','Roberts','45 There St , Townville','Telephonist'); INSERT INTO employees VALUES (3,'Brad','Johnson','1/34 Nowhere Blvd, Snowston','Doorman'); If the lines wrap, make sure that each insert statement is on a new line. Now we'll insert it into the mydb database. From the command line, type: mysql -u root mydb $db = mysql_connect("localhost", "root"); mysql_select_db("mydb",$db); $result = mysql_query("SELECT * FROM employees",$db); printf("First Name: %s \n", mysql_result($result,0,"first")); printf("Last Name: %s \n", mysql_result($result,0,"last")); printf("Address: %s \n", mysql_result($result,0,"address")); printf("Position: %s \n", mysql_result($result,0,"position")); Let's explain what happens here. The mysql_connect() function opens a link to a MySQL server on the specified host (in this case it's localhost) along with a username (root). If you needed to specify a password, you'd add it here as well. The result of the connection is stored in the variable $db. mysql_select_db() then tells PHP that any queries we make are against the mydb database. We could create multiple connections to databases on different servers. But for now, let's leave it to this. Next, mysql_query() does all the hard work. Using the database connection identifier, it sends a line of SQL to the MySQL server to be processed. The results that are returned are stored in the variable $result. Finally, mysql_result() is used to display the values of fields from our query. Using $result, we go to the first row, which is numbered 0, and display the value of the specified fields. The syntax of the printf function may seem a little odd if you haven't used Perl or C before. In each of the lines above, %s indicates that the variable in the second half of the expression (e.g., mysql_result($result,0,"position")) should be treated as a string and printed. For more on printf, see the PHP documentation. So there we have it. We successfully complied, installed, and configured MySQL and PHP, and we've executed a simple script to retrieve some information. In Lesson 2, we'll do some clever stuff to display multiple records and even send data to and from the database.
1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1 10.1 11.1 12.1 13.1 14.1 15.1 16.1 17.1 18.1 19.1 20.1
Bu yazımızda MySQL veritabanına ASP ve PHP’de nasıl bağlantı sağlanacağımızı ve veri çekebileceğimizi öğreneceğiz. Öncelikle ASP ile başlayalım. ASP’de bir MySQL bağlantısı için ADODB objesinden yararlanırız. Set Baglanti = Server.CreateObject("ADODB.Connection") Baglanti.Open "Driver={MySQL ODBC 3.51 Driver};Server=sunucu_adres;Database=db_adi;Port=3306;Uid=kullanici;Password=sifre;" Set Kayitlar = Baglanti.Execute( "SELECT * FROM tablo_adi ORDER BY id DESC LIMIT 0, 10;" ) Do While Not Kayitlar.Eof Response.Write Kayitlar("alinacak_veri") + " " Kayitlar.MoveNext Loop Baglanti.Close Yukarıda geçen “Baglanti” ve “Kayitlar” birer değişken olup. Biri bize bağlantımız hakkında bilgi verir, diğeri ise Baglanti.Execute() ile istediğimiz kayıtları barındırır. Bağlantıyı Baglanti.Close() ile kapatmayı unutmayın. Yukarıda yer alan Kayitlar.EOF bir mantıksal değer olup ancak kayıt listelemeleri bittikten sonra true (doğru) karşılığını verir. Kayitlar.MoveNext ise bir sonraki kaydı istemek için kullanacağımız koddur. Yukarıda bağlantıyı sağlarken kullanılan sunucu_adresi, db_adi, kullanıcı, şifre bilgilerini doğru yazmazsanız bağlantı sağlanamaz. Bunlar MySQL’e bağlantı için gereklidir. Peki PHP’de MySQL’e nasıl bağlanırız? Yukarıda yazdığımız ASP kodunu birebir PHP’ye dönüştürecek olursak aşağıdaki gibi bir kodlamaya gideriz. mysql_connect('sunucu_adresi','kullanici','sifre'); mysql_select_db('db_adi'); $sorgu = mysql_query( "SELECT * FROM tablo_adi ORDER BY id DESC LIMIT 0, 10;" ); while( $kayitlar = mysql_fetch_array( $sorgu ) ) echo $kayitlar['alinacak_veri'] . ' '; mysql_close(); Yukarıda göreceğimiz gibi bağlantımızı mysql_connect() ile sağlıyoruz. Parantez içine sırasıyla sunucu adresi, kullanıcı adı ve şifresi yazılır. mysql_select_db() ile hangi veritabanını kullanacağımızı seçtikten sonra bağlantıyı sağlamış oluruz. Eğer birden fazla veritabanı ile işlem yapacaksanız mysql_select_db’den sonra bir de bağlantı için atayacağınız bir değişken kullanmalısınız. Sorgu cümlemizi mysql_query() komutuyla yazdıktan sonra kayıtları mysql_fetch_array ya da mysql_fetch_assoc ile alırız. Sıralama için kolaylık olması açısından while() döngüsü kullandık. Burada başka bir değişken olarak kayıtların depolanacağı arraylist (dize) yi belirttik ($kayitlar) ve finalde mysql_close() ile veritabanı bağlantısını kapattık. (NOT: Bir PHP belgesi mutlaka “ ” ile biterken, ASP belgesi “ ” ile biter. Kodlar ile ilgili alıştırma yaparken bunu dikkate almayı unutmayın.