1 / 13

Internet application developmenT

Internet application developmenT. PRACTICAL ON CONNECTING TO MYSQL. PHP MySQL Introduction . What is MySQL? MySQL is the most popular open-source database system. The data in MySQL is stored in database objects called tables.

bevis
Download Presentation

Internet application developmenT

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. Internet application developmenT PRACTICAL ON CONNECTING TO MYSQL

  2. PHP MySQL Introduction • What is MySQL? • MySQL is the most popular open-source database system. The data in MySQL is stored in database objects called tables. • A table is a collection of related data entries and it consists of columns and rows. • Databases are useful when storing information categorically. A company may have a database with the following tables: "Employees", "Products", "Customers" and "Orders".

  3. A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or “students"). Tables contain records (rows) with data. • Below is an example of a table called “students": • The table above contains three records (one for each person) and four columns (LastName, FirstName, Address, and City).

  4. Queries • A query is a question or a request. • With MySQL, we can query a database for specific information and have a recordset returned. • Look at the following query: SELECT LastName FROM students • The query above selects all the data in the "LastName" column from the “students" table, and will return a recordset like this:

  5. PHP MySQL Connect to a Database • Create a Connection to a MySQL Database • Before you can access data in a database, you must create a connection to the database. • In PHP, this is done with the mysql_connect() function. • Syntax: mysql_connect(servername,username,password);

  6. How to add a new user to mysql • Visit http://localhost/phpmyadminThis will bring you to the MySQL setup page. • choose the user classification as follow Then click on Add user

  7. Add user will pops up. • Type the user name and password then check all the global privileges. And finally click on add user.

  8. Creating database using Mysql • There are two ways to create a database: • first way: 1)Visit http://localhost/phpmyadmin. This will bring you to the MySQL setup page. 2) Enter a name for the database, then clickon the Create button. 3) Ensure the database was successfully created. • Second way: through php code by using the next statement: mysql_query("CREATE DATABASE name_of_Db",connection)

  9. Create a Table • The CREATE TABLE statement is used to create a table in MySQL. • Syntax CREATE TABLE table_name( column_name1 data_type,column_name2 data_type,column_name3 data_type, .... ) • We must add the CREATE TABLE statement to the mysql_query() function to execute the command. • Example • The following example creates a table named “students", with three columns. The column names will be "FirstName", "LastName" and “UID":

  10. <?php$con = mysql_connect("localhost",“moh",“1414");if (!$con)  {  die('Could not connect: ' . mysql_error());}// Create databaseif (mysql_query("CREATE DATABASE first_DB",$con))  {  echo "Database created";  }else  {  echo "Error creating database: " . mysql_error();}mysql_select_db(“first_DB", $con);

  11. $sql = "CREATE TABLE students (FirstNamevarchar(15),LastNamevarchar(15), UID int )";// Execute querymysql_query($sql,$con);mysql_close($con);?>

  12. Insert Data Into a Database Table • The INSERT INTO statement is used to add new records to a database table. • Syntax INSERT INTO table_nameVALUES (value1, value2, value3,...) • Now, we will insert some data to the table that we created: <?php$con = mysql_connect("localhost",“moh",“1414");if (!$con)  {  die('Could not connect: ' . mysql_error());}mysql_select_db(“first_DB", $con);mysql_query("INSERT INTO students(FirstName, LastName, UID)VALUES (‘Ali', ‘Alnashri',430444)"); mysql_query("INSERT INTO students(FirstName, LastName, UID) VALUES (‘Khalid', ‘Alqahtani',430777)");mysql_close($con);?>

  13. Select data from database table • Select Data From a Database Table • The SELECT statement is used to select data from a database. • Syntax SELECT column_name(s) FROM table_name <?php$con = mysql_connect("localhost",“moh",“1414");if (!$con)  {  die('Could not connect: ' . mysql_error());}mysql_select_db(“first_DB", $con); $result = mysql_query("SELECT * FROM students");while($row = mysql_fetch_array($result))  {  echo $row['FirstName'] . " " . $row['LastName']. " " . $row[‘UID'] ;  echo "<br />";  }mysql_close($con);?>

More Related