Jump to content

"mysql_query() [function.mysql-query]" Error...


zeeshan_haider000

Recommended Posts

Hi,

I am getting this error for some reasons:

 

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/.kernel/aaa/aaa/sitev2/includes/functions.php on line 57

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/.kernel/aaa/aaa/sitev2/includes/functions.php on line 57
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

 

Any idea what could be causing this error?

I am absolutely newbie to PHP

Link to comment
Share on other sites

did you call mysql_connect(); and mysql_select_db(); ?

 

<?php

if(mysql_connect($server, $username, $password))
{
  mysql_select_db($database);
}
else
{
   die('Cannot connect to database '. $database);
}
?>

 

These have to be called and the correct information needs to be passed to them to run a query.

 

Nate

 

Link to comment
Share on other sites

Code

 

Here's the code:

<?php
include('config.php');
function GetNohay(){

if (isset($_GET['reciter'])){
$reciter = $_GET['reciter'];

//CONNECT TO DATABASE! 

//**QUERY 

$t = mysql_query("SELECT * FROM nauhey WHERE Speaker ='".addslashes($reciter)."'"); (LINE: 57)
  if(!$t) die(mysql_error()); 

$a = mysql_fetch_object($t); 
$total_items = mysql_num_rows($t); 

if(isset($_GET['limit'], $_GET['page'])){
$limit = $_GET['limit']; 
$page = $_GET['page'];
}
else {
$limit = 10;
$page = 1;
}

//set default if: $limit is empty, non numerical, less than 10, greater than 50 
if((!$limit)  || (is_numeric($limit) == false) || ($limit < 10) || ($limit > 30)) { 
     $limit = 10; //default 
} 
//set default if: $page is empty, non numerical, less than zero, greater than total available 
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) { 
      $page = 1; //default 
} ?> 

 

Here's config.php:

<?php
define ("HOST", "localhost", false);
define ("USER", "admin", true);
define ("PASS", "xxxxx", true);
$database = "dbx";
$dbcx = mysql_connect(HOST, USER, PASS)
or die ("<h2>Could not connect to the database.</h2>");
$db = mysql_select_db($database, $dbcx)
or die ("Could not connect to the database\n".mysql_error);
?>

Link to comment
Share on other sites

What happens when you load config.php by itself. If your not getting your errors, then I am not sure what is up.

 

I am not sure if this actually matters, but try puttin your or die() statement on the same line as the connect lines... e.g.

 

<?php
define ("HOST", "localhost", false);
define ("USER", "admin", true);
define ("PASS", "xxxxx", true);
$database = "dbx";
$dbcx = mysql_connect(HOST, USER, PASS) or die ("<h2>Could not connect to the database.</h2>");
$db = mysql_select_db($database, $dbcx) or die ("Could not connect to the database\n".mysql_error);
?>

 

Is this server on a machine you run, or is it on your paid host? The only other thing I can think of is that the mysql service is not running.

 

But if that was the case, then you should trigger your or die clauses....

 

Try the 2 things I suggested and see what that does.

 

Nate

Link to comment
Share on other sites

Is this server on a machine you run, or is it on your paid host? The only other thing I can think of is that the mysql service is not running.

 

But if that was the case, then you should trigger your or die clauses....

 

Try the 2 things I suggested and see what that does.

 

Nate

 

Hey Nate,

I followed what you suggested but with no success. Well I ran this script on my local machine first and everything worked great, but then when I uploaded it to my paid host, it gives me the error for some reasons. :(

Link to comment
Share on other sites

Then check and ensure that your hostname is actually localhost. I have a paid host and my mysql hostname is something like db4428583.domain.com. The mysql server and web server are apparently not the same machine.

 

But if that was the case, then your or die() clause should be tossing an error at you. I am not sure..... I would suggest making sure that your hostname, username and password for the mysql server are all correct.

 

nate

Link to comment
Share on other sites

Then check and ensure that your hostname is actually localhost. I have a paid host and my mysql hostname is something like db4428583.domain.com. The mysql server and web server are apparently not the same machine.

 

But if that was the case, then your or die() clause should be tossing an error at you. I am not sure..... I would suggest making sure that your hostname, username and password for the mysql server are all correct.

 

nate

 

I got it working. I changed this:

<?php
include('config.php'); //THIS THING LOL
function GetNohay(){

if (isset($_GET['reciter'])){
$reciter = $_GET['reciter'];
   
//CONNECT TO DATABASE! ?>

 

Into:

<?php
function GetNohay(){
include('config.php'); //INSIDE OF THE FUNCTION
if (isset($_GET['reciter'])){
$reciter = $_GET['reciter'];
   
//CONNECT TO DATABASE! 

 

And i gotta do the same for every new function i create. This was so frustrating lol. Thanks for the help though.  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.