+ 2
How to Fetch database info using php code?
3 Respuestas
+ 3
First of all connect to MySQL
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
if (!mysqli_select_db('mysql_dbname', $conn)) {
echo 'Could not select database';
exit;
}
$conn = 'SELECT foo FROM bar WHERE id = 42';
$result = mysqli_query($sql, $conn);
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysqli_error();
exit;
}
while ($row = mysqli_fetch_assoc($result)) {
echo $row['foo'];
}
mysqli_free_result($result);
+ 1
Using the example above, if you want to retrieve everything from a user, for example, the query is like this: "SELECT * FROM myTable WHERE id=23"
The * symbol means that is selecting everything from the table myTable where the user id is equal 23.
Also remember that SQL queries can be a little different between databases.
0
Thank you Bro!!