mysql&php Learning Log

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.