Using PDO to format mysql_connect correctly?

This is my current page:

<?php mysql_connect('localhost', 'root', 'mypass') or die (mysql_error()); mysql_select_db('radio1') or die (mysql_error()); $result = mysql_query("SELECT *, TIME_FORMAT(airtime, '%H:%i') `airtime` from presenters"); //Table starting tag and header cells while($row = mysql_fetch_array($result)) { ?> <?php foreach($rows as $row):?> <dl class="standard"> <dt><a href="<?=$row=['link'] ?>" title="<?=$row=['presenter'] ?>"><?=$row['airtime'] . " - " .$row['presenter']?></a></dt> <dd class="itemimg"><a href="<?=$row=['link'] ?>" title="<?=$row=['presenter'] ?>"><img src="<?=$row['image']; ?>" width="100" height="75" alt="<?=$row=['presenter'] ?>" title="<?=$row=['presenter'] ?>" /></a></dd> <dd class="itemdesc"> <?=$row['showinfo']; ?> </dd> <dd class="itemlink"> <a href="<?=$row=['link'] ?>" title="Find out more..."><span> </span> <?=$row['more']; ?></a> </dd> </dl> <?php endforeach;?> 

I want to convert this to code that works with PDO since it is included in my php.ini

How can I get PDO to work with this, as I intend (for this project and all future ones) to abandon the use of the older mysql_connect.

I looked at how to do this in the Zend Developer Zone , and although I can do it at a middle level for project-based Dwoo, this template does not use the template engine - it is pure PHP based syntax, without using templates, only various include() and require, plus echo() where necessary.

Any help is appreciated!

+6
source share
2 answers

Here is your solution.

 <?php $hostname = "localhost"; $username = "root"; $password = "mypass"; $dbname = 'radio1'; $dbh =null; try { $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password); } catch(PDOException $e) { echo $e->getMessage(); } $result = $dbh->query("SELECT *, TIME_FORMAT(airtime, '%H:%i') `airtime` from presenters"); //Table starting tag and header cells while($row = $result->fetch ()) { ?> <?php foreach($rows as $row):?> <dl class="standard"> <dt><a href="<?=$row=['link'] ?>" title="<?=$row=['presenter'] ?>"><?=$row['airtime'] . " - " .$row['presenter']?></a></dt> <dd class="itemimg"><a href="<?=$row=['link'] ?>" title="<?=$row=['presenter'] ?>"><img src="<?=$row['image']; ?>" width="100" height="75" alt="<?=$row=['presenter'] ?>" title="<?=$row=['presenter'] ?>" /></a></dd> <dd class="itemdesc"> <?=$row['showinfo']; ?> </dd> <dd class="itemlink"> <a href="<?=$row=['link'] ?>" title="Find out more..."><span> </span> <?=$row['more']; ?></a> </dd> </dl> <?php endforeach;?> 
+4
source

using the dev code below

// At the end of the code you must free resources

// Free resources used

$ result-> closeCursor ();

$ dbh = null;

-1
source

Source: https://habr.com/ru/post/895983/