23.03.2017 Views

wilamowski-b-m-irwin-j-d-industrial-communication-systems-2011

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

62-12 Industrial Communication Systems<br />

62.5.5 Insert, Update and Delete queries<br />

To insert data into a database table, use INSERT INTO Statement.<br />

Example:<br />

mysql_query("INSERT INTO employeeinfo (LastName, FirstName,<br />

Department, Employee_ID)<br />

VALUES ('Rives', 'John', 'sales', '2')");<br />

$_POST is used to collect data from a form and if you substitute it in VALUES, parameters of a form<br />

can be inserted into a table.<br />

To update existing records in a table, use UPDATE statement.<br />

Example:<br />

mysql_query("UPDATE employeeinfo SET LastName= 'Rives' WHERE<br />

FirstName = 'John' AND Employee_ID = '2' ");<br />

or<br />

mysql_query("UPDATE employeeinfo SET LastName= 'Rives' ".<br />

"WHERE FirstName = 'John' AND Employee_ID = '2' ");<br />

To delete existing records, use DELETE statement,<br />

mysql_query("DELETE FROM employeeinfo WHERE LastName='Tent' ");<br />

62.5.5.1 Select Query<br />

To select records from database, we use the SELECT statement.<br />

Example:<br />

To select all the records from the table employeeinfo<br />

$result = mysql_query("SELECT * FROM employeeinfo");<br />

The data returned by mysql_query is stored in the variable $result.<br />

Now we use mysql_fetch_array() function to return to the first row of the table. And each time a call<br />

to mysql_fetch_array() returns the next row in the table.<br />

Example:<br />

while($row = mysql_fetch_array($result) )<br />

{<br />

echo $row['LastName']. " ". $row['FirstName']. " ".<br />

$row['Department']. " ". $row['Employee_ID'];<br />

echo "";<br />

}<br />

Output:<br />

Rives John sales 2<br />

Tent Tom accounts 1<br />

Hold Tim technical 5<br />

© <strong>2011</strong> by Taylor and Francis Group, LLC

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!