(Should I post this on the PHP forum instead?)
People keep telling me I have to change my database queries to PDO, so I decided to make that project part of a huge, multi-site upgrade I’m working on.
Here’s the first query I want to convert:
$res = mysql_query ("SELECT P.URL, P.Title, P.Subtitle, P.MetaTitle, P.MetaDesc, P.KW, P.Site, P.Live,
PB.Common, PB.First, PB.Middle, PB.Last, PB.Prefix, PB.Suffix, PB.Alpha, PB.Born, PB.Died, PB.Birth_Place, PB.Death_Place, PB.Nationality, PC.URL, PC.Class, Brf.URL, Brf.Site, Brf.Brief, ART.Article PXA
FROM people P
LEFT JOIN people_1_bio PB ON PB.URL = P.URL
LEFT JOIN people_1_class PC ON PC.URL = P.URL
LEFT JOIN people_briefs Brf ON Brf.URL = P.URL
LEFT JOIN people_articles_px ART ON ART.URL = P.URL
WHERE P.Site = 'PX' AND Brf.Site = 'PX' AND PB.Alpha LIKE '$MyURL%' AND Brf.Brief !='' AND PC.URL = P.URL AND P.Live = 1
GROUP BY P.URL ORDER BY P.N") or die (mysql_error());
Of course, it’s probably pointless to tackle something that big and complex in PDO 101, so let me condense it to something simpler:
$res = mysql_query ("SELECT P.URL, P.Site, P.Live, Brf.URL, Brf.Site, Brf.Content
FROM people P
LEFT JOIN people_briefs Brf ON Brf.URL = P.URL
WHERE P.Site = 'PX' AND Brf.Site = 'PX' AND Brf.Brief !='' AND P.Live = 1
GROUP BY P.URL ORDER BY P.N") or die (mysql_error());
I’m following a tutorial @ http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers and I’m focusing on this example:
$stmt = $db->prepare("SELECT * FROM table WHERE id=? AND name=?");
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$stmt->bindValue(2, $name, PDO::PARAM_STR);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
This is how I began translating it to fit my query:
$stmt = $db->prepare("SELECT * FROM people WHERE P.Site = 'PX' AND Brf.Site = 'PX' AND Brf.Brief !='' AND P.Live = 1");
$stmt->bindValue(1, $id, PDO::PARAM_INT);
$stmt->bindValue(2, $name, PDO::PARAM_STR);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
But I was immediately lost. I assume that if a value is an integer, then I use a bindValue statement ending in PARAM_INT. Otherwise, I use a bindValue statement ending in PARAM_STR.
But I don’t understand how I’m supposed to translate my PHP query into a PDO query. Thanks for any tips.