PHP and MySQL IT Engineering I Instructor: Ali B. Hashemi 1 1 Connect to a MySQL Before accessing your database, you must make a connection to it. mysql_connect() mysql_connect Syntax mysql_connect(server, user, password); 2 2 Connect to a MySQL Parameter server user password Description Optional. Specifies the server to connect to (can also include a port number. e.g. "hostname:port" or a path to a local socket for the localhost). Default value is "localhost:3306" Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process Optional. Specifies the password to log in with. Default is "" mysql_connect("localhost","root",'1234')) 3 3 Connect to a MySQL Example <?php $con=mysql_connect("localhost",“yourlogin","abc"); if (!$con) { // The "die" part of the code is an error check. die('Could not connect: ' . mysql_error()); } Echo “you are successfully connected with the mysql server”; // some code … //Closing a Connection mysql_close($con ); ?> 4 4 mysql_error() mysql_error(): Shows the error message that has been returned directly from the MySQL server. 5 5 Connect to a database <?php $con=mysql_connect("localhost",“yourlogin","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } $select = mysql_select_db ("mydb") //$select = mysql_select_db("my_db", $con); if(!$select){ die('Could not connect: ' . mysql_error()); } // remaining code here ?> 6 6 Inserting data <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } $select = mysql_select_db (“test"); if(!$select){ die('Could not connect: ' . mysql_error()); } $sql="INSERT INTO items VALUES ('testing', 'C','Yellow')"; if ($result = mysql_query ("$sql")) { echo "Data added\n"; } else error_report (); ?> 7 7 Inserting Data From a Form to a Database <form action="insert_db.php" method="POST"> Enter Item Name: <input type="text" name="itemname" /><br /> Enter Item Type: <input type="text" maxlength="1“ name="itemtype" /> <br> Enter Item Color <input type="text" name="itemcolor" /><br /> <input type="submit" /> </form> 8 8 Inserting data From Form <?php $sql="INSERT INTO items(itemname,itemtype,itemcolour) VALUES ('$_POST[itemname]','$_POST[itemtype]', '$_POST[itemcolor]')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } echo "Success!"; ?> 9 9 View Data <?php $result = mysql_query("SELECT * FROM items"); while($row = mysql_fetch_array($result)) { echo “Item name:”.$row['itemName'].”<br>"; echo “Item Type:”.$row['itemType'].”<br>"; echo “Item name:”.$row['itemcolour'].”<br>"; echo "<br>"; } ?> 10 10 mysql_fetch_array($results) mysql_fetch_array("results variable from query"): mysql_fetch_array Used to return several rows of the entire results of a database query. The mysql_fetch_array function gets the next line in an array from a MySQL result use the (while) while loop to continue to get the next array until there is no next array to get 11 11 Updating Data <?php $con=mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("UPDATE Person SET Age = ‘10’ WHERE LastName = 'Hashemi'"); ?> 12 12 Delete Data <?php $con=mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query(“delete FROM Person WHERE LastName = ‘Hashemi'"); ?> 13 13 mysql_affected_rows() The mysql_affected_rows() function returns the number of affected rows in the previous MySQL operation. changed by the most recent DELETE, INSERT, REPLACE, or UPDATE statement When returning rows, acts the same way as mysql_num_rows() returns the number of affected rows on success, or -1 if the last operation failed. Syntax: mysql_affected_rows(connection) Optional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used. 14 14 mysql_affected_rows() Example <?php $con=mysql_connect("localhost","mysql_user","mysql_pwd"); if (!$con) { die("Could not connect: " . mysql_error()); } mysql_select_db("mydb"); mysql_query("DELETE FROM mytable WHERE id < 5"); $rc = mysql_affected_rows(); echo "Records deleted: " . $rc; mysql_close($con); ?> The output of the code above could be: Records deleted:7 15 15 mysql_fetch_row() mysql_fetch_rows("results variable from query") Used to return a row of the entire results of a database query. The mysql_fetch_row() function returns a row from a recordset as a numeric array. array Parameter: results of a database query (could be mysql_query() function returns an array on success, or FALSE on failure or when there are no more rows. 16 16 mysql_fetch_row() Example <?php $result = mysql_query($sql,$con); print_r (mysql_fetch_row($result)); mysql_close($con); ?> Output: Array ( [0] => Refsnes [1] => Kai Jim [2]=> Taugata 2 [3] => 22 ) 17 17 mysql_field_name() The mysql_field_name() Returns the field name on success, or FALSE on failure. Syntax: mysql_field_name(data,field_offset) data Required. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function field_offsetRequired. Specifies which field to start returning. 0 indicates the first field 18 18 mysql_field_name() Example <?php $sql = "SELECT * from Person"; $result = mysql_query($sql,$con); $name = mysql_field_name($result, 0); echo $name; mysql_close($con); ?> 19 19 mysql_num_fields() The mysql_num_fields() returns the number of fields in a recordset. returns FALSE on failure. Syntax: mysql_num_fields(data) Data Required. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function 20 20 mysql_num_fields() Example <?php $sql = "SELECT * FROM person"; $result = mysql_query($sql,$con); echo mysql_num_fields($result); mysql_close($con); ?> 21 21 mysql_num_rows() The mysql_num_rows() returns the number of rows in a recordset. returns FALSE on failure. Syntax: mysql_num_rows(data) Data Required. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function 22 22 mysql_num_rows() Example <?php $sql = "SELECT * FROM person"; $result = mysql_query($sql,$con); echo mysql_num_rows($result); mysql_close($con); ?> 23 23