Hi,

I have a database program that creates URL's that look like this...

http://www.mywebsiteDB.com/admin.php?toDo=module&module=RESERVATIONS&modsub=reservations

or this

http://www.mywebsiteDB.com/admin.php?toDo=module&module=RESERVATIONS&modsub=upcoming

etc.

I need to make a page that inserts fields into the database AJAX style, which I managed to do outside of the database program. However, when I put the code into a database page, I get this error...

Could not connect: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2).

This is not the result of a misconfigured php file, as the database works fine otherwise.

From what I can tell, the error is the result of some kind of two XMLHttpRequests running into each other.

The one that the database uses and the new one that I am introducing into the page.

So, my question is, what code do i need to use to make the two requests work in harmony. I thought of trying to trap the database request in a variable, then set the XMLHttpRequest to "", then after the new request, set the variable back to the orignal, but that did not work. Here is the code I am using to create the XMLHttpRequest object...

function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();

I did not write the code to the database, i am not that skilled yet, and everything I know about programming is self taught. So my knowledge is limited.

Thank you!

Recommended Answers

All 4 Replies

I don't think that error indicates the crash of 2 simultaneous call of query. It looks to me that the error is from a certain service from your server is not running. As a result, there is no listener from your database server to handle the request and that "couldn't connect via socket" error is given. What kind of server are you using to handle the request?

By the way, the queries you are calling are to read, write, or both read and write?

Okay, the code consists of these four files below...

config.php

<?php

define(_PATH, dirname($_SERVER['SCRIPT_FILENAME'])."/");    // Path : Change this to the absolute path of your website (Include trailing slash)
define(_URL, "http://".str_replace("//", "/",$_SERVER['SERVER_NAME']."/".dirname($_SERVER['PHP_SELF'])."/")); // URL : Change this to the absolute URL or your website (Include trailing slash)

define(_PATHCONTENT, _PATH);        // PATHCONTENT : Do not change this (Deprecated) 
define(_URLCONTENT, _URL);      // URLCONTENT : Do not change this (Deprecated) 
define(_LOG, 1);            // LOG: Whether or not to log access to 

$db['HOST']                 = "mysql4.hostica.com:3306";
$db['USER']                 = "riptide_root";
$db['PASS']                 = "xxxxxxxxxx";
$db['NAME']                 = "riptide_inquire";

$CONFIG['domain']           = $_SERVER['HTTP_HOST']; // The domain of your site (without the www. or http://) (Recommended $_SERVER['HTTP_HOST'])
$CONFIG['executablesdir']   = _PATH."admin_includes/admin_system/executables/"; 
$CONFIG['language']         = "en"; // The default Language (ISO 639-1 Format)
$CONFIG['skin']             = "default"; // The default skin used in the CMS. 
$CONFIG['modules']          = 1; // If modules should be enabled (Recommended to always be 1).
$CONFIG['sessionLength']    = "69200"; // Session Lenght in seconds.

$USERS                      = file(_PATH."admin_includes/config/users.php"); // Location of user file


// Load dependencies
@include_once($CONFIG['executablesdir'].'debug.inc.php');
@include_once($CONFIG['executablesdir'].'XMLParser.php');
@include_once($CONFIG['executablesdir'].'database.inc.php');
@include_once($CONFIG['executablesdir'].'sendEmail.php');

$connection = mysql_connect($db['HOST'], $db['USER'], $db['PASS']);
    if (!$connection) {
    die("Database Connection Failed: " . mysql_error());

}

$db_select = mysql_select_db($db['NAME'],$connection); 
    if (!$db_select) {
    die("Database Selection Failed: " . mysql_error());
}

?>

start.php

<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title></title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<meta name='robots' content='noindex, nofollow' />
</head>
<body>

<?php
echo "<script src=\"ajax_framework.js\" language=\"javascript\"></script>";

echo "<form action=\"javascript:insert()\" method=\"post\">";
echo "<div id=\"insert_response\"></div>";

echo "<div>";
echo "<form action=\"javascript:insert()\" method=\"post\">";
echo "<input name=\"aptmnt\" type=\"text\" id=\"aptmnt\" value=\"\"/><br /><br />";
echo "<input name=\"extra\" type=\"text\" id=\"extra\" value=\"\"/><br /><br />";
echo "<input type=\"submit\" name=\"Submit\" value=\"Insert\"/><br />";
echo "</form>";
echo "</div>";
?>

</body>
</html>

insert.php

<?php include('config.php'); ?>
<?php
$connection = mysql_connect($db['HOST'], $db['USER'], $db['PASS']);
    if (!$connection) {
    die("Database Connection Failed: " . mysql_error());    
}
$db_select = mysql_select_db($db['NAME'],$connection); 
    if (!$db_select) {
    die("Database Selection Failed: " . mysql_error());
}
$aptVar= $_GET['aptmnt'];
$extVar= $_GET['extra'];
$insertSite_sql = "INSERT INTO `riptide_inquire`.`tbl_aptQuote` (`aq_ID`, `aptmnt`, `extra`) VALUES (NULL, '".$aptVar."', '".$extVar."')"; 
$sql = mysql_query($insertSite_sql) or die(mysql_error());
echo $extVar;
?>

ajax_framework.js

/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();

/* -------------------------- */
/* INSERT */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Just a second..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var aptmnt= encodeURI(document.getElementById('aptmnt').value);
var extra = encodeURI(document.getElementById('extra').value);
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'insert.php?aptmnt='+aptmnt+'&extra=' +extra+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
function insertReply() {
if(http.readyState == 4){
var response = http.responseText;
// else if login is ok show a message: "Site added+ site URL".
document.getElementById('insert_response').innerHTML = 'Response:'+response;
}
}

When i put these four files in a seperate directory and submitt the form on start.php, the code works. But, if I put the form code from start.php inside an existing, working database page, with the same 3 files from the other directory.the code from the existing page still works fine (queries, inserts and updates). but i get the connection error when running the AJAX code on that same page.

I can't figure out why. Thanks for helping me out!

I am using a standard hosting database. Is there some advanced option I should enable?

Click Here

Click above for screenshot. Existing batabase fields filled in from query. But bottom form has the error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.