Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2012
    Posts
    10

    Unanswered: can't connect to MySQL

    Before I get started I'd like to say that I JUST started coding PHP to build my e-commerce site so I don't know enough yet to know how to identify the problem in order to ask a question about it.... I say this because I've been hearing people all week saying "be more specific".... when I CAN'T!!! because I don't know... now that that's out the way... I have some pages coded in PHP for creating tables which I uploaded to my database which worked.... but I'm having trouble with my login page.... so far I've honed in on my "MySQL_Connect" page not working right which may be causing my index and login page to not run... but here's the coding for all 3

    connect_to_mysql
    Code:
    <?php
    /*  
    1: "die()" will exit the script and show an error statement if something goes wrong with the "connect" or "select" functions. 
    2: A "mysql_connect()" error usually means your username/password are wrong  
    3: A "mysql_select_db()" error usually means the database does not exist. 
    */ 
    // Place db host name. Sometimes "localhost" but  
    // sometimes looks like this: >>      ???mysql??.someserver.net 
    $db_host = "localhost"; 
    // Place the username for the MySQL database here 
    $db_username = "#";  
    // Place the password for the MySQL database here 
    $db_pass = "#";  
    // Place the name for the MySQL database here 
    $db_name = "#"; 
    
    // Run the actual connection here  
    mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
    mysql_select_db("$db_name") or die ("no database");              
    ?>
    admin_index
    Code:
    <?php
    session_start();
    if(!isset($_SESSION["manager"])){
    	header("location:http://www.optiq-customtees.zxq.net/storeadmin/admin_login.php");
    	exit();
    	ini_set('display_errors',1); 
     error_reporting(E_ALL);
    }
    //Be sure to check that this manager SESSION value is in fact in the database
    $managerID=preg_replace('#[^0-9]#i','',$_SESSION["id"]);//filter everything but numbers and letters
    $manager=preg_replace('#[^A-Za-z0-9_]#i','',$SESSION["manager"]);//filter everything but numbers and letters
    $password=preg_replace('#[^A-Za-z0-9]#i','',$SESSION["password"]);//filter everything but numbers and letters
    //Run mySQL quesry to be sure that this person is an admin and that their password session var equals the database information
    //Connect to the MySQL database
    include"http://www.optiq-customtees.zxq.net/storeadmin/connect_to_mysql.php";
    $sql=mysql_query("SELECT*FROM admin WHERE id='$managerID' AND username='$manager' AND password='$password' LIMIT 1");//query the person
    //----- MAKE SURE PERSON EXISTS IN DATABASE ------
    $existCount=mysql_num_rows($sql);//count the row nums
    if ($existCount==0){// evaluate the count
    	header("location:http://www.optiq-customtees.zxq.net/storeadmin/index.php");
    	exit();
    	}
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Store Admin</title>
    <link rel="http://www.optiq-customtees.zxq.net/style/style.css" type="text/css" />
    </head>
    
    <body>
    <div align="center" id="mainWrapper">
    <?php include_once("http://www.optiq-customtees.zxq.net/template_header.php");?>
    	<div id="pageContent">
    		<div align="left" style="margin-left:24px;">
            	<h2>Hello Store manager what would you like to do today?</h2>
                <a href="#">Manage Inventory</a><br />
                <a href="#">Manage blahblahblah</a>
            </div>
    	</div>
        <br />
        <br />
    <?php include_once("http://www.optiq-customtees.zxq.net/template_footer.php");?>
    
    </div>
    
    </body>
    </html>

    admin_login
    Code:
    <?php
    session_start();
    if(!isset($_SESSION["manager"])){
    	header("location:'http://www.optiq-customtees.zxq.net/storeadmin/admin_index.php'");
    	exit();
    	}
    ?>
    <?php
    //Parse the log in form if the user has filled it out and pressed "Log In"
    if(isset($_POST["username"])&&isset($POST["password"])){
    	
    	$manager=preg_replace('#[^A-Za-z0-9_]#i','',$_POST["username"]);//filter everything but numbers and letters
    	$password=preg_replace('#[^A-Za-z0-9]#i','',$_POST["password"]);//filter everything but numbers and letters
    	//Connect to the MySQL database
    	include"http://www.optiq-customtees.zxq.net/storeadmin/connect_to_mysql.php";
    	$sql=mysql_query("SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1");//query the person
    	//---- MAKE SURE PERSON EXISTS IN DATABASEE ----
    	$existCount=mysql_num_rows($sql);//count rows nums
    	if($existCount==1){//evaluate the count
    		while($row=mysql_fetch_array($sql)){
    			$id=$row["id"];	
    		}
    		$_SESSION["id"]=$id;
    		$_SESSION["manager"]=$manager;
    		$_SESSION["password"]=$password;
    		header("location:http://www.optiq-customtees.zxq.net/storeadmin/admin_index.php");
    		exit();
    	}else{
    		echo'That information is incorrect, try again <a href="http://www.optiq-customtees.zxq.net/storeadmin/admin_index.php">Click Here</a>';
    		exit();}}?>
    
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Store Admin</title>
    <link rel="http://www.optiq-customtees.zxq.net/style/style.css" type="text/css" />
    </head>
    
    <body>
    <div align="center" id="mainWrapper">
    	<?php include_once("http://www.optiq-customtees.zxq.net/template_header.php");?>
    	<div id="pageContent"><br />
    		<div align="left" style="margin-left:24px;">
            	<h2>Please Log In To Manage Store</h2>
                <form id="form1" method="post" action="admin_login.php">
                	User Name:<br />
                    <input name="username" type="text" id="username" size="40" />
                    <br /><br />
                    Password:<br />
                    <input name="password" type="password" id="password" size="40" />
                    <br />
                    <br />
                    <br />
                    <input type="submit" name="button" id="button" value="Log In" />
                </form>
                <p>&nbsp;</p>
            </div>
    	</div>
        <br />
        <br />
    	<?php include_once("http://www.optiq-customtees.zxq.net/template_footer.php");?>
    
    </div>
    </body>
    </html>
    I'm using dreamweaver cs6 to do all the coding and the hosting site I use has a login area to something called "sql buddy" where I can create admins and tables... which I can do without a problem... I just can't get the login part to work.... SOMEBODY PLEEEEEEEEEEASE HELP!!!!! I'VE BEEN STUCK ON THIS FOR A WEEK NOW!!!!!... thanks in advance..

  2. #2
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    You need to take it one step at a time. What error message do you get? I suspect it is "could not connect to mysql" or "no database". Can you confirm this?
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

  3. #3
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    first off this is a PHP question NOT a MySQL question, so it would be better asking the question there

    As Ronan says in order to help we need to know what the MySQL error number is and what the message says
    change your "or die" to something like
    PHP Code:
    or die ("could not connect to mysql. Error:".mysql_errno().": ".mysql_error()); 
    Bear in mind you don't want to be displ;aying error numbers and messages on your live site

    sometimes it helps to actually display what the relevant data is
    ie the db, server & password OR the actual SQL

    reasons for not being able to establish a connection are usually down to
    there is a typo (mispelling, incorrect capitalisation)
    OR
    the user isn't defined in the MySQL DB
    there are no permissions, or no correct permissions for that user
    I'd rather be riding on the Tiger 800 or the Norton

  4. #4
    Join Date
    Oct 2012
    Posts
    10
    ok.... see... you guys taught me something new.... I thought this was where I'd post this.. lol... but anyways... now I'm getting a message that says:

    The webpage at http://optiq-customtees.zxq.net/stor...dmin_index.php has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.

    I did as the prompt said but it's STILL doing the same thing... the other page that creates the loop is admin_login.php.....

  5. #5
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    so you have various problems with your PHP
    Code:
    Warning: include_once() [function.include-once]: URL file-access is disabled in the server configuration in /www/zxq.net/o/p/t/optiq-customtees/htdocs/index.php on line 11
    
    Warning: include_once(http://www.optiq-customtees.zxq.net/mystore/template_header.php) [function.include-once]: failed to open stream: no suitable wrapper could be found in /www/zxq.net/o/p/t/optiq-customtees/htdocs/index.php on line 11
    
    Warning: include_once() [function.include]: Failed opening 'http://www.optiq-customtees.zxq.net/mystore/template_header.php' for inclusion (include_path='.:/usr/lib/php') in /www/zxq.net/o/p/t/optiq-customtees/htdocs/index.php on line 11
    
    
    Warning: include_once() [function.include-once]: URL file-access is disabled in the server configuration in /www/zxq.net/o/p/t/optiq-customtees/htdocs/index.php on line 13
    
    Warning: include_once(http://www.optiq-customtees.zxq.net/mystore/template_footer.php) [function.include-once]: failed to open stream: no suitable wrapper could be found in /www/zxq.net/o/p/t/optiq-customtees/htdocs/index.php on line 13
    
    Warning: include_once() [function.include]: Failed opening 'http://www.optiq-customtees.zxq.net/mystore/template_footer.php' for inclusion (include_path='.:/usr/lib/php') in /www/zxq.net/o/p/t/optiq-customtees/htdocs/index.php on line 13
    I'd rather be riding on the Tiger 800 or the Norton

  6. #6
    Join Date
    Oct 2012
    Posts
    10
    WOW!!!!! HOW DID YOU GET ALL THAT??!!!!... HOW CAN IT BE FIXED?.... I took a screen shot of my dreamweaver window to show you guys something I noticed.... in the upper left hand corner there's some coding showing...

    Click image for larger version. 

Name:	error01.jpg 
Views:	4 
Size:	111.2 KB 
ID:	13614

    the part of the coding this is from is at the bottom of this

    PHP Code:
    <?php
    //Parse the log in form if the user has filled it out and pressed "Log In"
    if(isset($_POST["username"])&&isset($POST["password"])){
        
        
    $manager=preg_replace('#[^A-Za-z0-9_]#i','',$_POST["username"]);//filter everything but numbers and letters
        
    $password=preg_replace('#[^A-Za-z0-9]#i','',$_POST["password"]);//filter everything but numbers and letters
        //Connect to the MySQL database
        
    include"../storescripts/connect_to_mysql.php";
        
    $sql=mysql_query("SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1");//query the person
        //---- MAKE SURE PERSON EXISTS IN DATABASEE ----
        
    $existCount=mysql_num_rows($sql);//count rows nums
        
    if($existCount==1){//evaluate the count
            
    while($row=mysql_fetch_array($sql)){
                
    $id=$row["id"];    
            }
            
    $_SESSION["id"]=$id;
            
    $_SESSION["manager"]=$manager;
            
    $_SESSION["password"]=$password;
            
    header("location:admin_index.php");
            exit();
            }
        else{
            echo
    'That information is incorrect, try again <a href="admin_index.php">Click Here</a>';
            exit();
            }
        }
    ?>
    what's wrong here?

  7. #7
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    I don't know
    what error message(s) are you getting
    what line is the script dieing on
    where is the problem

    Its virtually impossible for anyone to look through a piece of code and identify why it working at runtime.

    as said before you need to investigate that you are making a MySQL connection (check the values in ../storescripts/connect_to_mysql.php

    whilst debuggihn your PHP scripts turn on the maximum level of error_reporting

    take it one step at a time
    I'd rather be riding on the Tiger 800 or the Norton

  8. #8
    Join Date
    Oct 2012
    Posts
    10
    sorry... I just didn't know enough yet to be able to really say anything more... but I figured out my sql connection page is fine.. I coded some other pages for creating tables in the database which use the connection page and they all went through.. the error I'm getting now is......... Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.... as far as what line it's dieing on.... how can I figure that out?... I wasn't getting any feedback with the error message showing me that.. thanks for your reponse..

  9. #9
    Join Date
    Oct 2012
    Posts
    10
    ok... I just googled error reporting and came up with this code

    error_reporting(E_ALL|E_STRICT);

    I tried it both at the top and bottom of each php script and didn't see anything new in the browser window... it's the same error message...

  10. #10
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    error to may redirects is a PHP error
    https://www.google.co.uk/#hl=en&sa=X...w=1920&bih=952
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

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