This question has been answered by dunnock. See the first response.
//Directly with query
$q = "SELECT something.id FROM some_table WHERE id = 1";
while ($modx->pdo->query($q) as $row) {
}
// With prepared statement (lot safer generally
$q = "SELECT something.id FROM some_table WHERE id = :id";
$stmt = $modx->pdo->prepare($q);
$stmt->execute(array(':id' => 1));
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
}
$stmt->closeCursor();
<?php
function elements_modsnippet_3($scriptProperties= array()) {
global $modx;
if (is_array($scriptProperties)) {
extract($scriptProperties, EXTR_SKIP);
}
// Make the query
// $result = $modx->db->query('SELECT * FROM b2rmodx2.b2rmodx2_station_affiliates ORDER BY id ASC LIMIT 3');
$result = $modx->query('SELECT * FROM b2rmodx2.b2rmodx2_station_affiliates ORDER BY id ASC LIMIT 3');
// $query = "SELECT * FROM station_affiliates ORDER BY id ASC LIMIT 3";
// $result = @mysql_query($query); // Run the query
// $result = @mysql_query($result); // Run the query
if ($result) {
// If the query ran OK, display the result
echo '<table width="160" border="0" cellpadding="0" cellspacing="0" bgcolor="666666" class="affiliates">
<tr>
<td><div align="center"><font color="ffffff" size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Boxtorow\'s New Affiliates</strong></font></div></td></tr>';
// Fetch records and display results
// while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo '<tr><td class="affiliates">';
echo $row['station_name'];
echo ' ';
echo $row['city'];
}
echo '</td></tr></table>';
mysql_free_result($result); // Free resources;
} else {
// Public message
echo '<p class="error">Not available right now...</p>';
// Debugging message
// echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>';
echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>';
}
mysql_close(); // Close DB connection
}
Actually, $modx->query and $modx->prepare() are the proper methods to use. Accessing the pdo object directly is not recommended in xPDO. Besides, by using the modX equivalent wrapper methods for PDO, you get on-demand connections. In order to access $modx->pdo, you must already have a connection.
Actually, $modx->query and $modx->prepare() are the proper methods to use. Accessing the pdo object directly is not recommended in xPDO. Besides, by using the modX equivalent wrapper methods for PDO, you get on-demand connections. In order to access $modx->pdo, you must already have a connection.
$sql = "SELECT column FROM table WHERE column = $string"; $results = $modx->query($sql); while($row = $results->fetch(PDO::FETCH_ASSOC); //do stuff with $row['column ']; }