This topic describes how to connect to and use LindormTable with PHP.
Prerequisites
PHP 8.0 or later must be installed.
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 a whitelist.
Procedure
Install the php-mysql module. For more information, see PHP 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;Parameter descriptions
Parameter
Description
$lindorm_addr
The MySQL-compatible endpoint for LindormTable. To obtain the endpoint, see View connection addresses.
ImportantIf your application is deployed on an ECS instance, connect to the Lindorm instance over a virtual private cloud (VPC) for higher security and lower network latency.
If your application is deployed locally and connects to the Lindorm instance over the public network, you must first enable the public endpoint in the console. In the navigation pane on the left, choose . On the Wide Table Engine tab, click Enable Public Endpoint.
If you use a VPC to access the Lindorm instance, set $lindorm_addr to the MySQL-compatible VPC address. If you use the public network to access the Lindorm instance, set $lindorm_addr to the MySQL-compatible Internet address.
$lindorm_username
If you forget the user password, you can change it in the cluster management system of LindormTable. For more information, see Change a user password.
$lindorm_password
$lindorm_database
The name of the database to connect to. The default database is `default`.
$lindorm_port
The port for the MySQL protocol of LindormTable. The port number is fixed at 33060.
Create a connection and use Lindorm SQL to interact with LindormTable. The following example shows how 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; }
Full example
<?php
// The MySQL-compatible endpoint for LindormTable.
$lindorm_addr="ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com";
// The username for the LindormTable connection.
$lindorm_username="user";
// The password for the LindormTable connection.
$lindorm_password="test";
// The name of the database to connect to.
$lindorm_database="default";
// The port for the MySQL protocol of LindormTable.
$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.
$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.
$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