0

i am using some php and ajax to make a call to a database and i get this error: Warning: mysql_query() [function.mysql-query]: A link to the server could not be established... and Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

now from what i read looks like i need to create a database connection first, and the problem is that i am.

I am using require_once ('db_connect.php'); at the begining:

<?php
define("HOST", "localhost");
define("DBUSER", "123");
define("PASS", "123");
define("DB", "123");
$prefix = "";
############## Make the mysql connection ###########
$conn = mysql_connect(HOST, DBUSER, PASS) or  die('Could not connect !<br />Please contact the site\'s administrator.');
$db = mysql_select_db(DB) or  die('Could not connect to database !<br />Please contact the site\'s administrator.');
?>

My script looks something like this (ignore any missing or broken html):

<?php
session_start();
require_once ('db_connect.php'); // include the database connection 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml" xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<html>
<head>

</head>
<body>
<div id="wrap">
<script type="text/javascript">
$(function() {
$(".submit").click(function() {
var name = $("#name").val();
var com_type = 1;
var email = $("#email").val();
var comment = $("#comment").val();
    var post_id = $("#post_id").val();
var dataString = 'name='+ name + '&email=' + email + '&comment=' + comment + '&post_id=' + post_id;

if(name=='' || email=='' || comment=='')
 {
alert('Please Give Valide Details');
 }
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" align="absmiddle">&nbsp;<span class="loading">Loading Comment...</span>');
$.ajax({
    type: "POST",
url: "commentajax.php",
data: dataString,
cache: false,
success: function(html){

$("ol#update").append(html);
$("ol#update li:last").fadeIn("slow");
document.getElementById('email').value='';
document.getElementById('name').value='';
document.getElementById('comment').value='';
$("#name").focus();

$("#flash").hide();

}
});
}
return false;
});
});
</script>
<div id="main">
<ol  id="update" class="timeline">
<?php
$sql=mysql_query("select * from comments where post_id_fk='$post_id'");
while($row=mysql_fetch_array($sql))
{
$name=$row['com_name'];
$com_type=$row['com_type'];
$email=$row['com_email'];
$comment_dis=$row['com_dis'];

$lowercase = strtolower($email);
$image = md5( $lowercase );
?>
<li class="box">
<img src="http://www.gravatar.com/avatar.php?gravatar_id=<?php echo $image; ?>" class="com_img">
<span class="com_name"> <?php echo $name; ?><?php echo $com_type; ?></span> <br />My Comment</li>
<?php
}
?>
</ol>
<div id="flash" align="left"  ></div>
<div style="margin-left:100px">
<form action="#" method="post">
<input type="hidden" name="post_id" id="post_id" value="<?php echo $post_id; ?>"/>
<input type="text" name="title" id="name"/><span class="titles">Name</span><span class="star">*</span><br />
<input type="text" name="email" id="email"/><span class="titles">Email</span><span class="star">*</span><br />
<textarea name="comment" id="comment"></textarea><br />
<input type="submit" class="submit" value=" Submit Comment " />
</form>
</div>
</div>
</div>
</body>
</html>

the connectajax.php :

<?php

if($_POST)
{
$name=$_POST['name'];
$name=mysql_real_escape_string($name);
$com_type=$_POST['com_type'];
$name=mysql_real_escape_string($com_type);
$email=$_POST['email'];
$email=mysql_real_escape_string($email);
$comment=$_POST['comment'];
$comment=mysql_real_escape_string($comment);
$post_id=$_POST['post_id'];
$post_id=mysql_real_escape_string($post_id);
$lowercase = strtolower($email);
$image = md5( $lowercase );
mysql_query("insert into comment(com_name,com_type,come_email,com_dis) values ('$name','$com_type','$email','$comment_dis','$post_id')");
}

?>

<li class="box">
<img src="http://www.gravatar.com/avatar.php?gravatar_id=
<?php echo $image; ?>"/>
<?php echo $name;?><br />
<?php echo $comment; ?>
</li>

any idea? thanks

2
  • are you running mysql server on the same server as the webserver? if so, what OS are you running? If linux you could try 'netstat -tap' (without the quotes) on the command line and will show what ports are being listened on (look for mysqld somewhere in that list) Commented Jun 27, 2011 at 3:39
  • If this is answered, please mark it as such. Commented Jun 27, 2011 at 3:52

3 Answers 3

3

You didn't require_once the db_connect.php script in your connectajax.php script (unless you omitted that line when copying the code in).

Sign up to request clarification or add additional context in comments.

1 Comment

It's the simple things that will get you every time :)
0

Sounds to me as though your mysql service is down. Try reloading it. If that doesn't work, maybe try connecting to a mysql database on another server, to see if maybe it's not the mysql service's fault.

1 Comment

nope, the connection works, i know this because i have a login system and it works
0

Check if you have given proper access rights to Mysql database... that will resolve this issue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.