My English is not so well and I am new with php. So, maybe you can be bored with my question. I am really sorry for that.
Here’s the Question: I have a table created in SQL database like this:
- ID Name PICTURE - 1 John john.jpg - 1 dora h.jpg - 1 Dane hkjpg - 2 Mougli f.jpg - 3 Mona g.jpg
and I want to show the data in a table created in my web page like this:
- ID Name PICTURE - 1 John john.jpg - 1 dora h.jpg - 1 Dane hkjpg
I Want to show all the information in my web page which has the id “1” and others will not be in the table. I want to use a loop to do that. If you know how to do that then please try to answer with the full php code( I mean the code should start from connecting database to the page and end to looping the information in the table. Of-course it depends on you if you want to write the full code. But it would be very helpful for me).
Thanks in advance.
Advertisement
Answer
I would recommend using PDO: https://www.php.net/manual/en/class.pdo.php You want to start with the __construct and query functions. Query will return a PDOStatement object that you can fetch. Your code should look like this:
// Connect $host = '127.0.0.1'; $db = 'test'; $user = 'root'; $pass = ''; $charset = 'utf8mb4'; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $pdo = new PDO($dsn, $user, $pass, $options); // Query $statement = $pdo->query("SELECT * FROM your_table WHERE ID = 1"); // Fetch $data = $statement->fetchAll(); foreach ($data as $row) { echo $row['ID'].', '.$row['name'].', '.$row['PICTURE']."<br />n"; }