-1

I need help with mysql_query to pdo

$sql = mysql_query("SELECT * FROM songs");

while($row = mysql_fetch_array($sql))
{
    $result[] = $row;
}
Simone Nigro
  • 4,717
  • 2
  • 37
  • 72
Aivaras
  • 19
  • 5

1 Answers1

2

PDO basic query (read Migrate from the MySQL Extension to PDO)

// Step 1: Establish a connection
$db = new PDO("mysql:host=localhost;dbname=YourDB", "YourUser", "YourPassword");

// Step 2: Construct a query
$query = "SELECT * FROM songs";

// Step 3: Send the query
$qresult = $db->query($query);

// Step 4: Iterate over the results
while($row = $qresult->fetch(PDO::FETCH_ASSOC)) {
    $result[] = $row;
}

// Step 5: Free used resources
$qresult->closeCursor();
$db = null;
Simone Nigro
  • 4,717
  • 2
  • 37
  • 72