This topic describes how to use PHP to connect to and use LindormTable.
Prerequisites
The MySQL compatibility feature is enabled for the instance. For more information, see Enable the MySQL compatibility feature.
The IP address of your client is added to the whitelist of your Lindorm instance. For more information, see Configure whitelists.
Procedure
Install the php-mysql module. For more information, see Installation.
Configure connection parameters.
$lindorm_addr="ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com"; $lindorm_username="user"; $lindorm_password="test"; $lindorm_database="default"; $lindorm_port=33060;
Parameters
Parameter
Description
$lindorm_addr
The LindormTable endpoint for MySQL. For more information about how to obtain the endpoint, see View endpoints.
ImportantIf your application is deployed on an ECS instance, we recommend that you use a VPC to connect to the Lindorm instance to ensure higher security and lower network latency.
If your application is deployed on a local server and needs to connect to the Lindorm instance over the Internet, you can perform the following steps to enable the Internet endpoint for the instance in the Lindorm console: In the left-side navigation pane, select
. On the Wide Table Engine tab, click Enable Public Endpoint.If you use a VPC to access the Lindorm instance, specify the LindormTable VPC endpoint for MySQL in the value of $lindorm_addr. If you use the Internet to access the Lindorm instance, specify the LindormTable Internet endpoint for MySQL in the value of $lindorm_addr.
$lindorm_username
If you forget your password, you can change the password in the cluster management system of LindormTable. For more information, see Manage users.
$lindorm_password
$lindorm_database
The name of the database to which you want to connect. By default, your client is connected to a database named default.
$lindorm_port
The port used to access LindormTable by using MySQL. The value of this parameter is fixed to 33060.
Establish a connection and use LindormTable SQL to perform operations in LindormTable. The following code block provides an example on how to use LindormTable SQL to create a table:
// Connect to LindormTable. $lindorm_conn=mysqli_connect($lindorm_addr,$lindorm_username,$lindorm_password,$lindorm_database, $lindorm_port); if (!$lindorm_conn) { printf("Can't connect to Lindorm Server. Errorcode: %s \n",mysqli_error($lindorm_conn)); exit; } else { printf("connect to Lindorm successfully\n"); } // Create a table. $create_table="create table if not exists user_test(id int, name varchar,age int, primary key(id))"; if (mysqli_query($lindorm_conn, $create_table)) { printf("create table successfully\n"); } else { printf("create table error: %s \n", mysqli_error($lindorm_conn)); exit; }
Sample code
<?php
// Specify the LindormTable endpoint for MySQL.
$lindorm_addr="ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com";
// Specify the username used to connect to LindormTable.
$lindorm_username="user";
// Specify the password used to connect to LindormTable.
$lindorm_password="test";
// Specify the database to which you want to connect.
$lindorm_database="default";
// Specify the port used to access LindormTable by using MySQL.
$lindorm_port=33060;
// Connect to LindormTable.
$lindorm_conn=mysqli_connect($lindorm_addr,$lindorm_username,$lindorm_password,$lindorm_database, $lindorm_port);
if (!$lindorm_conn) {
printf("Can't connect to Lindorm Server. Errorcode: %s \n",mysqli_error($lindorm_conn));
exit;
} else {
printf("connect to Lindorm successfully\n");
}
// Create a table.
$create_table="create table if not exists user_test(id int, name varchar,age int, primary key(id))";
if (mysqli_query($lindorm_conn, $create_table)) {
printf("create table successfully\n");
} else {
printf("create table error: %s \n", mysqli_error($lindorm_conn));
exit;
}
// Insert data to the table.
$insert_sql="upsert into user_test(id,name,age) values(1,'zhangsan',17)";
if (mysqli_query($lindorm_conn, $insert_sql)) {
printf("insert data successfully\n");
} else {
printf("insert data error: %s \n", mysqli_error($lindorm_conn));
exit;
}
// Query data in the table.
$select_sql="select * from user_test";
$result=mysqli_query($lindorm_conn, $select_sql);
while($row = mysqli_fetch_array($result)) {
printf("id %d\n", $row["id"]);
printf("name %s\n", $row["name"]);
printf("age %d\n", $row["age"]);
}
?>
The following result is returned:
connect to Lindorm successfully
create table successfully
insert data successfully
id 1
name zhangsan
age 17