Announcement

Collapse
No announcement yet.

Really Stupid Question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Really Stupid Question

    OK All,

    Starting off with MySql and have a really stupid question. I have a DB set up but can't seem to even get the connection right...

    I have a page that I want to connect to the DB and just say ok but it does not work.. I ave an incling that it's the "localhost" bit but could be wrong..!!

    Code:
    <?php 
    
     $link=mysql_connect("localhost", "ash", "jessica1");
    	if(mysql_select_db("mysql", $link)) echo "connected to DB";
    		Else die ("Connection failed");
    ?>
    Could someone let me into the secret of connecting to my DB.

    Thanks
    Ash

  • #2
    Originally posted by comports View Post
    OK All,

    Starting off with MySql and have a really stupid question. I have a DB set up but can't seem to even get the connection right...

    I have a page that I want to connect to the DB and just say ok but it does not work.. I ave an incling that it's the "localhost" bit but could be wrong..!!

    Code:
    <?php 
    
     $link=mysql_connect("localhost", "ash", "password");
    	if(mysql_select_db("mysql", $link)) echo "connected to DB";
    		Else die ("Connection failed");
    ?>
    Could someone let me into the secret of connecting to my DB.

    Thanks
    Ash
    I'm no expert, but I would suggest something like:

    PHP Code:
    <?php 

     $link
    =mysql_connect("localhost""ash""password");
        if(
    mysql_select_db("mysql"$link)) {

    echo 
    "Connected to DB"; } else { die ("Can't connect to DB.");

    }

    ?>

    Comment


    • #3
      Thanks - Can't really see whats different from mine and it did not work anyway.

      Thanks for trying though.

      Anyone else..??

      Comment


      • #4
        The difference was the '{' and '}' tags. You need them to declare different sections of code, i.e. before and after the 'else' since you are declaring a different scenario. As far as I know, these are needed in any situation.

        Here's some working code off W3Schools you can refer to:

        PHP Code:
        <?php
        $con 
        mysql_connect("localhost""peter""abc123");
        if (!
        $con)
          {
          die(
        'Could not connect: ' mysql_error());
          }

        $db_selected mysql_select_db("test_db"$con);

        if (!
        $db_selected)
          {
          die (
        "Can\'t use test_db : " mysql_error());
          }

        mysql_close($con);
        ?>

        Comment


        • #5
          Originally posted by flesso View Post
          The difference was the '{' and '}' tags. You need them to declare different sections of code, i.e. before and after the 'else' since you are declaring a different scenario. As far as I know, these are needed in any situation.
          I don't know who told you that, but it's not true, here's an example - PHP If...Else Statements
          I prefer wrapping things in brackets after if/else, but it's in no way essential.

          Back to the problem in hand...
          Instead of having lots of "if" conditional tests it's easier to use the "or die" method as so:

          PHP Code:
          $connection mysql_connect('localhost','user','pass')or die('could not connect to server');
          mysql_select_db('database_name',$connection) or die('could not connect to database'); 

          Comment


          • #6
            Originally posted by smiffsoft View Post
            I don't know who told you that, but it's not true, here's an example - PHP If...Else Statements
            I prefer wrapping things in brackets after if/else, but it's in no way essential.

            Back to the problem in hand...
            Instead of having lots of "if" conditional tests it's easier to use the "or die" method as so:

            PHP Code:
            $connection mysql_connect('localhost','user','pass')or die('could not connect to server');
            mysql_select_db('database_name',$connection) or die('could not connect to database'); 
            What do you mean? I was applying the code to his problem, and it worked.

            Comment


            • #7
              Originally posted by flesso View Post
              What do you mean? I was applying the code to his problem, and it worked.
              Josh

              You don't need to use { } Josh if there is just one line of code to execute if the condition is met.

              Comment


              • #8
                Yeah, I was just pointing out that you don't need {} brackets all the time like you said you do. I usually prefer adding them anyway, but they are not needed for a single line of code after the conditional.

                Comment

                Working...
                X