Monday, April 04, 2005

PHP would help...

** OK. After trying to post this about 6 times, despite the interface, I have given up trying to get Blogger to actually display html code as code. I'll note in the decsriptions below where the HTML belongs. **

It occurred to me this morning, that while I was working on all this web development stuff, and posting all this SQL code, I haven't posted any of the PHP for it yet. Well, really the crux of each PHP page is the SQL calls. Let's face it. Without dynamic data calls, the pages would be just a tad boring, now wouldn't they.


So, first you open the SQL connection with the following:

$connect = mssql_connect("DBSERVER") or die
("ERROR: Unable to connect to DB Server. Please provide this error message to a technical staff member.");

$db = mssql_select_db("DBNAME") or die
("ERROR: Unable to connect to database. Please provide this error message to a technical staff member.");

Note that the error messages should be marked up with the appropriate HTML.

The first variable, obviously is the connection to the server itself. The second is the database name that you want to connect to. Now, what you have is a variable assignment, followed by a logical or and then a 'die' command. Meaning, for the uninitated, 'Assign this variable, and if you can't, kill the script and output this error message.'


So now, that you have your variables, you have to actually connect:

mssql_connect($connection, $db);

Note that the syntax is C-like, and you have to end your statements with semicolons. mssql_connect is the function that performs the connection, and now that you've assigned your variables, it knows where to go. Now that you've connected, you can run your query. First, you assign a variable to the query.

$query = "select * from tablename where column = value";

Then, you run your query with this:

mssql_query($query, $connect);

Of course, to actually do something with the query, you have to assign it to a variable:

$results = mssql_query($query, $connect) or die
("ERROR: Unable to execute query. Please provide this error message to a technical staff member.");

Again, note that the error message must be marked up in HTML.

Note that all you have to provide is the connection variable, not the database variable. This is something that kind of bugs me about PHP, that you can't change databases mid-stream. What if I want to query more than one database on the same page? (I'll probably find a way to do this in about 24 hours and prove myself wrong. Or maybe not.)


Now that you have run your query, SQL returns rows to PHP. You need to tell PHP what to do with them, presumably to put them into a table of some kind to display to the user. You may or may not have multiple rows, so you need to allow for that by enclosing the output in a 'while' loop. Again, for the uninitiated, the while loop means that, while you still have rows in the result set, keep doing whatever I tell you to do. In this case, we've already created a table in the HTML and put in the column headers. Now we need to echo each row of the table as HTML code so the browser can print it up.

while ($col_results = mssql_fetch_array($results)) {
$column1= $col_results["column_name1"];
$column2= $col_results["column_name2"];
$column3= $col_results["column_name3"];
$column4= $col_results["column_name4"];
echo "$column1
$column2
$column3
$column4";
}

The column data should be marked up in HTML, as individual table cells, in the same row format as you specified with the headers.

Now that we've gotten our data out of the database, and displayed it properly, we can close our connection to the database, in case anyone else wants to use it. (If you're using temporary tables in your queries, this is a particularly good idea, since others won't be able to use the page until you close out of your browser.)


mssql_close($connect)

And that's how I do it. I'm sure there are more efficient or cleaner ways to do it, but again, I'm not a developer. I'm learning. Gimme time and play along.

Later.
-D.

No comments: