Home  >  Article  >  Backend Development  >  Head First PHP&MySQL study notes (2)

Head First PHP&MySQL study notes (2)

WBOY
WBOYOriginal
2016-08-08 09:31:071074browse

3. Create and populate the database

1. A web application is a dynamic website designed to meet a specific goal of the user

2. In PHP code, SQL statements do not need to end with a semicolon; while MySqL A semicolon must be added to the end of each SQL statement in the terminal

3. Create a database: CREATE DATABASE database_name

Create a table: CREATE TABLE table_name(column_name1 column_type1,column_name2 column_type2,…)

Select default database: USE database_name

Show the structure of the table: DESCRIBE table_name

Delete table: DROP TABLE table_name

Delete data: DELETE FROM table_name

Use the where clause to specify the range

4. There may be objections to how to express yes/no values ​​in MySQL. The char(1) method is very straightforward. Very efficient

5. The -> prompt indicates that you are still entering the same statement. After the statement ends and a semicolon is added, MySQL will execute the statement

6. The mysqli_fetch_array() function stores a data row in an array

7. Code example

<?php
  // addemail.php

  $dbc = mysqli_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;&#39;, &#39;elvis_store&#39;)
    or die(&#39;Error connecting to MySQL server.&#39;);

  $first_name = $_POST[&#39;firstname&#39;];  // 从前台获取数据
  $last_name = $_POST[&#39;lastname&#39;];
  $email = $_POST[&#39;email&#39;];

  $query = "INSERT INTO email_list (first_name, last_name, email)  VALUES (&#39;$first_name&#39;, &#39;$last_name&#39;, &#39;$email&#39;)";
  mysqli_query($dbc, $query)          // 执行SQL语句
    or die(&#39;Error querying database.&#39;);

  echo &#39;Customer added.&#39; . $email;

  mysqli_close($dbc);
?>
<pre name="code" class="php"><?php
&#160;&#160;// sendemail.php
  $from = &#39;jarray@foxmail.com&#39;;
&#160; $subject = $_POST[&#39;subject&#39;];
&#160; $text = $_POST[&#39;elvismail&#39;];

&#160; $dbc = mysqli_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;&#39;, &#39;elvis_store&#39;)
&#160; &#160; or die(&#39;Error connecting to MySQL server.&#39;);

&#160; $query = "SELECT * FROM email_list";
&#160; $result = mysqli_query($dbc, $query)            // 执行SQL语句
&#160; &#160; or die(&#39;Error querying database.&#39;);

&#160; while ($row = mysqli_fetch_array($result)){     // while循环条件是mysqli_fetch_array()函数的返回值
&#160; &#160; $to = $row[&#39;email&#39;];
&#160; &#160; $first_name = $row[&#39;first_name&#39;];
&#160; &#160; $last_name = $row[&#39;last_name&#39;];
&#160; &#160; $msg = "Dear $first_name $last_name,\n$text";
&#160; &#160; mail($to, $subject, $msg, &#39;From:&#39; . $from);
&#160; &#160; echo &#39;Email sent to: &#39; . $to . &#39;<br />';
  } 

  mysqli_close($dbc);
?>


<?php
  // removeemail.php
&#160; 
&#160; $dbc = mysqli_connect(&#39;localhost&#39;, &#39;root&#39;, &#39;&#39;, &#39;elvis_store&#39;)
    or die(&#39;Error connecting to MySQL server.&#39;);

  $email = $_POST[&#39;email&#39;];

  $query = "DELETE FROM email_list WHERE email = &#39;$email&#39;";   // 删除数据库中指定条件的邮件
  mysqli_query($dbc, $query)
    or die(&#39;Error querying database.&#39;);

  echo &#39;Customer removed: &#39; . $email;

  mysqli_close($dbc);
?>


The above introduces the Head First PHP&MySQL study notes (2), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn