Hi hsysgrp,
Did you remember to do global $wpdb; within scope of this code?
In what way is this not working? I don’t see a problem with doing $find = $searchq;. What do you get with that when you echo out $SQL? Whatever came in from $_POST['searchmonth'] should appear between the wildcard hashes of the prepared SQL.
If I hardcode $find = ‘Feb’ I get results, When I type Feb into the form the output is
Feb searchq
Birthdays in Feb
Feb find
global $wpdb;
$path = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/twentytwelve-child/custom-page_Download_Birthdays-wpdb.php ";
if(isset($_POST['searchmonth'])) {
$searchq = $_POST['searchmonth'];
echo "$searchq" . " searchq";
echo "<h3>Birthdays in ". $searchq."</h3>";
echo "<br>";
// Setup wildcard
$wild = '%';
$find = $searchq;
echo "$find" . " find" ;
$like = $wild . $wpdb->esc_like( $find ) . $wild;
echo "$like" . " $like" ;
// $sql = $wpdb->prepare("SELECT * FROM <code>AAUW_Members</code> WHERE Birth_Month = %s", $searchq);
$SQL = $wpdb->prepare("SELECT FirstName, LastName, Birth_Month, Birth_Day FROM <code>AAUW_Members</code> WHERE Birth_Month LIKE %s",$like);
echo " .. " . $SQL . " .. ";
$result = $wpdb->get_results($SQL);
//etc. to print out $result
“Feb find” was the end of output? I tried your code and got
Feb searchq
Birthdays in Feb
Feb find%Feb% %Feb% .. SELECT FirstName, LastName, Birth_Month, Birth_Day FROM AAUW_Members WHERE Birth_Month LIKE '{e3e2552da16cd1ad78c02305c63bb48d4b82bce775d37a7cd524e7897fa7e510}Feb{e3e2552da16cd1ad78c02305c63bb48d4b82bce775d37a7cd524e7897fa7e510}' ..
The hash of % is normal. I then got a DB error since I don’t have that table. I did alter you code slightly in the element in $_POST to match an existing form I have, but it still returns “Feb”. I also removed the back ticks around Birth_Month when posting here due to how the forum tries to make them into code tags, but they exist in the original output.
IOW I don’t see why you wouldn’t at least see the prepared SQL. Do you have WP_DEBUG defined as true in wp_config.php? It’ll help flag any errors in code.
Thank you for your help. Some progress. Debug is on;
Notice: Undefined index: searchmonth in /home4/hsysgrpc/public_html/wp-content/themes/twentytwelve-child/custom-page_Download_Birthdays-wpdb.php on line 29 complaining about the form input searchmonth.
sql is
SELECT * FROM AAUW_Members WHERE Birth_Month = ‘Feb’ ORDER BY LastName, works fine if I don’t use the form. But
`// $month = ‘Feb’; //works if hardcoded, $sql prints as Birth_Month = ‘Feb’
$month = $searchq;
echo “<h3>Birthdays in “. $searchq.”</h3>”;
echo “<br>”;
$sql = $wpdb->prepare(“SELECT * FROM AAUW_Members WHERE Birth_Month = %s ORDER BY LastName”, $month); // the value of $month = Feb, it needs to be ‘Feb’
echo $sql;
Output is
Feb searchq
‘Feb’ ‘Feb’
Birthdays in Feb
The sql works if $month is hardcoded to ‘Feb’, $month = ‘Feb’, what am I not seeing?
$searchq = $_POST['searchmonth'];
echo $searchq . " searchq";
echo "<br>";
$month = "'" . $searchq . "'";
echo "<br>";
echo $month . " " . "$month ";
echo "<h3>Birthdays in ". $searchq."</h3>";
echo "<br>";
$sql = $wpdb->prepare("SELECT * FROM <code>AAUW_Members</code> WHERE <code>Birth_Month</code> = %s ORDER BY LastName", $month);
echo $sql;
The quotes for Feb exist in the DB records? Because prepare() adds its own quotes around any %s, resulting in '\'Feb\''. (\' are escaped quotes and part of the string, not part of SQL itself) The DB records would literally need to be entered as 'Feb', which is weird, but if that’s the way the data is saved, then it is what it is.
You’re not getting any output for echo $sql;? When I tested your code I got at least that. Of course the query fails for me since I don’t have that table.
@hsysgrp, did you find a way to run your database query?
The search by month seems to be working nicely on the page you needed help with. Would you mind to share what the problem was? It could help others in the future.