• Resolved pmarks906d

    (@pmarks906d)


    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, 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)
  • 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/

    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

    (@pmarks906d)

    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

    (@pmarks906d)

    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.