I have a simple PHP/MySQL psychiatric drug database website. Everything is working well, until the user puts a single quote anywhere into the search string. I get this error:
Fatal error: Uncaught Error: Call to a member function fetch() on boolean in /home/psychoph/public_html/connection.php:76 Stack trace: #0 /home/psychoph/public_html/index.php(70): include() #1 {main} thrown in /home/psychoph/public_html/connection.php on line 76
I tried this: $generic = $dbc->real_escape_string($generic);
to no avail ($generic
is the variable assigned to the user input)...I get this error:
Fatal error: Uncaught Error: Call to undefined method PDO::real_escape_string() in /home/psychoph/public_html/connection.php:12 Stack trace: #0 /home/psychoph/public_html/index.php(70): include() #1 {main} thrown in /home/psychoph/public_html/connection.php on line 12
Any guidance would be appreciated. I really just need to strip single quotes from the user input, but don't know how.
Here's the code for connection.php:
<?php
$dbc = new PDO(
"mysql:host=173.236.101.74;dbname=psychoph_psychopharm",
'psychoph_knasky',
'#######');
try {
$results = $dbc->query("
SELECT *
FROM drugs
WHERE generic = '$generic'
OR brand = '$generic'
"); // this method call returns the query results and places it into $results
} catch (Exception $e) {
echo "Unable to retrieve results";
exit;
}
try {
$side_effects = $dbc->query("
SELECT se.side_effect
FROM side_effects AS se
JOIN jnct_drug_side_effects AS jse ON se.id = jse.side_effect
JOIN drugs AS d ON jse.drug = d.id
WHERE generic = '$generic'
OR brand = '$generic'
");
} catch (Exception $e) {
echo "Unable to retrieve results";
exit;
}
try {
$indications = $dbc->query("
SELECT i.indication
FROM indications AS i
JOIN jnct_drug_indications AS ji ON i.id = ji.indication
JOIN drugs AS d ON ji.drug = d.id
WHERE generic = '$generic'
OR brand = '$generic'
");
} catch (Exception $e) {
echo "Unable to retrieve results";
exit;
}
try {
$oi_indications = $dbc->query("
SELECT oi.indication
FROM ol_indications AS oi
JOIN jnct_ol_drug_indications AS joi ON oi.id = joi.indication
JOIN drugs AS d ON joi.drug = d.id
WHERE generic = '$generic'
OR brand = '$generic'
");
} catch (Exception $e) {
echo "Unable to retrieve results";
exit;
}
$row = $results->fetch(PDO::FETCH_ASSOC);
$se_row = $side_effects->fetchAll(PDO::FETCH_COLUMN, 0);
$print_se = implode(", ", $se_row);
$ind_row = $indications->fetchAll(PDO::FETCH_COLUMN, 0);
$print_ind = implode(", ", $ind_row);
$oi_ind_row = $oi_indications->fetchAll(PDO::FETCH_COLUMN, 0);
$print_ol_ind = implode(", ", $oi_ind_row);
?>
To clarify my question, I don't have any SQL injection concerns. This isn't a site that contains any data that needs protection (unless someone really wants to change the half-life of Zoloft on some random website for psychiatrists). I'm just trying to get this to work. I'm a physician trying to build a useful web tool, and don't like that a single quotation mark in my query returns an error message to the user. Is there a simple fix to this? I'm not a programmer - I'm trying to learn just enough to make this site work. Thanks, all!