Connecting to an SQL database

db , php United Kingdom
  • 13 years ago

    It seems like the simplest code in the world and it works too but then when I refresh... it doesn't.... odd indeed. I will then go back onto the site 5/10 minutes later and it will work again... until I refresh.... then it doesn't. Very frustrating....

    I guess it is something I am not doing which is then leaving the connection open for some reason even though I close it down at the bottom of the script. The only reference I can find at all is that it will return false if the connection already exists....

     Any ideas anybody?

    Thanks

    Tom.

     <?php

    ##TitleQuick list of blog entries from SQL Server DB...
    $myServer = "***.***.***.***";
    $myUser = "********";
    $myPass = "********";
    $myDB = "********";

    $s = @mssql_connect($myServer, $myUser, $myPass)
    or die("Boom, Can't even connect to the server!.");
    echo "Connected to $myServer<br />";

    $d = @mssql_select_db($myDB, $s)
    or die("Boom, shackalak. db select failed.");
    echo "Connected to $myDB<br />";

    $query = "SELECT Title, blogid FROM Blog";
    $result = mssql_query($query);
    $numRows = mssql_num_rows($result);

    echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

    $text = "<table border=0>";
    while($row = mssql_fetch_array($result))
    {
     $text .= "<tr><td>" . $row["blogid"] . "</td><td>" . $row["Title"] . "</td></tr>";
    }
    $text .= "</table>";
    #Close the connection...
    mssql_close($s);


    echo "Disconnected from $myServer<br />";

    #Print the result.
    echo $text;

    ?>

  • 13 years ago

    Hello,

     This link might be helpful to you...

     

    http://searchcode.tm-sol.com/codeid.php?Code_ID_Code=106&dowhattodo=&pageid=1&skey=how+to+connect+database+in+php

     

    This gives an example of how to connect to database. Let me know if this is too complex, will try to find some more examples for you.

     

    Cheers

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is anticipated with distaste, performed with reluctance, and bragged about forever.” - Dan Kaminsky