I am slowly learning PHP and have been using prepared statements.
I have a simple category.php page, which takes a category tag to display posts from that category, such as localhost/php/category.php?tag=sysadmin. I want to select the Category Title from the "j_oracle_cat" table based on the value of the incoming tag in the querystring.
My code seems to be extremely cumbersome and long-winded (I'm using MySQLi instead of PDO):
<?php
/* connection string up here */
/* get category tag */
$foo = $_GET['tag'];
/* if it contains an apostrophe go to homepage */
$foo2 = strpos($foo, '\'');
if ($foo2 > 0) { Header("Location: default.php"); }
/* sql */
$cat_title_sql = "SELECT fld_title FROM j_oracle_cat WHERE fld_tag = ? LIMIT 1";
/* category variable */
$foo_cat = $foo;
/* initialise and prepare */
$stmt = $conn->stmt_init();
$result_cat_title = $stmt->prepare($cat_title_sql);
if(!$result_cat_title) { trigger_error("Error preparing statement: $stmt->error<br/>SQL query: $cat_title_sql", E_USER_ERROR); }
/* bind */
$result = $stmt->bind_param('s',$foo_cat);
if(!$result) { trigger_error("Error binding parameter: $stmt->error<br/>", E_USER_ERROR); }
/* execute */
$stmt->execute();
/* store */
$stmt->store_result();
$count_cat = $stmt->num_rows;
/* if no results go to homepage */
if ($count_cat == 0) {
Header("Location: ../default.php");
} elseif ($result) {
$stmt->bind_result($fld_title);
while ($stmt->fetch()) {
$cat_ttl = $fld_title;
}
}
/* ... header include down here */
?>
<title><?php echo $cat_ttl?></title>
I just wondered if I am making a meal of this, and if the code is over the top, or if I am on the right track (both with the prepared statement and handling the incoming querystring variable).