Build Your Own Business Website header image

Welcome to our Members Only Forum

You are welcome to view the content on our Forum, just scroll down to view this forum post.
If you like what you find and want to be able to post a question, please check out our subscription options.
Please feel free to search the forum using either the Google Site Search above or the forum search below.

Avatar
Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
PHP MySQL How to Access, Select and Update MySQL tables
Avatar
Henk Jordaan
Member
Members
Forum Posts: 23
Member Since:
January 5, 2013
sp_UserOfflineSmall Offline
1
April 10, 2016 - 5:17 pm
sp_Permalink sp_Print sp_EditHistory

Hi Rick,

I have a seeminly simple SQL data base application.

I have created two Tables through phpMyAdmin.  One saves user data submitted via forms with one of the fields being "Area" that the user selects via form select dropdown box.  Based on the area selected I want to collect email addresses from the Second table that stores Area Coordinators.

I've got everything working in a Custom Template used by the Twenty Fifteen Theme.

But when I put the Same code in Thesis, either in the Thesis Template with OpenHook or directly in the page (using a PHP Exec plugin so that I can put PHP directly into the Page), I get a variety of errors.  Error messages range but either I get an empty array returned or it complains about the Select Statement or the Foreach Statement, basically indicating that code such as the following is wrong:... (I think the warning on "foreach" comes because it did not find an array, because the result is empty)

Code Example 1:

$result = "SELECT * FROM `Chipper_Leaders` WHERE area = '".$area."' ";
mysql_query($result);
if(!empty($result)){
foreach($result as $carea){
{
echo $carea . "I found Something!";
}

Code Example 2:

$areas = $wpdb->get_results(
$wpdb ->prepare("SELECT email FROM `wp_ac_coordinators` where area = '".$member_area."' ", '')
);

if(!empty($areas)){
foreach($areas as $area){

echo $carea . "I found Something!";

I put the Echo statements in to see why the code does not work but I never get to see them.

Can you Help!!smile

Avatar
Pierre Cote
Moderator
Members

Moderators

Community Member
Forum Posts: 1160
Member Since:
September 21, 2014
sp_UserOfflineSmall Offline
2
April 10, 2016 - 5:39 pm
sp_Permalink sp_Print

Hi

Instead of "echo", have you tried "return" ?

 

Pierre

Pierre ( postrophe )

Avatar
Henk Jordaan
Member
Members
Forum Posts: 23
Member Since:
January 5, 2013
sp_UserOfflineSmall Offline
3
April 11, 2016 - 2:08 am
sp_Permalink sp_Print

Hi Pierrre, thanks for thinking.

Yes. I have tried return and print_r. The issue is not the echo statement... It skips over it because the array (result) isempty.  There is nothing there.  The error messages, when they do appear, refer to either the select statement that returns something like 'trying to access a non-object', or the 'foreach' which does not work on empty array's.

Avatar
Pierre Cote
Moderator
Members

Moderators

Community Member
Forum Posts: 1160
Member Since:
September 21, 2014
sp_UserOfflineSmall Offline
4
April 11, 2016 - 4:17 pm
sp_Permalink sp_Print

Hi

As I see your code above, they are missing 2 closing brackets at the end.

Not sure if this will make a difference !!

Pierre ( postrophe )

Avatar
Rick Anderson
Desert Hot Springs, CA
Admin
Forum Posts: 13637
Member Since:
November 8, 2009
sp_UserOfflineSmall Offline
5
April 15, 2016 - 9:09 am
sp_Permalink sp_Print

Henk - you should avoid using code like example 1 - it opens you up to security vulnerabilities.

However code example 2 is the right way to proceed.  The only thing I see is that you haven't yet instantiated the global $wpdb variable.

The first line in that code should read

global $wpdb;

Forum Timezone: America/Los_Angeles

Most Users Ever Online: 228

Currently Online:
20 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

Keith W Jones: 1428

Brian Tozer: 1004

Carolin: 982

Heidi Hafner: 508

John Cunningham: 423

Reece Morrel: 353

Newest Members:

emma62755

sharonmarks

emma80233

Tijs de Jong

Daniel Frey

mutek

Forum Stats:

Groups: 8

Forums: 46

Topics: 6995

Posts: 34796

 

Member Stats:

Guest Posters: 6

Members: 6761

Moderators: 2

Admins: 2

Administrators: Rick Anderson, Rick Anderson

Moderators: Pierre Cote, Laura Nugent