Hem | Visa källkod | XHTML | CSS | How to become a hacker

Visa källkod

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

mysql1.php
<?php
// ----------------------------------------------------------------------------
//
// Small example on connecting to a MySQL Server, from PHP, using the
// mysqli 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 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);
*/


// ----------------------------------------------------------------------------
//
// Show this source code for those intrested
//

echo <<<EOD
<p>
<a href='index.php'>Home</a> |
Show source code: 
<a href='source.php?dir=&file=mysql1.php'>mysql1.php</a>
<a href='source.php?dir=&file=config.php'>config.php</a>
</p>
EOD;


// ----------------------------------------------------------------------------
//
// Connect to the database server
//

// 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
//
$query = "SELECT * FROM Cars;";

$res = $mysqli->query($query) 
                    or die("Could not query database");

echo "<p>Query={$query}</p><p>Number of rows in resultset: " . $res->num_rows . "</p>";


// ----------------------------------------------------------------------------
//
// Show the results of the query
//
$i = 1;

while($row = $res->fetch_object()) {
        echo $i . ": " . $row->idCars . " - " . $row->nameCars . " - " .$row->modelCars . " - " . $row->readyCars . " - " . $row->hoursCars . "<br/>";
        $i++;
    }

$res->close();


// ----------------------------------------------------------------------------
//
// 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 = NOW() WHERE idCars < 10;";

$res = $mysqli->query($query) 
                or die("Could not query database");

echo "<p>Query={$query}</p><p>Number of rows affected by last query: " . $mysqli->affected_rows . "</p>";


// ----------------------------------------------------------------------------
//
// Close the connection to the database
//
$mysqli->close();

exit;


?>
 .                        
..:    © Mikael Roos, 2009