PHP Mysql
PHP Mysql
22/12/1 2
Select a database, issue queries
Once you have connected successfully, you
should select the database you will use.
Then you can issue queries to the database.
<?php
mysql_select_db("your DB name", $connection)
or die("Could not select database");
$result = mysql_query("select * from
some_table"); // No ; at the end
or die("Could not issue MySQL query");
?>
Now the variable $result holds the query
resultset.
22/12/1 3
Table of books
<?php
$result = mysql_query("select book_name, publish_year from books");
if (mysql_num_rows($result) == 0) {
print("No results matching your query<BR>\n");
} else {
print("The Books:<br />\n");
while ($row = mysql_fetch_assoc($result)) {
$book_name = $row["book_name"];
$publish_year = $row["publish_year"];
print("Book name: $book_name, Publish year: $publish_year\n");
}
}
?>
22/12/1 5
HTML forms and PHP variables
Create a HTML form:
<form action="add_book.php" method="POST">
<input type="text" name="book_name">
<input type="text" name="publish_year">
<input type="submit" value="Add Book">
</form>
22/12/1 7