How to Fetch database info using php code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to Fetch database info using php code?

14th Nov 2016, 3:34 PM
Sathish Kumar
Sathish Kumar - avatar
3 Answers
+ 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);
14th Nov 2016, 3:44 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
+ 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.
14th Nov 2016, 9:59 PM
Edwin Martinez
Edwin Martinez - avatar
0
Thank you Bro!!
14th Nov 2016, 3:50 PM
Sathish Kumar
Sathish Kumar - avatar