+ Reply to Thread
Results 1 to 5 of 5

Thread: PHP HELP - 'cannot connect'

  1. #1
    pspost is offline x10Hosting Member pspost is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    1

    Question PHP HELP - 'cannot connect'

    Im vary new to php I'm doing ok on geting my surver up and the code but I cant get the surver to connect to my website!
    i dont know what im saposted to do this is what im puting down

    the **** is blocked content but you get the idea

    <?php
    $host=http://chopin.x10hosting.com:****; // Host name
    $username="*"; // Mysql username
    $password="*"; // Mysql password
    $db_name="pspost_database"; // Database name ^thats the database name
    $tbl_name="members"; // Table name
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    // username and password sent from form
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];
    // To protect MySQL injection
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("myusername");
    session_register("mypassword");
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>

    I dont know what's the ulr of my data base can some one help me
    maby someone that had the problem before?
    thank you :dunno::dunno::dunno::dunno:

    pleas do not take my code with out asking me thank you

  2. #2
    leafypiggy's Avatar
    leafypiggy is offline Community Advocate leafypiggy is on a distinguished road
    Join Date
    Aug 2007
    Location
    Massachusetts
    Posts
    2,228

    Re: PHP HELP - 'cannot connect'

    PHP Code:
    <?php
    $host
    ="localhost"// Host name
    $username="*"// Mysql username
    $password="*"// Mysql password
    $db_name="pspost_database"// Database name ^thats the database name
    $tbl_name="members"// Table name
    // Connect to server and select databse.
    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    // username and password sent from form
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];
    // To protect MySQL injection
    $myusername stripslashes($myusername);
    $mypassword stripslashes($mypassword);
    $myusername mysql_real_escape_string($myusername);
    $mypassword mysql_real_escape_string($mypassword);
    $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
    $result=mysql_query($sql);
    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);
    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("myusername");
    session_register("mypassword");
    header("location:login_success.php");
    }
    else {
    echo 
    "Wrong Username or Password";
    }
    ?>
    Last edited by leafypiggy; 03-13-2009 at 02:34 PM.
    Neil Hanlon | x10Hosting Support Representative
    Neil[at]x10hosting.com
    █ I'm always happy to help. Just ask a question in Free Hosting
    Terms of Service IRC

  3. #3
    Alice_Hoyden is offline x10Hosting Member Alice_Hoyden is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    6

    Re: PHP HELP - 'cannot connect'

    Add this to the top: error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
    Then if the database is on your site as well (which it seems to be), the host name would be 'localhost' and you should use single quotes for the values, not double quotes.

    It also might be a good idea to put the connection details and the connection function into a separate php file which will be included into every file that requires a database connection (include 'connectiondetailsfile.php')

    here's a connection function:
    connecttodb($servername,$dbname,$dbusername,$dbpas sword);
    function connecttodb($servername,$dbname,$dbuser,$dbpasswor d)
    {
    global $link;
    $link=mysql_connect ("$servername","$dbuser","$dbpassword");
    if(!$link){die("Could not connect to MySQL");}
    mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
    }
    set the values before you call it. $servername='localhost' $dbuser='yoursiteCpanelloginname_databasename' (probably, in any case there will be a username involved)
    $dbpassword='password associated with the username for that database'

    Hope that helps.

  4. #4
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: PHP HELP - 'cannot connect'

    Alice, here is an updated version of you function, which doesn't use globals:
    PHP Code:
    function connectToDb($servername$dbname$dbuser$dbpassword) {
    $link mysql_connect($servername$dbuser$dbpassword);
    if(!
    $link) {
    die(
    "Could not connect to MySQL: " mysql_error());
    }
    $db mysql_select_db($dbname$link);
    if(!
    $db) {
    die (
    "Cannot use " $dbname " : " mysql_error());
    }
    return 
    $link;

    I would also recommend using mysqli instead.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  5. #5
    sapotest is offline x10Hosting Member sapotest is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    1

    Re: PHP HELP - 'cannot connect'

    My php-mysql script create.php placed under /home/sapo/public_html and given 755 privileges causes a Gateway Timeout when run. I have tested simple non-db php scripts which work just fine.

    I have created a database named sapo_testingdb and a db user named sapo_testing which has been added and given full privileges for the database sapo_testingdb. The database is shown correctly in the phpMyAdmin panel as well.

    My code for create.php is as follows.

    Code:
    <?php
    echo "Creating database... ";
    
    $username="sapo_testing";
    $password="notthis";
    $database="sapo_testingdb";
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    
    $query="CREATE TABLE Tag (id int(6) NOT NULL auto_increment,uid varchar(100),PRIMARY KEY (id),UNIQUE id (id))";
    mysql_query($query);
    mysql_close();
    
    echo "Database created";
    ?>
    My domain is called sapo and when I create a database with an intended name testingdb then it is changed to sapo_testingdb, the same for the user testing. I believe this is supposed to happen, but I have tried all variations with and without the prefix sapo_ for both the username and database, and all causes a timeout. As I've said, other php scripts without database access run just fine.

    Why does this give a Gateway Timeout? Your help is appreciated, since I cannot find a previous post or thread which could help me.

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. My 2cents on Ruby vs PHP.
    By jwillia in forum Programming Help
    Replies: 0
    Last Post: 03-15-2008, 11:18 PM
  4. Important PHP Information
    By Bryon in forum News and Announcements
    Replies: 0
    Last Post: 11-21-2007, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers