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 MySQL Server, from PHP, using the // mysqli database connector and a HTML-form. // --> <html> <head> </head> <body> <p> <a href='index.php'>Home</a> | Show source code: <a href='source.php?dir=&file=mysql2.php'>mysql2.php</a> <a href='source.php?dir=&file=config.php'>config.php</a> </p> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get"> <fieldset> <legend>Search Cars!</legend> <p>Write string to search for (nameCars or modelCars) and press Search</p> <table> <tr> <td><label for="search">Searchstring:</label></td> <td><input id="search" 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> </fieldset> </form> <?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 IF EXISTS; CREATE TABLE Cars ( idCars INT AUTO_INCREMENT, 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(nameCars, modelCars, readyCars, hoursCars) VALUES('Volvo', 'V70', '2007-09-01 12:50', 34.5); INSERT INTO Cars(nameCars, modelCars, readyCars, hoursCars) VALUES('Volvo', 'V40', '2007-08-15 15:45', 33); INSERT INTO Cars(nameCars, modelCars, readyCars, hoursCars) VALUES('Volvo', 'V70', '2007-08-01 13:15', 36); INSERT INTO Cars(nameCars, modelCars, readyCars, hoursCars) VALUES('Volvo', 'Amazon', '2007-08-11 08:50', 59.3); INSERT INTO Cars(nameCars, modelCars, readyCars, hoursCars) VALUES('Volvo', 'Amazon', '2007-08-07 10:08', 62); INSERT INTO Cars(nameCars, modelCars, readyCars, hoursCars) VALUES('Saab', '9000', '2007-08-25 14:50', 42); INSERT INTO Cars(nameCars, modelCars, readyCars, hoursCars) VALUES('Saab', '99', '2007-09-02 12:05', 45); INSERT INTO Cars(nameCars, modelCars, readyCars, hoursCars) VALUES('Saab', '9000', '2007-08-27 14:22', 41); */ // ---------------------------------------------------------------------------- // // Connect to the database server // // http://php.net/manual/en/book.mysqli.php // http://php.net/manual/en/mysqli.connect-error.php // // Host, database, user and password stored in separate file require_once('config.php'); $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE); if (mysqli_connect_error()) { echo "Connect failed: ".mysqli_connect_error()."<br>"; exit(); } echo "<p>Connected successfully</p>"; // ---------------------------------------------------------------------------- // // Prepare and perform a SELECT query // // http://php.net/manual/en/mysqli.query.php // http://php.net/manual/en/mysqli.real-escape-string.php // http://php.net/manual/en/mysqli.error.php // http://php.net/manual/en/mysqli-result.num-rows.php // // Sanitize data $search = $mysqli->real_escape_string($_GET['search']); // Prepare query $query = "SELECT * FROM Cars WHERE nameCars LIKE '%{$search}%' OR modelCars LIKE '%{$search}%';"; if(empty($search)) { echo "Nothing to search for. Exiting..."; exit; } // Execute query $res = $mysqli->query($query) or die("Could not query database, query =<br/><pre>{$query}</pre><br/>{$mysqli->error}"); echo "<p>Query={$query}</p><p>Number of rows in resultset: " . $res->num_rows . "</p>"; // ---------------------------------------------------------------------------- // // Show the results of the query // // http://php.net/manual/en/mysqli-result.fetch-object.php // http://php.net/manual/en/mysqli.close.php // $i = 1; while($row = $res->fetch_object()) { echo $i . ": " . $row->idCars . " - " . $row->nameCars . " - " . $row->modelCars . " - " . $row->readyCars . " - " . $row->hoursCars . "<br/>"; $i++; } $res->close(); // ---------------------------------------------------------------------------- // // Close the connection to the database // // http://php.net/manual/en/mysqli.close.php // $mysqli->close(); //exit; ?> </body> </html>
. ..: © Mikael Roos, 2009