zeeshan_haider000 Posted December 30, 2008 Share Posted December 30, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/138924-mysql_query-functionmysql-query-error/ Share on other sites More sharing options...
Maq Posted December 30, 2008 Share Posted December 30, 2008 Code Quote Link to comment https://forums.phpfreaks.com/topic/138924-mysql_query-functionmysql-query-error/#findComment-726524 Share on other sites More sharing options...
chronister Posted December 30, 2008 Share Posted December 30, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/138924-mysql_query-functionmysql-query-error/#findComment-726533 Share on other sites More sharing options...
zeeshan_haider000 Posted December 30, 2008 Author Share Posted December 30, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138924-mysql_query-functionmysql-query-error/#findComment-726636 Share on other sites More sharing options...
chronister Posted December 31, 2008 Share Posted December 31, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/138924-mysql_query-functionmysql-query-error/#findComment-726641 Share on other sites More sharing options...
zeeshan_haider000 Posted December 31, 2008 Author Share Posted December 31, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138924-mysql_query-functionmysql-query-error/#findComment-726760 Share on other sites More sharing options...
chronister Posted December 31, 2008 Share Posted December 31, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/138924-mysql_query-functionmysql-query-error/#findComment-726762 Share on other sites More sharing options...
zeeshan_haider000 Posted December 31, 2008 Author Share Posted December 31, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138924-mysql_query-functionmysql-query-error/#findComment-726771 Share on other sites More sharing options...
Maq Posted December 31, 2008 Share Posted December 31, 2008 Why do you put config.php in that function anyway? Quote Link to comment https://forums.phpfreaks.com/topic/138924-mysql_query-functionmysql-query-error/#findComment-726944 Share on other sites More sharing options...
premiso Posted December 31, 2008 Share Posted December 31, 2008 Why do you put config.php in that function anyway? Newbie mistake. At least he learned his lesson now =) Quote Link to comment https://forums.phpfreaks.com/topic/138924-mysql_query-functionmysql-query-error/#findComment-726946 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.