php, connecting to database using mysql_connect() problem, how to?
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"> <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
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).
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.
Check if you have given proper access rights to Mysql database... that will resolve this issue
精彩评论