Author Topic: Trouble accessing MySQL database  (Read 1675 times)

0 Members and 1 Guest are viewing this topic.

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Trouble accessing MySQL database
« on: September 01, 2010, 10:14:38 AM »
Hello,

After re-hosting a website there has been trouble connecting to the database.

The MySQL error can be seen here:

http://www.lock-tech.co.uk/home/

If someone can help out with this i would be amazed.

Kind regards,
laanes

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Trouble accessing MySQL database
« Reply #1 on: September 01, 2010, 10:18:08 AM »
You do realize that most people on a forum won't click on links you put in your posts.

Just copy/paste the error message into a post to get the quickest solution.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline Pikachu2000

  • I hate everything.
  • Global Moderator
  • Freak!
  • *
  • Posts: 9,061
  • Gender: Male
  • Is it solipsistic in here, or is it just me?
    • View Profile
Re: Trouble accessing MySQL database
« Reply #2 on: September 01, 2010, 10:26:19 AM »
Looks like either the username, or password, or MySQL server host name, or any combination of those are wrong. Check the hosting company's FAQ's, and other docs to make sure you have everything specified correctly.
"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf * || Linux Help --> linuxforum.com

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #3 on: September 01, 2010, 10:35:13 AM »
The error message:


Warning
mysql_connect() [function.mysql-connect]: Access denied for user 'swansea3_locktes'@'localhost' (using passwordYESin /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/functions.php on line 25

Warning
mysql_select_db() [function.mysql-select-db]: Access denied for user 'swansea3'@'localhost' (using passwordNOin /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/functions.php on line 26

Warning
mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/functions.php on line 26

Warning
Cannot modify header information headers already sent by (output started at /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/functions.php:25in /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/sessions.php on line 3

Warning
Cannot modify header information headers already sent by (output started at /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/functions.php:25in /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/sessions.php on line 4

Warning
Cannot modify header information headers already sent by (output started at /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/functions.php:25in /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/sessions.php on line 5

Warning
Cannot modify header information headers already sent by (output started at /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/functions.php:25in /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/sessions.php on line 6

Warning
mysql_query() [function.mysql-query]: Access denied for user 'swansea3'@'localhost' (using passwordNOin /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/functions.php on line 203

Warning
mysql_query() [function.mysql-query]: A link to the server could not be established in /home/swansea3/public_html/lock-tech.co.uk/home/scrptz/functions.php on line 203
ERROR 
{2.1a}! Please navigate to a different page and then try this page again.


I have tried various sets of user names with various different database names/passwords.

The hosting provider says that my connection details are correct and i need to check the configuration in the index.php file. That does not make sense as the website has only been re-hosted, not modified in any way.



Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Trouble accessing MySQL database
« Reply #4 on: September 01, 2010, 10:40:39 AM »
As Pikachu2000 wrote, either one of the three pieces of information is wrong or you don't have a database on that hostname setup with permissions for that username/password.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #5 on: September 01, 2010, 10:56:06 AM »
Structure of index.php:


<?php 
$hostName
="localhost"
$dbName="*******"$pwd="*******"
@
mysql_connect($hostName$dbName$pwd); 
@
mysql_select_db($dbName); 

function 
clean($input$maxLength){$input=substr(stripslashes(EscapeShellCmd($input)), 0$maxLength); return $input;}
foreach(
$HTTP_GET_VARS as $varName=>$value)$getVars[$varName]=trim(clean($value100)); 
foreach(
$HTTP_POST_VARS as $varName=>$value)$postVars[$varName]=trim(clean($value100));

?>



Is it possible that some of the syntax out of date and is not compatible with the server version?

Offline Pikachu2000

  • I hate everything.
  • Global Moderator
  • Freak!
  • *
  • Posts: 9,061
  • Gender: Male
  • Is it solipsistic in here, or is it just me?
    • View Profile
Re: Trouble accessing MySQL database
« Reply #6 on: September 01, 2010, 11:01:34 AM »
What company is the site hosted with?
"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf * || Linux Help --> linuxforum.com

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #7 on: September 01, 2010, 11:04:47 AM »
MySQL client version: 5.0.91

Server version: 5.0.91

Server: Localhost via UNIX socket

Offline Pikachu2000

  • I hate everything.
  • Global Moderator
  • Freak!
  • *
  • Posts: 9,061
  • Gender: Male
  • Is it solipsistic in here, or is it just me?
    • View Profile
Re: Trouble accessing MySQL database
« Reply #8 on: September 01, 2010, 11:09:42 AM »
If they provide phpMyAdmin as one of the tools, are you able to log in and use it with the same credentials you're using in the script?
"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf * || Linux Help --> linuxforum.com

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #9 on: September 01, 2010, 11:19:20 AM »
I am always logged into phpMyAdmin with one default user.

Credentials for each database are set through a database wizard in cPanel but when using phpMyadmin it always shows the default user - defaultuser@localhost, no matter which database is currently being used.
« Last Edit: September 01, 2010, 11:20:07 AM by laanes »

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #10 on: September 01, 2010, 11:31:52 AM »
If any helpful, here is the rest of the code in index.php:

<?php $hostName="localhost"
$dbName="****"$pwd="****"
@
mysql_connect($hostName$dbName$pwd); 
@
mysql_select_db($dbName); 

function 
clean($input$maxLength){$input=substr(stripslashes(EscapeShellCmd($input)), 0$maxLength); return $input;}
foreach(
$HTTP_GET_VARS as $varName=>$value)$getVars[$varName]=trim(clean($value100)); 
foreach(
$HTTP_POST_VARS as $varName=>$value)$postVars[$varName]=trim(clean($value100));
if(
$postVars["action"]=="login")
{
	
if(
$postVars["usr"] && $postVars["pwd"])
	
{
	
	
// Get customer details
	
	
if(
$custDetails=mysql_fetch_array(@mysql_query("SELECT * FROM customers WHERE accCode='".$postVars["usr"]."' and password='".$postVars["pwd"]."'")))
	
	
{
	
	
	
$securePrefix="https://fusion.bpweb.net/secure/locktech/";
	
	
	
$navTo=$postVars["goto"]."?uid=".$postVars["uID"];

	
	
	
// Insert details in session table
	
	
	
mysql_query("UPDATE sessions SET accCode='".$custDetails["accCode"].
	
	
	
"', discount='".$custDetails["salesInd3"].
	
	
	
"', buyAlert='".$custDetails["buyAlert"].
	
	
	
"', stockAlert='".$custDetails["stockAlert"].
	
	
	
"', vat='".$custDetails["vat"].
	
	
	
"' WHERE uid='".$postVars["uID"]."' LIMIT 1")
	
	
	
or 
$navTo="login.php?uid=".$postVars["uID"]."&logError=true";
	
	
}
	
	
else
	
	
{
	
	
	
unset(
$securePrefix);
	
	
	
$navTo="login.php?uid=".$postVars["uID"]."&logError=true";
	
	
}
	
}
}
elseif(
$postVars["killSession"]){@mysql_query("DELETE FROM sessions WHERE uid='".$postVars["killSession"]."' AND accCode='".$postVars["account"]."' LIMIT 1"); $navTo="main.php";}
elseif(
$getVars["myURL"])
{
	
if(
strpos($getVars["myURL"], "account")===false && strpos($getVars["myURL"], "basket")===false &&
	
strpos($getVars["myURL"], "checkout")===false &&
	
strpos($getVars["myURL"], "confirm")===false && strpos($getVars["myURL"], "goto")===false && strpos($getVars["myURL"], "vat")===false &&
	
strpos($getVars["myURL"], "PCode")===false && strpos($getVars["myURL"], "qty")===false && strpos($getVars["myURL"], "orders")===false && strpos($getVars["myURL"], "chpwd")===false &&
	
strpos($getVars["myURL"], "price")===false && strpos($getVars["myURL"], "UPDATE")===false && strpos($getVars["myURL"], "DELETE")===false &&
	
strpos($getVars["myURL"], "myURL")===false && strpos($getVars["myURL"], "uid")===false && strpos($getVars["myURL"], "action")===false)
	
{
	
	
$navTo=$getVars["myURL"];
	
	
foreach(
$getVars as $varName=>$value)if($varName!="myURL" && $varName!="goto" && $varName!="vat" && $varName!="PCode" && $varName!="qty" &&
	
	
$varName!="price" && $varName!="UPDATE_x" && $varName!="DELETE_x" && $varName!="uid" && $varName!="action")$navTo.="&amp;".$varName."=".$value;
	
}
	
else{
$navTo="main.php"; unset($securePrefix);}
}
else{
$navTo="main.php"; unset($securePrefix);} ?>

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #11 on: September 01, 2010, 11:36:20 AM »
I just noticed that URL does not exist:

$securePrefix="https://fusion.bpweb.net/secure/locktech/";

Quote
The requested URL /secure/locktech/ was not found on this server.

But even if i remove it nothing changes so I guess that hasn't got nothing to do with it.
« Last Edit: September 01, 2010, 11:45:26 AM by laanes »

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Trouble accessing MySQL database
« Reply #12 on: September 01, 2010, 11:44:39 AM »
Quote
@mysql_connect($hostName, $dbName, $pwd);
@mysql_select_db($dbName);

^^^ If that's your real code, take a look at it. You are using $dbName as the mysql username (2nd parameter in the mysql_connect.) Edit: And I'm going to guess that if you had actually read the mysql_connect() error message, you would have noticed that the user it mentions is actually your database name? You are the only one here who knows what your actual information is and can interpret what the error message means relative to what you are doing.

And I'm guessing that the @'s in the code were just put there by you due to the error messages?

Hiding the errors won't make your code work. It still won't work AND there won't be any error messages to tell you why it is not working.
« Last Edit: September 01, 2010, 11:53:39 AM by PFMaBiSmAd »
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #13 on: September 01, 2010, 12:04:07 PM »
Quote
You are the only one here who knows what your actual information is and can interpret what the error message means relative to what you are doing.

$hostName="localhost";
$dbUser="swansea3_lockT";
$dbName="swansea3_locktest";


Quote
And I'm guessing that the @'s in the code were just put there by you due to the error messages?

Hiding the errors won't make your code work. It still won't work AND there won't be any error messages to tell you why it is not working.

Code has not been written by me, if it would, i would not be asking questions here.

I have removed the @'s and changed the parameters in mysql_connect:


$hostName
="localhost"
$dbName="swansea3_locktest"
$dbUser="swansea3_lockT";
$pwd="******"
mysql_connect($hostName$dbUser$pwd); 
mysql_select_db($dbName); 


Error message does not change.

I don't get why the error message is saying Access denied for user 'swansea3'@'localhost as I have not swansea3 to be a user for that database.

swansea3 is the default username given you when creating an account with the hosting provider and is also the database prefix.
« Last Edit: September 01, 2010, 12:11:00 PM by laanes »

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Trouble accessing MySQL database
« Reply #14 on: September 01, 2010, 01:04:20 PM »
There's no way the code you have been posting with the @'s in it could have been outputting the posted errors. You must have some other code at the file/line number being reported in the errors.

Only the first error is relevant to this problem. The other errors are follow-on errors caused because the mysql_connect() is failing.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #15 on: September 01, 2010, 02:04:23 PM »
There's no way the code you have been posting with the @'s in it could have been outputting the posted errors. You must have some other code at the file/line number being reported in the errors.

Only the first error is relevant to this problem. The other errors are follow-on errors caused because the mysql_connect() is failing.

So most definately some of the credentials must be wrong?

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Trouble accessing MySQL database
« Reply #16 on: September 01, 2010, 02:10:36 PM »
Quote
/home/scrptz/functions.php on line 25

For anyone here to directly be able to help, it would take seeing what lines 1 - 25 of that file is.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #17 on: September 01, 2010, 03:01:20 PM »
Quote
/home/scrptz/functions.php on line 25

For anyone here to directly be able to help, it would take seeing what lines 1 - 25 of that file is.

<?php
define
("VAT"17.5);
define("JIFFY"5);
define("HTMLSIGNATURE""<br><br><br><br><br><br>LOCK-TECH&reg; a division of<br>Swansea Timber & Ply Co. Ltd.<br>57-60 Oxford Street, Swansea SA1 3JD<br><br>[t] 01792 655680 [f] 01792 462243<br>[e] <a href=\"mailto:sales@lock-tech.co.uk\">sales@lock-tech.co.uk</a> [w] <a href=\"http://www.lock-tech.co.uk\">www.lock-tech.co.uk</a>");
define("TEXTSIGNATURE""\n\n\n\n\n\nLOCK-TECH&#65533; a division of\nSwansea Timber & Ply Co. Ltd.\n57-60 Oxford Street, Swansea SA1 3JD\n\n[t] 01792 655680 [f] 01792 462243\n[e] sales@lock-tech.co.uk [w] www.lock-tech.co.uk");
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
$bArray=array();
$sessions=array();
@
set_time_limit(3);

// Database vars
$hostName="localhost";
$dbUser="swansea3_lockT";
$dbName="swansea3_locktech";
$pwd="****";
$qMain="SELECT * FROM pDetails
	
INNER JOIN products ON (pDetails.code = products.SCode)
	
INNER JOIN PRanges ON (products.PRange = PRanges.RangeID)
	
INNER JOIN PGroups ON (products.PGroup = PGroups.PG)
	
INNER JOIN SubG ON (products.PSubG = SubG.SPG)
	
INNER JOIN SubSubG ON (products.SSPG = SubSubG.SSPG) "
;

mysql_connect($hostName$dbUser$dbName$pwd);         <-------  LINE 25
mysql_select_db
($dbName);


Ok, now when you pointed where to look, I can see that there is one too many variables listed in the MySQL connection. No need to have dbname in there.

However, when I remove it, a whole new error message is being displayed:

ERROR {2.1a}! Please navigate to a different page and then try this page again.

I can see that this is an "or die" message added to every function in functions.php file.


functions with this output in functions.php:

function inDaList($addy)
{
	
$result=@mysql_query("SELECT email FROM mailinglist") or die("<font color=\"#FF0000\">ERROR {2.0a}! Please navigate to a <a href=\"main.php?uid=".$sessions['uid']."\">different page</a> and then try this page again.</font>");
	
while(
$row=@mysql_fetch_array($result))foreach($row as $data)$addyArray[]=strtoupper($data);

	
return 
in_array($addy$addyArray);
}


// Session functions
function startSession()
{
	
$result=mysql_query("SELECT uid FROM sessions")
	
or die(
"<font color=\"#FF0000\">ERROR {2.1a}! Please navigate to a <a href=\"main.php?uid=".$sessions['uid']."\">different page</a> and then try this page again.</font>");

	
while(
$row=mysql_fetch_array($result))foreach($row as $data)$sArray[]=$data;

	
$result=mysql_query("SELECT sessionID FROM basket")
	
or die(
"<font color=\"#FF0000\">ERROR {2.1b}! Please navigate to a <a href=\"main.php?uid=".$sessions['uid']."\">different page</a> and then try this page again.</font>");

	
while(
$row=mysql_fetch_array($result))foreach($row as $data)$bArray[]=$data;

	
srand((float)microtime()*100000000);
	
$theID=md5(rand());

	
if(!
count($sArray))$sArray=array();
	
if(!
count($bArray))$bArray=array();
	
while(
in_array($theID$sArray) || in_array($theID$bArray))$theID=md5(rand());
	

	
mysql_query("INSERT INTO sessions SET uid='".$theID."',theTime='".time()."'")
	
or die(
"<font color=\"#FF0000\">ERROR {2.2}! Please navigate to a <a href=\"main.php\">different page</a> and then try this page again.</font>");
	

	
global 
$getVars;
	
$getVars["uid"]=$theID;
}


function 
destroySession($ID)
{
	
global 
$getVars$sessions;
	
mysql_query("DELETE FROM sessions WHERE ID='".$ID."' LIMIT 1")
	
or die(
"<font color=\"#FF0000\">ERROR {2.3}! Please navigate to a <a href=\"main.php?uid=".$sessions['uid']."\">different page</a> and then try this page again.</font>");
	

	
// Remove session ID from getVars
	
unset(
$getVars['uid']);
}

function 
readSessionVars($ID)
{
	
global 
$getVars$sessions;
	

	
$result=mysql_query("SELECT * FROM sessions WHERE uid='".$ID."' AND theTime>='".(time()-(3600*8))."'")
	
or die(
"<font color=\"#FF0000\">ERROR {2.4}! Please navigate to a <a href=\"main.php?uid=".$sessions['uid']."\">different page</a> and then try this page again.</font>");


	
while(
$row=mysql_fetch_array($result))foreach($row as $i=>$data)$sessions[$i]=$data;
	

	
mysql_query("UPDATE sessions SET theTime='".time()."' WHERE ID='".$sessions["uid"]."' LIMIT 1")
	
or die(
"<font color=\"#FF0000\">ERROR {2.5}! Please navigate to a <a href=\"main.php?uid=".$sessions['uid']."\">different page</a> and then try this page again.</font>");
}

function 
updateSession($ID$var$value)
{
	
global 
$getVars$sessions;

	
mysql_query("UPDATE sessions SET ".$var."='".$value."' WHERE ID='".$ID."' LIMIT 1")
	
or die(
"<font color=\"#FF0000\">ERROR {2.6}! Please navigate to a <a href=\"main.php?uid=".$sessions['uid']."\">different page</a> and then try this page again.</font>");

	
readSessionVars($getVars["uid"]);
}

include(
'efunctions.php');
include(
'sessions.php');





Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Trouble accessing MySQL database
« Reply #18 on: September 01, 2010, 03:10:35 PM »
The current symptom means that the query failed due to an error (I'm going to guess a problem with the table name or the column name or permissions or the database...)

For debugging purposes (restore the code to what it currently is now when you are done), change the or die(.....) to

or die(mysql_error());

P.S. Are you sure this code worked before it was moved to the current server? All the @'s tell me that whoever wrote this was in favor of hiding problems in the code rather than finding and fixing them. For example, @set_time_limit(3); won't ever produce an error unless that function has been disabled on the server (which is highly unlikely), but you would want to know if that was the case and you would want to have that error reported.
« Last Edit: September 01, 2010, 03:14:39 PM by PFMaBiSmAd »
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #19 on: September 02, 2010, 03:51:13 AM »
I have now added - or die(mysql_error()); to every function that used to have - or die("<font color=\"#FF0000\">ERROR {2.6}! Please navigate to a <a href=\"main.php?uid=".$sessions['uid']."\">different page</a> and then try this page again.</font>"); instead.

I have also tried to add a comment to every function to help myself to understand what each of them means. It is possible that I have misunderstood some of them in which case the comments would not be very helful.


function inDaList($addy)
{
	
$result=@mysql_query("SELECT email FROM mailinglist") or die(mysql_error());
	
while(
$row=@mysql_fetch_array($result))foreach($row as $data)$addyArray[]=strtoupper($data);

	
return 
in_array($addy$addyArray);
}

// Session functions
function startSession()
{
	
// Read all sessionIDs from session table
	
$result=mysql_query("SELECT uid FROM sessions")
	
or die(
mysql_error());

	
// Stick them in an array
	
while(
$row=mysql_fetch_array($result))foreach($row as $data)$sArray[]=$data;

	
// Read all sessionIDs from basket table
	
$result=mysql_query("SELECT sessionID FROM basket")
    or die(
mysql_error());
	
// Stick them in an array
	
while(
$row=mysql_fetch_array($result))foreach($row as $data)$bArray[]=$data;

	
// Generate random id string
	
srand((float)microtime()*100000000);
	
$theID=md5(rand());

	
// Make sure that the id is unique
	
if(!
count($sArray))$sArray=array();
	
if(!
count($bArray))$bArray=array();
	
while(
in_array($theID$sArray) || in_array($theID$bArray))$theID=md5(rand());
	

	
// Insert values in db
	
mysql_query("INSERT INTO sessions SET uid='".$theID."',theTime='".time()."'")
	
or die(
mysql_error());
	

	
// Add sessionid to Getvars array
	
global 
$getVars;
	
$getVars["uid"]=$theID;
}

function 
destroySession($ID)
{
	
global 
$getVars$sessions;
	
mysql_query("DELETE FROM sessions WHERE ID='".$ID."' LIMIT 1")
	
or die(
mysql_error());
	

	
// Remove session ID from getVars
	
unset(
$getVars['uid']);
}

function 
readSessionVars($ID)
{
	
global 
$getVars$sessions;
	

	
$result=mysql_query("SELECT * FROM sessions WHERE uid='".$ID."' AND theTime>='".(time()-(3600*8))."'")
	
or die(
mysql_error());

	
// Stick session variables into an array
	
while(
$row=mysql_fetch_array($result))foreach($row as $i=>$data)$sessions[$i]=$data;
	

	
// Update session with current time
	
mysql_query("UPDATE sessions SET theTime='".time()."' WHERE ID='".$sessions["uid"]."' LIMIT 1")
	
or die(
mysql_error());
}

function 
updateSession($ID$var$value)
{
	
global 
$getVars$sessions;

	
// Update requested variable in session table
	
mysql_query("UPDATE sessions SET ".$var."='".$value."' WHERE ID='".$ID."' LIMIT 1")
	
or die(
mysql_error());

	
readSessionVars($getVars["uid"]);
}

« Last Edit: September 02, 2010, 03:52:12 AM by laanes »

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #20 on: September 02, 2010, 04:14:21 AM »
I have now added - or die(mysql_error()); to every function that used to have - or die("<font color=\"#FF0000\">ERROR {2.6}! Please navigate to a <a href=\"main.php?uid=".$sessions['uid']."\">different page</a> and then try this page again.</font>"); instead.

I have also tried to add a comment to every function to help myself to understand what each of them means. It is possible that I have misunderstood some of them in which case the comments would not be very helful.


function inDaList($addy)
{
	
$result=@mysql_query("SELECT email FROM mailinglist") or die(mysql_error());
	
while(
$row=@mysql_fetch_array($result))foreach($row as $data)$addyArray[]=strtoupper($data);

	
return 
in_array($addy$addyArray);
}

// Session functions
function startSession()
{
	
// Read all sessionIDs from session table
	
$result=mysql_query("SELECT uid FROM sessions")
	
or die(
mysql_error());

	
// Stick them in an array
	
while(
$row=mysql_fetch_array($result))foreach($row as $data)$sArray[]=$data;

	
// Read all sessionIDs from basket table
	
$result=mysql_query("SELECT sessionID FROM basket")
    or die(
mysql_error());
	
// Stick them in an array
	
while(
$row=mysql_fetch_array($result))foreach($row as $data)$bArray[]=$data;

	
// Generate random id string
	
srand((float)microtime()*100000000);
	
$theID=md5(rand());

	
// Make sure that the id is unique
	
if(!
count($sArray))$sArray=array();
	
if(!
count($bArray))$bArray=array();
	
while(
in_array($theID$sArray) || in_array($theID$bArray))$theID=md5(rand());
	

	
// Insert values in db
	
mysql_query("INSERT INTO sessions SET uid='".$theID."',theTime='".time()."'")
	
or die(
mysql_error());
	

	
// Add sessionid to Getvars array
	
global 
$getVars;
	
$getVars["uid"]=$theID;
}

function 
destroySession($ID)
{
	
global 
$getVars$sessions;
	
mysql_query("DELETE FROM sessions WHERE ID='".$ID."' LIMIT 1")
	
or die(
mysql_error());
	

	
// Remove session ID from getVars
	
unset(
$getVars['uid']);
}

function 
readSessionVars($ID)
{
	
global 
$getVars$sessions;
	

	
$result=mysql_query("SELECT * FROM sessions WHERE uid='".$ID."' AND theTime>='".(time()-(3600*8))."'")
	
or die(
mysql_error());

	
// Stick session variables into an array
	
while(
$row=mysql_fetch_array($result))foreach($row as $i=>$data)$sessions[$i]=$data;
	

	
// Update session with current time
	
mysql_query("UPDATE sessions SET theTime='".time()."' WHERE ID='".$sessions["uid"]."' LIMIT 1")
	
or die(
mysql_error());
}

function 
updateSession($ID$var$value)
{
	
global 
$getVars$sessions;

	
// Update requested variable in session table
	
mysql_query("UPDATE sessions SET ".$var."='".$value."' WHERE ID='".$ID."' LIMIT 1")
	
or die(
mysql_error());

	
readSessionVars($getVars["uid"]);
}


And there we are:

Quote
Table 'swansea3_locktech.sessions' doesn't exist

I guess the next thing now is to find out where is the correct sql file located as the one i imported does not have all the tables needed and can not therefore be the right one.

Re-hosting a website which isn't developed by yourself and is without any documentation seems to be a whole lot of fun.
« Last Edit: September 02, 2010, 04:25:57 AM by laanes »

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #21 on: September 02, 2010, 04:46:22 AM »
After re-importing the database with the missing sessions table, the website came up, hurray! But with missing features - can not get the products from the database.

Quote
No Products Found.
Sorry, but there are no products in this section at the moment. The products are constantly being updated, however. Please try again later or contact us.

I found this line in a file is called groups.php

<td height="416" valign="top" class="mainbold"><?php if($totalGroups>0){?><img src="grp_pics/<?php echo $groupDetails["PG"?>.jpg" alt="Group Picture" galleryimg="no"><br><img src="imgz/pixel.gif" width="306" height="5"><br> <?php echo $groupDetails["GPText"?><?php }else{?><p class="noresult">No Products Found.</p><p class="mainbold">Sorry, but there are no products in this section at the moment. The products are constantly being updated, however. Please try again later or <a href="mailto:sales@lock-tech.co.uk" class="link1" onmouseover="window.status='Send us an Email.'; return true">contact us</a>.</p><?php }?></td>

What is that telling me?

Would there be a way of adding an or die element into that line instead of that please try again later message?
« Last Edit: September 02, 2010, 04:54:36 AM by laanes »

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #22 on: September 02, 2010, 05:08:25 AM »
Or,

it fails before it even gets to that line - when trying to select second level sub groups in the database?

groups.php:

<?php
include 'scrptz/functions.php';

// Page vars
$thisPage=$getVars["page"];
if(
$thisPage<1)$thisPage=1;
$firstGroup=($thisPage*16)-16;

// Get group details and stick them into an array
$qMain="SELECT * FROM PGroups WHERE PG = '".$getVars["prodg"]."'";
$result=mysql_query($qMain)
or die(
"<font color=\"#FF0000\">ERROR {9.1}! Please navigate to a <a href=\"main.php?uid=".$getVars['uid']."\">different page</a> and then try this page again.</font>");
$groupDetails=mysql_fetch_array($result);

// Get all sub groups in current product group
$qMain="SELECT * FROM SubG WHERE ProdG = '".$getVars["prodg"]."'";
$result=mysql_query($qMain)
or die(
"<font color=\"#FF0000\">ERROR {9.2}! Please navigate to a <a href=\"main.php?uid=".$getVars['uid']."\">different page</a> and then try this page again.</font>");

// Stick sub groups in a 2D associative array
$j=0;
while (
$row=mysql_fetch_array($result))
{
	
foreach(
$row as $i=>$data)$array[$j][$i]=$data;
	
$j++;
}

// Calculate vars for navigation
$totalGroups=$j;
$totalPages=ceil($totalGroups/16);

// Set page title (for bookmark)
$pageTitle=$groupDetails["GPname"];

// Set top title
if($totalGroups>0){$topTitle="VIEW PRODUCTS";}else{$topTitle="ERROR";}
?>
<html>
<head>
<script language="JavaScript" type="text/javascript">var theID="<?php echo $getVars['uid']; ?>";</script>
<script language="JavaScript" src="scrptz/content.js" type="text/javascript"></script>
<script language="JavaScript" src="scrptz/menu.js" type="text/javascript"></script>
<script language="JavaScript" src="scrptz/scripts.js" type="text/javascript"></script>
</head>
<body background="imgz/bg.gif" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0"<?php echo $notify ?>>
<?php include("top.php"); ?>
<table width="952" height="509" border="0" cellpadding="0" cellspacing="0" class="maintext">
<?php if($sessions['accCode']){?><form action="http://www.lock-tech.co.uk/index.php" method="post" name="logout" target="_parent"><input name="account" type="hidden" value="<?php echo $sessions['accCode']; ?>"><input name="killSession" type="hidden" value="<?php echo $getVars['uid']; ?>"></form><?php }?>
  <tr> 
    <td width="1" rowspan="5" bgcolor="#000000"></td>
    <td height="21" background="imgz/topbar.gif" bgcolor="#14359E">&nbsp;</td>
    <td height="21" colspan="5" valign="middle" background="imgz/topbar.gif" bgcolor="#14359E" class="mainyellow"><?php include("scrptz/dd_menu.php"); ?></td>
    <td height="21" background="imgz/topbar.gif" bgcolor="#14359E">&nbsp;</td>
    <td width="1" rowspan="5" bgcolor="#000000"></td>
  </tr>
  <tr> 
    <td width="5" rowspan="3">&nbsp;</td>
    <td width="306" height="30" valign="bottom" class="title2"><?php echo $groupDetails["GPname"?></td>
    <td width="11" rowspan="3"><img src="imgz/maindiv.gif" width="11" height="466"></td>
    <td width="306" height="30" align="center" valign="bottom" class="title2"><?php if($totalGroups>0){?><a href="g_view.php?pgroup=<?php echo $groupDetails["PG"?>&amp;uid=<?php echo $getVars['uid']; ?>" class="link4" onmouseover="window.status='View List of all Products in <?php echo $groupDetails["GPname"?>'; return true">VIEW ALL</a> | <a href="g_browse.php?pgroup=<?php echo $groupDetails["PG"?>&amp;uid=<?php echo $getVars['uid']; ?>" class="link4" onmouseover="window.status='Browse all Products in <?php echo $groupDetails["GPname"?>'; return true">BROWSE ALL</a><?php }?></td>
    <td width="11" rowspan="3"><img src="imgz/maindiv.gif" width="11" height="466"></td>
    <td width="306" height="30" align="center" valign="bottom">&nbsp;</td>
    <td width="5" rowspan="3">&nbsp;</td>
  </tr>
  <tr> 
    <td height="20" valign="top" class="mainbold"></td>
    <td width="306" rowspan="2" valign="top" class="maintext">
      <?php for($i=$firstGroup$i<($firstGroup+8) && $i<$totalGroups$i++){?>
      <a href="sub.php?PSubG=<?php echo $array[$i]["SPG"?>&amp;uid=<?php echo $getVars['uid']; ?>" class="link1" onmouseover="window.status='View Products in <?php echo $array[$i]["SPGname"?>'; return true"><?php echo $array[$i]["SPGname"?></a><br>
	
	
	
<?php
	
	
	
// Get all subsubs in current sub group
	
	
	
$qMain="SELECT * FROM SubSubG WHERE SPG = '".$array[$i]["SPG"]."'";
	
	
	
$result=mysql_query($qMain)
	
	
	
or die(
"<font color=\"#FF0000\">ERROR {9.3}! Please navigate to a <a href=\"main.php?uid=".$getVars['uid']."\">different page</a> and then try this page again.</font>");

	
	
	
// Print subsubs
	
	
	
while (
$row=mysql_fetch_array($result))
	
	
	
{
	
	
	
?>
	
	
	
	
[<a href="subsub.php?PSubSubG=<?php echo $row["SSPG"]?>&amp;uid=<?php echo $getVars['uid']; ?>" class="link3" onmouseover="window.status='View Products in <?php echo $row["SSPGName"]?>'; return true"><?php echo $row["SSPGName"]?></a>]&nbsp;
	
	
	
<?php
	
	
	
}
	
	
	
?>
	
	
	
<br><br>
	
	
	
<?php }?></td>
    <td width="306" rowspan="2" valign="top" class="maintext">
      <?php for($i=$firstGroup+8$i<($firstGroup+16) && $i<$totalGroups$i++){?>
      <a href="sub.php?PSubG=<?php echo $array[$i]["SPG"?>&amp;uid=<?php echo $getVars['uid']; ?>" class="link1" onmouseover="window.status='View Products in <?php echo $array[$i]["SPGname"?>'; return true"><?php echo $array[$i]["SPGname"?></a><br>
	
	
	
<?php
	
	
	
// Get all subsubs in current sub group
	
	
	
$qMain="SELECT * FROM SubSubG WHERE SPG = '".$array[$i]["SPG"]."'";
	
	
	
$result=mysql_query($qMain)
	
	
	
or die(
"<font color=\"#FF0000\">ERROR {9.4}! Please navigate to a <a href=\"main.php?uid=".$getVars['uid']."\">different page</a> and then try this page again.</font>");

	
	
	
// Print subsubs
	
	
	
while (
$row=mysql_fetch_array($result))
	
	
	
{
	
	
	
?>
	
	
	
	
[<a href="subsub.php?PSubSubG=<?php echo $row["SSPG"]?>&amp;uid=<?php echo $getVars['uid']; ?>" class="link3" onmouseover="window.status='View Products in <?php echo $row["SSPGName"]?>'; return true"><?php echo $row["SSPGName"]?></a>]&nbsp;
	
	
	
<?php
	
	
	
}
	
	
	
?>
	
	
	
<br><br>
<?php }?></td>
  </tr>
  <tr> 
    <td height="416" valign="top" class="mainbold"><?php if($totalGroups>0){?><img src="grp_pics/<?php echo $groupDetails["PG"?>.jpg" alt="Group Picture" galleryimg="no"><br><img src="imgz/pixel.gif" width="306" height="5"><br> <?php echo $groupDetails["GPText"?><?php }else{?><p class="noresult">No Products Found.</p><p class="mainbold">Sorry, but there are no products in this section at the moment. The products are constantly being updated, however. Please try again later or <a href="mailto:sales@lock-tech.co.uk" class="link1" onmouseover="window.status='Send us an Email.'; return true">contact us</a>.</p><?php }?></td>
  </tr>
  <tr> 
    <td width="5" height="22" background="imgz/btmbar.gif" bgcolor="#14359E">&nbsp;</td>
    <td width="306" height="22" align="center" valign="top" background="imgz/btmbar.gif" bgcolor="#14359E"><a href="javascript:history.back()" onmouseover="prev_btn.src='imgz/prev2.gif';window.status='Previous Page'; return true" onmouseout="prev_btn.src='imgz/prev.gif'"><img name="prev_btn" src="imgz/prev.gif" width="124" height="16" border="0"></a></td>
    <td width="11" height="22" bgcolor="#14359E"><img src="imgz/btmdiv.gif" width="11" height="22"></td>
    <td width="306" height="22" align="center" background="imgz/btmbar.gif" bgcolor="#14359E" class="nav"><? include 'scrptz/nav.php' ?></td>
    <td width="11" height="22" bgcolor="#14359E"><img src="imgz/btmdiv.gif" width="11" height="22"></td>
    <td width="306" height="22" align="center" valign="top" background="imgz/btmbar.gif" bgcolor="#14359E"><a href="javascript:directLink('<?php echo $prodLink?>','<? echo $pageTitle ?>')" onmouseover="link_btn.src='imgz/link2.gif'; window.status='Direct Link to This Page'; return true" onmouseout="link_btn.src='imgz/link.gif'"><img name="link_btn" src="imgz/link.gif" alt="" width="182" height="16" border="0"></a></td>
    <td width="5" height="22" background="imgz/btmbar.gif" bgcolor="#14359E">&nbsp;</td>
  </tr>
</table>
</body>
</html>


« Last Edit: September 02, 2010, 05:17:03 AM by laanes »

Offline laanesTopic starter

  • Enthusiast
  • Posts: 70
    • View Profile
Re: Trouble accessing MySQL database
« Reply #23 on: September 02, 2010, 11:29:14 AM »
I am not getting any further with it.

Anyone willing to help?

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: Trouble accessing MySQL database
« Reply #24 on: September 06, 2010, 01:33:28 AM »
So have you proven you can actually connect?
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.