Connecting to an SQL database

db , php United Kingdom
  • 12 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?




    ##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...

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

    #Print the result.
    echo $text;


  • 12 years ago


     This link might be helpful to you...



    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.




Post a reply

Enter your message below

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


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.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth