What's with this code?

Discussion in 'Web Design and Development' started by wfoster, Apr 3, 2009.

  1. wfoster macrumors 6502a

    wfoster

    Joined:
    Feb 16, 2009
    Location:
    Plymouth, UK
    #1
    http://wfoster.co.uk/talk.php

    Is it because it cannot connect to the database? I have a mySQL DB, not sure if my sql.php file has the right information in? This is my sql.php file:

    Code:
    <?
    $mysql_host = "localhost";
    $mysql_database = "web231-wfoster";
    $mysql_user = "web231-wfoster";
    $mysql_password = "************";
    $MyDB = "web231-wfoster";
    
    $dieMysqlMessage = 'Could not connect to database.<br />';
    
    if(!mysql_connect($MySqlhostname, $MySqlusername, $MySqlpassword,  $MyDB))
    {
    	die($dieMysqlMessage . mysql_error());
    }
    		
    $con = mysql_connect($MySqhostname, $MySqlusername, $MySqlpassword);
    mysql_select_db($MyDB, $con);
    ?>
    Is it correct or how can I find out all my information for it? (Yes, I'm new to coding).

    Thanks
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    You don't seem to be making use of your variables there. Also you need <?php, not just <?. Try this,
    PHP:
    <?php
    $mysql_host 
    "localhost";
    $mysql_database "web231-wfoster";
    $mysql_user "web231-wfoster";
    $mysql_password "************";

    $con mysql_connect$mysql_host$mysql_user$mysql_password ) or
      die (
    "Error connecting: "mysql_error());
    mysql_select_db$mysql_database$con ) or
      die (
    "Error selecting DB: "mysql_error());
    ?>
    If it doesn't you should get one of the error messages.
     
  3. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    Variable names are case sensitive.
     
  4. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #4
    PHP:
    <?php

    $mysql_host 
    "localhost";
    $mysql_database "web231-wfoster";
    $mysql_user "web231-wfoster";
    $mysql_password "************"// your actual password 

    $con mysql_connect($mysql_host$mysql_user$mysql_password
               or die (
    "Could not connect to database.<br />Error: " mysql_error());
    mysql_select_db($mysql_database$con)
               or die (
    "Could not select the database.");

    ?>
    Untested but fixed code based on responses above including some consolidation on my part (i.e. why define a variable to hold text for the die function when you can include it in the function itself?) to streamline your code. As you learn more about coding, that's one of my golden rules - less code is better than more code to accomplish the same task. Makes for faster runtime and easier debugging, when possible of course.

    -jim
     

Share This Page