Just started to learn MySQL and PHP, in this record to learn the little bit, but also hope to share with you some of the knowledge learned.
1.PHP connect to the MySQL database, you can connect to the database by the following methods (if your environment is already set up, of course)
$host = "localhost";
$user = "root";
$password = "123456";
$database = "Lyz";
$port = 3306;
$connection = new Mysqli ($host, $user, $password, $database, $port);
2. View the connection results, and use the Mysqli_connect_errno () function to see if there is an error returning the connection process.
if (Mysqli_connect_errno ()) {
echo "<p> connection Failed". Mysqli_connect_error (). " </p>\n ";
} else {
echo "<p> connection success </p>\n";
}
3. Execute SQL statements
After the database is connected, the database should be queried, modified and other related operations. The following statement queries a database table test.
$result = $connection->query ("select* from Test");
Use the function fetch_fields () to get the field of the table and look directly at the code
$num = $result->field_count;
$info = $result->fetch_fields ();
echo "<p>table name is:" $info [0]->table.] </p> ";
for ($i = 0; $i < $num; $i + +) {
echo $info [$i]->name.] \ t ";
}
4. Finally, we can output the contents of the table test.
$rs = $result->fetch_row ();
while ($rs) {
echo "<p>". $rs [0]. " \ t ". $rs [1]." </p> ";
$rs = $result->fetch_row ();
}
if ($result) {
echo "<p> record number:" $result->num_rows. " </p> ";
echo "<p> Number of fields:" $result->field_count. " </p> ";
}
$result->close ();
5. Inserting data into the table test
$sql = "INSERT INTO Test (b, a) VALUES (?,?)";
$stmt = $connection->prepare ($sql);
$BV = 2;
$av = ' a ';
$stmt->bind_param ("is", $BV, $av);
$stmt->execute ();
$stmt->close ();
$connection->close ();
OK, the above is the basic operation of MySQL table test--Query and add function.
mysql&php Learning Log