SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: PHP Problems

  1. #1
    SitePoint Evangelist AlexC's Avatar
    Join Date
    Oct 2000
    Location
    Sheffield, UK
    Posts
    437
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HELP! :}

    i'm trying to get an install script to work, and keep getting errors - specifically, it says The site database is unavailable.Access denied for user: '@localhost' to database 'atomico_ads'. - I HAVE!!! Granted access to the db user

    config.php:
    Code:
    <?php
    // config.php
    //database host (usually localhost)
    $dbhost = "localhost";
    //database user
    $dbuser = "atomico_ads";
    //database password
    $dbpass = "xxx";
    //database
    $dbname = "atomico_ads";
    //admins email
    $email = "you@domain.com";
    //send mail on recieving an article?
    //1 for yes, 2 for no
    $mailrecieve = 1;
    //email message that users will recieve when someone sends them an article from the site
    //use \n for new lines. an example message is provided
    //use $articleurl for the url of the article
    //use $sendersname for the senders name
    //use $sendersemail for the senders email
    //use $sendermessage for the message the sender included
    //use $friendsname for the name of the person being sent to
    //use $reciever for the email of the person recieving
    $emailmessage = "na";
    //$fromemail - this will appear as the From: address on the mail
    $fromemail = "na";
    
    //BELOW ARE COMMON FUNCTIONS USED BY THIS APPLICATION. DO NOT MODIFY ANY OF IT!
    //database connect - uses variables above
    function dbconnect($db) {
    	mysql_connect($dbhost,$dbuser,$dbpass) or die("The site database appears to be down.");
        mysql_select_db($db) or die("<p><font face=verdana size=3 color=#FF0000><b>The site database is unavailable. " . MySQL_Error() . " .</font></b></p>");
    }
    //error sequence
    function error($msg) {
    	?>
    		<html>
    		<head>
    		<script language="JavaScript">
    		<!--
    			alert("<?=$msg?>");
    			history.back();
    		//-->
    		</script>
    		</head>
    		<body>
    		</body>
    		</html>
    		<?
    		exit;
    }
    //mail funtion
    function articlemail($reciever,$sendername,$senderemail,$sendermessage,$friendsname) {
    	mail("$reciever","Article","$emailmessage","From:$fromemail");
    }
    //email function :~}
    function email23($articleurl) {
    	mail("$email","article submitted","$articleurl","From:$fromemail");
    }
    //thats it for config.php!
    ?>
    install.php:
    Code:
    <html>
    <head>
    <title>Install Wizard V1.1</title>
    </head>
    <body>
    <?php
    require("config.php");
    //install.php
    //DELETE AFTER USE!
    if ($step==1) {
    	echo("Welcome to the setup program! Please follow all instructions carefully and make sure you have read the readme file.<br><br><a href='install.php?step=2'>Click here to continue!</a>");
    	exit;
    }
    if ($step==2) {
    	if (file_exists("config.php")) {
    		echo("CONFIG.PHP: Check all the following is correct:<br><br>Database Host: $dbhost<br><br>Database User: $dbuser<br><br>Database Password: $dbpass<br><br>Database: $dbdb<br><br><a href='install.php?step=3'>Click Here to proceed, or change your config file</a>");
    		exit;
    	} else {
    		echo("Config.php - please re-upload this file");
    	}
    	exit;
    }
    if ($step==3) {
    	dbconnect($dbname);
    	echo("Setting up tables in the database");
    	$sql = mysql_query("CREATE TABLE articles (" .
    					   "ArticleID INT NOT NULL AUTO_INCREMENT PRIMARY KEY," .
    					   "ArticleTitle VarChar(60) NOT NULL," .
    					   "ArticleCat INT NOT NULL," .
    					   "ArticleDescription Text NOT NULL," .
    					   "ArticleDate VarChar(50) NOT NULL," .
    					   "ArticleAuthor INT NOT NULL," .
    					   "ArticleText TEXT NOT NULL," .
    					   "ArticleKeywords TEXT NOT NULL," .
    					   "ArticleVisible ENUM('N','Y') NOT NULL" .
    					   ");");
    	if (!$sql) {
    		echo("Error: Articles Table Not Created! Please load the file tables.sql into phpmyadmin");
    	} else {
    		echo("Articles Table Created Successfully...<br><br>");
    	}
    	$sql2 = mysql_query("CREATE TABLE columns (" .
    					   "ColumnID INT NOT NULL AUTO_INCREMENT PRIMARY KEY," .
    					   "ColumnName VarChar(60) NOT NULL," .
    					   ");");
    	if (!$sql2) {
    		echo("Error: Columns Table Not Created! Please load the file tables.sql into phpmyadmin");
    	} else {
    		echo("Columns Table Created Successfully...<br><br>");
    	}
    	$sql3 = mysql_query("CREATE TABLE news (" .
    					   "NewsID INT NOT NULL AUTO_INCREMENT PRIMARY KEY," .
    					   "News Text NOT NULL," .
    					   "Column INT NOT NULL," .
    					   ");");
    	if (!$sql3) {
    		echo("Error: News Table Not Created! Please load the file tables.sql into phpmyadmin");
    	} else {
    		echo("News Table Created Successfully...<br><br>");
    	}
    	$sql4 = mysql_query("CREATE TABLE users (" .
    					   "UserID ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY," .
    					   "Username VarChar(20) NOT NULL," .
    					   "Password VarChar(20) NOT NULL," .
    					   "lastlogin VarChar(20) NOT NULL," .
    					   "lastdate VarChar(50) NOT NULL," .
    					   "level INT NOT NULL," .
    					   "firstname TEXT NOT NULL," .
    					   "lastname TEXT NOT NULL," .
    					   "bio TEXT NOT NULL" .
    					   ");");
    	if (!$sql4) {
    		echo("Error: Users Table Not Created! Please load the file tables.sql into phpmyadmin");
    	} else {
    		echo("Users Table Created Successfully...");
    	}
    }
    	?>
    </body>
    </html>
    any help is appreciated - Alex
    Nearly 7 years old!

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a clue:

    Access denied for user: '@localhost'

    You expect user to be atomico_ads@localhost

    Looking at the function
    PHP Code:

    function dbconnect($db) {
        
    mysql_connect($dbhost,$dbuser,$dbpass
         or die(
    "The site database appears to be down.");
        
    mysql_select_db($db) or die("<p><font face=verdana size=3 color=#FF0000>
       <b>The site database is unavailable. " 
    MySQL_Error() . " .</font></b></p>");

    $dbhost, $dbuser and $dbpass - none of these variables have been initialised. They are not the same variables as the ones at the begining of your script becuase they have different scope (being inside the function).

    So you can do two things:

    1) Insert this as the first line of code inside your function
    PHP Code:
    function dbconnect($db) {
       global 
    $dbhost$dbuser$dbpass;
       
    // rest of function code

    2) Or for a more correct design pass the parameters to the function
    PHP Code:
    function dbconnect($db$dbhost$dbuser$dbpass$db) {
       
    // function code

    Then in your code you will have to modify the calls to the function dbconnect so that, here for example:
    PHP Code:
    if ($step==3) {
        
    dbconnect($dbname);
        echo(
    "Setting up tables in the database"); 
    dbconnect($dbname);

    replace this with

    dbconnect($db, $dbhost, $dbuser, $dbpass, $dbname);


    Anyway while option 2) above is a more correct design, option 1) will provide you with an easier "fix" at this stage.
    Last edited by freakysid; Feb 28, 2001 at 00:58.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •