Title: Accessing MySql database from page  function
Last modified: August 22, 2016

---

# Accessing MySql database from page function

 *  Resolved [pmarks906d](https://wordpress.org/support/users/pmarks906d/)
 * (@pmarks906d)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/accessing-mysql-database-from-page-function/)
 * Hi. I am new to wordpress. I have made an installation on Bluehost, and have 
   set up a MySql database with a user and with a table. I have edited the wp-config.
   php file, under the “public_html” directory, to point to it. I have created a
   page and put a form on it with some <script> functions. What I have not been 
   able to do is to put code in one of the <script> functions to interact with the
   MySql database. I have looked at the $wpdb article but have not been able to 
   come up with any working syntax to do any connecting, row insertion, selection,
   or any querying at all. No matter what I try, the page just stops doing the other
   input and output stuff which it was doing before I tried to put in some database
   code. I think that what i need is something that describes some exact syntax 
   and complete example about how to put that code into a <script> function. On 
   the other hand, maybe there is somewhere else the database code needs to go which
   I don’t know about. I sure could use a good example and some further direction.
   My domain is [http://www.mysimplesurvey.com](http://www.mysimplesurvey.com), 
   but i don’t think the page i am working on is visible yet. Thanks in advance —
   Phil Marks.

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

 *  [leejosepho](https://wordpress.org/support/users/leejosepho/)
 * (@leejosepho)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/accessing-mysql-database-from-page-function/#post-5434096)
 * I had once mistakenly had one multitasking MySQL user handing three separate 
   sites and their respective databases all at once, but I would think you would
   need some kind of router to have multiple users doing simultaneous traffic with
   a common database. Too many cooks stirring one pot, you know? I do know it is
   possible (and I have this) to have your own phpMyAdmin installed in a subfolder
   at your site, so maybe do that and then do some hard-testing just to see whether
   you can break something before going any farther.
 * [http://kb.siteground.com/how_to_access_phpmyadmin_directly_from_my_domain/](http://kb.siteground.com/how_to_access_phpmyadmin_directly_from_my_domain/)
 * note: you will see a couple of messages at the bottom of the screen. One goes
   away by making a config file from the sample file provided, and the other can
   go away by adding a database for some whistles and bells.
 *  Thread Starter [pmarks906d](https://wordpress.org/support/users/pmarks906d/)
 * (@pmarks906d)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/accessing-mysql-database-from-page-function/#post-5434282)
 * Hi, Thankyou for your post. I’ve taken your advice and installed phpmyadmin as
   described. In the process I also put my code in a php theme file, which interprets
   PHP code correctly; then i chose that theme for my page. Again, however, everything
   still works great except when I try to access my MySql database. I have the following
   code in that theme file. The problem is the function never gets beyond the $wpdb
   part.
 * <script>
    function registerFunction() { elEmailOut = document.getElementById(“
   emailReadId”);
 *  // this text box displays the right stuff if $wpdb commented out
    elEmailOut.
   value = “got to here1”;
 *  // execution gets here if i comment out the $wpdb stuff
 *  <?php
    global $wpdb; $wpdb->insert(‘myCustomers’, array( ‘FirstName’ => ‘Fred’,‘
   SecondName’ => ‘Marks’)); ?>
 *  // execution never reaches here.
    elEmailOut.value = “got to here2”; /** // 
   more code here but commented out */ } </script>
 *  Thread Starter [pmarks906d](https://wordpress.org/support/users/pmarks906d/)
 * (@pmarks906d)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/accessing-mysql-database-from-page-function/#post-5434316)
 * Hi, I think I have gotten on track.I found a solution on line. If I put a form
   on my theme page, and have the submit button post from the local machine to the
   server to another php file, the following MySql code works inside that file. 
   I’ve still got to work on security, however the basics seems to be there:
    <html
   > <body> Welcome <?php echo $_GET[“name”]; ?> Your email address is: <?php echo
   $_GET[“email”]; ?> <?php // Create connection $conn = new mysqli($servername,
   $username, $password, $dbname); // Check connection if ($conn->connect_error){
   die(“Connection failed: ” . $conn->connect_error); } $sql = “INSERT INTO myCustomers(
   firstname, lastname) VALUES (‘Fred’, ‘Marks’)”; if ($conn->query($sql) === TRUE){
   echo “New record created successfully”; } else { echo “Error: ” . $sql . “” .
   $conn->error; } $conn->close(); ?> </body> </html>

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

The topic ‘Accessing MySql database from page function’ is closed to new replies.

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [MySQL](https://wordpress.org/support/topic-tag/mysql/)
 * [script](https://wordpress.org/support/topic-tag/script/)
 * [select](https://wordpress.org/support/topic-tag/select/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [pmarks906d](https://wordpress.org/support/users/pmarks906d/)
 * Last activity: [11 years, 7 months ago](https://wordpress.org/support/topic/accessing-mysql-database-from-page-function/#post-5434316)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
