1 / 12

PHP - MySql

Bayu Priyambadha, S.Kom. PHP - MySql. PHP mySQL functions. We are using here the new version of PHP mySQL function, starting with mysql_ The interface is object-oriented, but can also be accessed in a non-object-oriented way. This is known as the procedural style, in the documentation.

chen
Download Presentation

PHP - MySql

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bayu Priyambadha, S.Kom PHP - MySql

  2. PHP mySQL functions • We are using here the new version of PHP mySQL function, starting with mysql_ • The interface is object-oriented, but can also be accessed in a non-object-oriented way. This is known as the procedural style, in the documentation. • You should use the online documentation at http://php.net/mysql

  3. mysql_connect() • This is used to establish a connection to the mySQL server. It is typically of the form mysql_connect('host', 'user', 'password'); • Example $link= mysql_connect('localhost','boozer','heineken'); • You can use localhost as the host name for wotan talking to itself, but you could also connect to other Internet hosts, if you have permission. • The function returns a variable of type “resource”. If there is a mistake, it returns false.

  4. mysql_connect_error () • This function does not exist. Do not study this slide. • This function returns a string with the last connection error. $link = mysql_connect("localhost", "bad_user", "");if (!$link) {print "Can't connect to localhost. The error is<br>"; print mysql_connect_error(); print "<br/>"; }

  5. mysql_error() • This function return the error from the last mySQL command. It returns false if there was no error. $error=mysql_error(); if($error) { print "mySQL error: $error<br/>"; } • The value returned from that function is a simple string. • It is a good idea to check out error messages.

  6. mysql_select_db() • This command has the syntax mysql_select_db('database') where database is the name of a database. • It returns a Boolean. • This tells mySQL that you now want to use the database database. mysql_select_db('beer_shop'); • It has the same effect as issuing USE beer_shop; within mySQL.

  7. mysql_query() • mysql_query(query) send the query query to mySQL. $link = mysql_connect("localhost", "shop_owner", "bruch"); // you may then add some connection checks $query="SELECT * FROM beer_shop.customers"; $result=mysql_query($query); • Note that the query itself does not require a terminating semicolon. • The result is in $result.

  8. result of mysql_query() • For SELECT, SHOW, DESCRIBE or EXPLAIN mySQL queries, mysql_query() returns a resource that can be further examined with mysql_fetch_array(). • For UPDATE, DELETE, DROP and others, mysql_query() returns a Boolean value.

  9. examining resulting rows • mysql_fetch_array(result) returns an array that is the result row for the resource resource representing the most recent, or NULL if it the last result is reached. Its results in an array that contains the columns requested both by number and by column name: while($columns=mysql_fetch_array($result)) { print 'name: '.$columns['name']; print 'first column: $columns[0]; }

  10. mysql_real_escape_string() • mysql_real_escape_string(string) returns a string escaped for the using in mySQL. $name="John O'Guiness"; $s_name=mysql_real_escape_string($name); print $s_name; // prints: John O\'Guiness • Note that this function makes a call to mySQL, therefore a connection must be established before the function can be used. • This function guards against SQL injections.

  11. mysql_close() • This command connection. When it is invoked without an argument, it closes the current connection. • This is the happiest command there is, because it means that we have finished. • Unfortunately it is not used very often because the mySQL connection is closed automatically when the script finishes running.

  12. echo "</table>"; mysql_close($conn); ?> </body> </html> <html> <head> <title>Untitled Document</title> </head> <body> <?php $conn = mysql_connect("localhost","root",“xxxxxx"); mysql_select_db("buku",$conn); $qry = mysql_query("select * from buku",$conn); echo "<table border='1'>"; while ($data = mysql_fetch_array($qry)) { echo "<tr>"; echo "<td>".$data["buku_id"]."</td>"; echo "<td>".$data["buku_judul"]."</td>"; echo "<td>".$data["buku_pengarang"]."</td>"; echo "<td>".$data["buku_publiser"]."</td>"; echo "<td>".$data["buku_tahun"]."</td>"; echo "</tr>"; }

More Related