Title: Establish Database connection
Last modified: June 10, 2018

---

# Establish Database connection

 *  [shreeupadhyaya](https://wordpress.org/support/users/shreeupadhyaya/)
 * (@shreeupadhyaya)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/establish-database-connection/)
 * I am trying to insert form submissions into my custom table in my wordpress site.
   But I am not able to establish connection to database.In my functions.php –
 *     ```
       	$servername = "localhost";
       	$username = "xyz";
       	$password = "xyz";
       	$dbname = "xyz";
   
       	// Create connection
       	$conn = new mysqli($servername, $username, $password, $dbname);
   
       	// Check connection
       	if (!$con) {
       	    return new WP_Error("database_error",__("No Connection: ". $conn->connect_error,'feasible_check'));
       	} 
       ```
   
 * Below is link to page with form, on filling this and submit, insert action to
   DB is performed.
    [Form](http://vineeticonsultants.in/vthome_wp/sample-page/)
 * I will get ‘No Connection:’ response.I have verified username/password/dbname
   is intact. I donot want to use any plugins.
 * I have also tried using
    `$conn = new PDO("mysql:host=$servername;dbname=$dbname",
   $username, $password);` instead of mysqli, even with that approach i am not able
   to connect.
 * I have tried using wpdb , but since this is custom table it is not listed in
   `
   $wpdb->tables`

Viewing 1 replies (of 1 total)

 *  [Joey](https://wordpress.org/support/users/leglesslizard/)
 * (@leglesslizard)
 * [7 years, 11 months ago](https://wordpress.org/support/topic/establish-database-connection/#post-10383657)
 * Hi,
 * I would always go the $wpdb route. Did you check out the [codex](https://codex.wordpress.org/Class_Reference/wpdb#Tables)?
 * > The $wpdb object is not limited to the default tables created by WordPress;
   > it can be used to read data from any table in the WordPress database (such 
   > as custom plugin tables). For example to SELECT some information from a custom
   > table called “mytable”, you can do the following.
 * `$myrows = $wpdb->get_results( "SELECT id, name FROM mytable" );`

Viewing 1 replies (of 1 total)

The topic ‘Establish Database connection’ is closed to new replies.

## Tags

 * [db connection](https://wordpress.org/support/topic-tag/db-connection/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Joey](https://wordpress.org/support/users/leglesslizard/)
 * Last activity: [7 years, 11 months ago](https://wordpress.org/support/topic/establish-database-connection/#post-10383657)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
