Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ch7(publishing my sql data on the web)

83 views

Published on

chhom karath

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Ch7(publishing my sql data on the web)

  1. 1. Publishing MySQL Data on the Web
  2. 2. Introduction • have installed and learned the basics of MySQL, a relational database engine, and PHP, a server-side scripting language. Now you'll see how to use these two new tools together to create a true database-driven Website! • The whole idea of a database-driven Website is to allow the content of the site to reside in a database, and for that content to be dynamically pulled from the database to create Web pages for people to view with a regular Web browser
  3. 3. • ❑ The visitor's Web browser requests the Web page using a standard URL. • ❑ The Web server software (Apache, IIS, or whatever) recognizes that the requested file is a PHP script, and so the server interprets the file using its PHP plug-in, before responding to the page request. • ❑ Certain PHP commands (which you have yet to learn) connect to the MySQL database and request the content that belongs in the Web page. • ❑ The MySQL database responds by sending the requested content to the PHP script. • ❑ The PHP script stores the content into one or more PHP variables, and then uses the now-familiar echo function to output the content as part of the Web page. • ❑ The PHP plug-in finishes up by handing a copy of the HTML it has created to the Web server. • ❑ The Web server sends the HTML to the Web browser as it would a plain HTML file, except that instead of coming directly from an HTML file, the page is the output provided by the PHP plug-in.
  4. 4. Connecting to MySQL with PHP Connect to our MySQL server. • $dbcnx = mysql_connect('localhost', 'root', 'mypasswd'); – EX: $dbcnx = @mysql_connect('localhost', 'root', 'mypasswd'); if (!$dbcnx) { echo( '<p>Unable to connect to the ' .'database server at this time.</p>' ); exit();} Select your database – EX: if (! @mysql_select_db('jokes') ) { die( '<p>Unable to locate the joke ' .'database at this time.</p>' ); }
  5. 5. Sending SQL Queries with PHP • EX: INSERT $sql = "INSERT INTO tbcustomer VALUES(2,'Sopheap','F','Siem Reap','23233434')"; if ( @mysql_query($sql) ) { echo('<p>Insert hery</p>'); } else { die('<p>Cannot Insert te: ' . mysql_error() .'</p>');} • EX: DELETE $sql = "DELETE FROM tbcustomer WHERE CustID=1"; if ( @mysql_query($sql) ) { echo('<p>DELETED hery na</p>'); } else { die('<p>cannot delete te! : ' . mysql_error() .'</p>'); }
  6. 6. • EX: UPDATE $sql = "UPDATE tbcustomer SET CustName='lola', Gender='F', Address='Phnom Penh' WHERE CustID=1"; if ( @mysql_query($sql) ) { echo('<p>Update complete hery na</p>'); } else { die('<p>Cannot delete te! ' . mysql_error() .'</p>'); }
  7. 7. Handling SELECT Result Sets • For SELECT queries this just isn't enough. • In addition to indicating whether the query succeeded or failed, PHP must also receive the results of the query. – EX: $result = @mysql_query('SELECT* FROM tbcustomer;'); if (!$result) { die('<p>Error performing query: ' . mysql_error() .'</p>'); } – EX: $result = @mysql_query('SELECT* FROM tbcustomer;'); if (!$result){ die('<p>Error performing query: ' . mysql_error() .'</p>');} while ( $row = mysql_fetch_array($result) ) { echo('<p>' . $row['CustID'] . '|' . $row['CustName'] . '|' . $row['Gender'] . '|' . $row['Address'] . '|' . $row['Phone'] . '</p>'); }
  8. 8. Inserting Data into the Database • EX: $sql = "INSERT INTO tbcustomer SET CustID=1, CustName='lola', Gender='M', Address='SR', Phone='2343434'"; if (@mysql_query($sql)) { echo('<p>Your joke has been added.</p>'); } else { echo('<p>Error adding submitted joke: ' . mysql_error() . '</p>'); } Delete Data in the Database • EX: $sql = 'DELETE FROM tbcustomer WHERE CustID=1'; if (@mysql_query($sql)) { echo('<p>has been deleted.</p>'); } else { echo('<p>Error deleting joke: ' . mysql_error() . '</p>'); }

×