I am using Xampp localhost. How can I select the database inside the database structure? here is my code.
<?php
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=lifeands_phdb_150522", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
I want to select database name loan_calculator. Here is the picture of the database.
Any one can help?
to fetch from the table "loan_calculator" of "lifeands_phdb_150522" DB. select the DB and write the query to fetch data from table
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=lifeands_phdb_150522", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
//to query table
$sql = 'SELECT * FROM loan_calculator';
$res = $conn->query($sql, PDO::FETCH_ASSOC);
foreach ($res as $row) {
echo "<pre>";
print_r($row);
echo "</pre>";
}
You can simply try this
if (!$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
echo 'Could not connect to mysql';
exit;
}
if (!mysql_select_db('lifeands_phdb_150522', $link)) {
echo 'Could not select database';
exit;
}
$sql = 'SELECT * FROM loan_calculator';
$result = mysql_query($sql, $link);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.