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
<!-- // ---------------------------------------------------------------------------- // // Small example on connecting to a MS SQL Server, from PHP, using the // Mssql database connector and a HTML-form. // --> <html> <head> </head> <body> <fieldset> <legend>Search Cars!</legend> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get"> <p>Write string to search for (nameCars or modelCars) and press Search</p> <table> <tr> <td><label for="search">Searchstring:</label></td> <td><input type="text" name="search" value="<?php echo $_GET['search']?>"></td> </tr> <tr> <td></td> <td style="text-align: right"><button type="submit" name="submit">Search</button></td> </tr> </table> </form> </fieldset> <?php // ---------------------------------------------------------------------------- // // 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 "<hr><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 // $search = $_GET['search']; $query = "SELECT * FROM Cars WHERE nameCars LIKE '%{$search}%' OR modelCars LIKE '%{$search}%';"; if(empty($search)) { echo "Nothing to search for. Exiting..."; exit; } $res = mssql_query($query) or 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++; } // ---------------------------------------------------------------------------- // // Close the connection to the database // mssql_close($conn); //exit; ?> </body> </html>
. ..: © Mikael Roos, 2009