• 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

    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

    (@leglesslizard)

    Hi,

    I would always go the $wpdb route. Did you check out the codex?

    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.