Title: Connecting MySQL with php code in WordPress
Last modified: August 30, 2016

---

# Connecting MySQL with php code in WordPress

 *  [plet9090](https://wordpress.org/support/users/plet9090/)
 * (@plet9090)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/)
 * I am trying to follow this tutorial:
    [https://developers.google.com/maps/articles/phpsqlinfo_v3](https://developers.google.com/maps/articles/phpsqlinfo_v3)
 * I have the map placed on my page, but I am trying to figure out where I need 
   to place the .php code that inserts a row into my database? Also, where do I 
   place the code that includes my username, password, and database name?
 * I tried placing these as separate php files in my themes directory, but this 
   did not work.
 * Any suggestions?
 * Thanks!

Viewing 10 replies - 1 through 10 (of 10 total)

 *  [juggledad](https://wordpress.org/support/users/juggledad/)
 * (@juggledad)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/#post-6810437)
 * If you are doing this from with in WP you need to use the WP API and WP will 
   hndle the calls. Here is an example I used (in a plugin I was using to track 
   milage I walked on a virtual Applicaian Trail hike) to insert a row in a table
   I created in the WP database
 *     ```
       //Submits data to the MYSQL database table wp_at_hike
   
       	if (isset($_POST['Submit'])) {
       		global $wpdb, $current_user;
       		get_currentuserinfo();
       		$trail_name = $current_user->display_name;
   
       		$year = mysql_real_escape_string($_POST['yeardata']);
       	    $month = mysql_real_escape_string($_POST['monthdata']);
       	    $day = mysql_real_escape_string($_POST['daydata']);
       	    $time = 0;
       	    $distance = mysql_real_escape_string($_POST['distancedata']);
       	    $sql = "INSERT INTO wp_at_hike (trail_name,year,month,day,time,distance) VALUES ('$trail_name','$year','$month','$day','$time','$distance')"; 
   
       		if ($wpdb->query($sql) === FALSE) {
       			echo "Error!";
       		} else {
       			echo "Your entry was successfully added to your Trail Log.";
       		}
       	};
       ```
   
 *  Thread Starter [plet9090](https://wordpress.org/support/users/plet9090/)
 * (@plet9090)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/#post-6810472)
 * If I wanted to do a google maps API that when you clicked on it, it added a new
   infowindow that can be saved into MySQL (map already created [http://www.tothenationsworldwide.com/social-travel-map/](http://www.tothenationsworldwide.com/social-travel-map/))
 * Where would I include this code you just showed me? into my functions.php?
 *  Thread Starter [plet9090](https://wordpress.org/support/users/plet9090/)
 * (@plet9090)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/#post-6810473)
 * this code:
 * <?php
    require(“phpsqlinfo_dbinfo.php”);
 * // Gets data from URL parameters
    $name = $_GET[‘name’]; $address = $_GET[‘address’];
   $lat = $_GET[‘lat’]; $lng = $_GET[‘lng’]; $type = $_GET[‘type’];
 * // Opens a connection to a MySQL server
    $connection=mysql_connect (“localhost”,
   $username, $password); if (!$connection) { die(‘Not connected : ‘ . mysql_error());}
 * // Set the active MySQL database
    $db_selected = mysql_select_db($database, $
   connection); if (!$db_selected) { die (‘Can\’t use db : ‘ . mysql_error()); }
 * // Insert new row with user data
    $query = sprintf(“INSERT INTO markers ” . ”(
   id, name, address, lat, lng, type ) ” . ” VALUES (NULL, ‘%s’, ‘%s’, ‘%s’, ‘%s’,‘%
   s’);”, mysql_real_escape_string($name), mysql_real_escape_string($address), mysql_real_escape_string(
   $lat), mysql_real_escape_string($lng), mysql_real_escape_string($type));
 * $result = mysql_query($query);
 * if (!$result) {
    die(‘Invalid query: ‘ . mysql_error()); }
 * ?>
 *  [juggledad](https://wordpress.org/support/users/juggledad/)
 * (@juggledad)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/#post-6810475)
 * You could make it a function and put it in functions.php and then you can use
   the function where you want.
 * Are you opening a new connection to a differentdatabase than th ewordpress database?
   if so, why? (I’m curious)
 *  [Digico Paris](https://wordpress.org/support/users/digico-paris/)
 * (@digico-paris)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/#post-6810476)
 * I suggest you learn Jquery, because doing a clickable map from API will probably
   involve quite a lot of Jquery <– in/out –> with php.
 * Not sure it answers your question,
 *  Thread Starter [plet9090](https://wordpress.org/support/users/plet9090/)
 * (@plet9090)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/#post-6810482)
 * I have added the table to my database on myphpadmin with my host. I just want
   to connect the map to the table so everytime someone clicks and saves, it adds
   a new row to the table and saves the information.
 *  Thread Starter [plet9090](https://wordpress.org/support/users/plet9090/)
 * (@plet9090)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/#post-6810489)
 * The map is already completed, I have that done at [http://www.tothenationsworldwide.com/social-travel-map/](http://www.tothenationsworldwide.com/social-travel-map/)
 *  [juggledad](https://wordpress.org/support/users/juggledad/)
 * (@juggledad)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/#post-6810498)
 * So is the database the one defined for wordpress or a seperate database? If it
   is the same one, use the wordpress API to add things to your new table.
 *  Thread Starter [plet9090](https://wordpress.org/support/users/plet9090/)
 * (@plet9090)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/#post-6810514)
 * It is the same database. Sorry for my confusion. I added a table in the same 
   database and want users to be able to add information to the table when clicking
   on the map and saving.
    How do I go about this?
 *  [juggledad](https://wordpress.org/support/users/juggledad/)
 * (@juggledad)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/#post-6810522)
 * Look at my example again – you don’t want to give each user access to the database,
   the wordpress user already has that access. I would suggest that when they click
   on the map, you have code check that they are logged in so you can grab their
   name fron their user row in the wp-suers table, then do an insert into your new
   table with th einformation you want to store it in.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Connecting MySQL with php code in WordPress’ is closed to new replies.

## Tags

 * [google maps api](https://wordpress.org/support/topic-tag/google-maps-api/)
 * [MySQL](https://wordpress.org/support/topic-tag/mysql/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 3 participants
 * Last reply from: [juggledad](https://wordpress.org/support/users/juggledad/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/connecting-mysql-with-php-code-in-wordpress/#post-6810522)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
