Nedanstående filer finns i denna katalogen. Klicka på en fil för att visa dess innehåll.
php-to-db/
common.php config.php index.php mssql1.php mssql1.phps mssql1.php~ mssql2.php mssql2.phps mysql1.php mysql2.php source.php
<?php // ---------------------------------------------------------------------------- // // Small example on connecting to a MS SQL Server, from PHP, using the // Mssql database connector. // // ---------------------------------------------------------------------------- // // SQL code to Create the table used in the example // /* -- -- Create a table containing cars, when they were manufactored and -- the amount of hours it took to manufactor it. -- DROP TABLE Cars; CREATE TABLE Cars ( idCars INT IDENTITY(1,1), nameCars CHAR(20) NOT NULL, modelCars CHAR(20) NOT NULL, readyCars DATETIME NOT NULL, hoursCars FLOAT(5) NOT NULL, PRIMARY KEY (idCars) ) -- -- Insert some basic values into the table -- INSERT INTO Cars VALUES('Volvo', 'V70', '2007-09-01 12:50', 34.5); INSERT INTO Cars VALUES('Volvo', 'V40', '2007-08-15 15:45', 33); INSERT INTO Cars VALUES('Volvo', 'V70', '2007-08-01 13:15', 36); INSERT INTO Cars VALUES('Volvo', 'Amazon', '2007-08-11 08:50', 59.3); INSERT INTO Cars VALUES('Volvo', 'Amazon', '2007-08-07 10:08', 62); INSERT INTO Cars VALUES('Saab', '9000', '2007-08-25 14:50', 42); INSERT INTO Cars VALUES('Saab', '99', '2007-09-02 12:05', 45); INSERT INTO Cars VALUES('Saab', '9000', '2007-08-27 14:22', 41); */ // ---------------------------------------------------------------------------- // // Show this source code for those intrested // echo "<p><a href='{$_SERVER['PHP_SELF']}s'>View the php-source</a></p>"; // ---------------------------------------------------------------------------- // // Connect to the database server // $conn = mssql_connect('194.47.135.20', 'mos_0', 'programvaruteknik') or die("Could not connect !"); echo "<p>Connected successfully</p>"; // ---------------------------------------------------------------------------- // // Prepare and perform a SELECT query // $query = "SELECT * FROM Cars;"; $res = mssql_query($query); if(!$res) { mssql_get_last_message(); die("Could not query database"); } echo "<p>Query={$query}</p><p>Number of rows in resultset: " . mssql_num_rows($res) . "</p>"; // ---------------------------------------------------------------------------- // // Show the results of the query // $i = 1; while($row = mssql_fetch_object($res)) { echo $i . ": " . $row->idCars . " - " . $row->nameCars . " - " .$row->modelCars . " - " . $row->readyCars . " - " . $row->hoursCars . "<br/>"; $i++; } // ---------------------------------------------------------------------------- // // Prepare and perform a INSERT/UPDATE query (or a stored procedure) // //$query = "exec SPInsertProducedCar @aModel='V70', @aHours=888;"; //$query = "INSERT INTO Cars VALUES('Puch', 'Dakota', getdate(), 666);"; $query = "UPDATE Cars SET readyCars = getdate() WHERE idCars < 10;"; $res = mssql_query($query) or die("Could not query database"); echo "<p>Query={$query}</p><p>Number of rows affected by last query: " . mssql_rows_affected($conn) . "</p>"; // ---------------------------------------------------------------------------- // // Close the connection to the database // mssql_close($conn); exit; ?>
. ..: © Mikael Roos, 2009