PHP-mysqli select output

PHP-mysqli select output

PHP-mysqli select output

 

<?php
/*
http://php.net/manual/en/mysqli.quickstart.statements.php
*/
$mysqli = new mysqli(“example.com”, “user”, “password”, “database”);
if ($mysqli->connect_errno) {
echo “Failed to connect to MySQL: (” . $mysqli->connect_errno . “) ” . $mysqli->connect_error;
}

if (!$mysqli->query(“DROP TABLE IF EXISTS test”) ||
!$mysqli->query(“CREATE TABLE test(id INT)”) ||
!$mysqli->query(“INSERT INTO test(id) VALUES (1), (2), (3)”)) {
echo “Table creation failed: (” . $mysqli->errno . “) ” . $mysqli->error;
}

$res = $mysqli->query(“SELECT id FROM test ORDER BY id ASC”);

echo “Reverse order…\n”;
for ($row_no = $res->num_rows – 1; $row_no >= 0; $row_no–) {
$res->data_seek($row_no);
$row = $res->fetch_assoc();
echo ” id = ” . $row[‘id’] . “\n”;
}

echo “Result set order…\n”;
$res->data_seek(0);
while ($row = $res->fetch_assoc()) {
echo ” id = ” . $row[‘id’] . “\n”;
}
?>

 

//—————–//

<?php
/*
http://www.w3schools.com/php/php_mysql_select.asp
*/
$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “myDB”;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: ” . $conn->connect_error);
}

$sql = “SELECT id, firstname, lastname FROM MyGuests”;
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo “<br> id: “. $row[“id”]. ” – Name: “. $row[“firstname”]. ” ” . $row[“lastname”] . “<br>”;
}
} else {
echo “0 results”;
}

$conn->close();
?>

 

 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *