-1

I am trying to pull user data from a Cart66 table I have and put it into a shortcode in wordpress. $account is an integer pulled from session data. The code below returns nothing.

$account =Cart66Session::get(Cart66AccountId);
global $wpdb;
$fname=$wpdb->get_results("SELECT * FROM 'vfp_cart66_accounts' WHERE id = '$account',         ARRAY_N");
foreach ($fname AS $row)
{
echo $row;
}

This returns "Array"

return $fname;
8
  • Sooooo iterate the array? Dec 24, 2013 at 0:43
  • Are you sure $fname has a value? Dec 24, 2013 at 0:43
  • 2
    SQL Syntax.... table names such as vfp_cart66_accounts aren't string literals, so don't quote them.... if you absolutely must, use backticks (`), not regular quote marks (')
    – Mark Baker
    Dec 24, 2013 at 0:44
  • 1
    SQL Syntax... what is WHERE id = '$account', ARRAY_N supposed to mean? If it's an output type being passed to the get_results() method, then it shouldn't be in quotes with the SQL query
    – Mark Baker
    Dec 24, 2013 at 0:45
  • warning your code appears to be vulnerable to sql injection attacks. Dec 24, 2013 at 0:45

2 Answers 2

0

Ok firstly, maybe I am the only one who saw this, and it could be the source of your entire problem, but you have a misplaced double quote, at the end of your SQL line, which should live at the end of the actual SQL string, not after the requested return type:

// at the end of this line you have: '$account',     ARRAY_N");
// this should be changed to:        '$account'", ARRAY_N);
$fname=$wpdb->get_results("SELECT * FROM 'vfp_cart66_accounts' WHERE id = '$account',         ARRAY_N");

Even the first person who answered the question did not correct you, so I am assuming he didn't see it either. Secondly, using single quotes (') to escape a table name is invalid. If it is quoted at all, use backticks (`). Single quotes indicate a string, not an database, table, or field, all three of which should only be quoted with backticks (except on utility queries like SHOW). Use this instead:

select * from `vfp_cart66_accounts` where id = '$account'

Thirdly, as your commenters point out, you could be vulnerable to SQL Injection. Make sure to use the tools that WP gives you, and do this, or similar, instead:

$fname = $wpdb->get_results(
  $wpdb->prepare(
    'select * from `vfp_cart66_accounts` where id = %d',
    $account
  ),
  ARRAY_N
);

Lastly, you are requesting an array from the DB, but you are trying to echo it as if it were a scalar value. This explains why printing the value of $row yields "Array". When you convert an array() to a string, by default, you get "Array", since arrays can be complex data that may not be beautifully converted to a string. As a correction of this, you can do one of two things.

First, if you need the entire resulting array that represents the entire row of the table, then you can simply change your echo code to this:

foreach ($fname as $row) {
  // print the fname of the row
  echo $row['fname'];

  // do the other stuff you need to do with $row
  ...
}

OR, if you simply need the fname field out of that table, for the given id, you could use a different $wpdb function, called $wpdb->get_var(), which gets one specific field from the first entry of the resulting data from the database, coupled with some minor SQL changes:

// use the get_var() function instead
$fname = $wpdb->get_var(
  $wpdb->prepare(
    // 1) change the 'fields' of your sql to only get the `fname` field
    // 2) also add limit 1, to reduce load by only asking for one row
    // NOTE: #2 is optional really, because WP does this for you when using get_var,
    //       but is good practice to only ask for what you need. so do it
    'select fname from `vfp_cart66_accounts` where id = %d limit 1',
    $account
  ),
  ARRAY_N
);

echo $fname; // print the value of field fname from vfp_cart66_accounts for id $account

Now. I don't have specific knowledge of Cart66. That being said, if the above changes to PHP, WordPress, and SQL syntax do not yield results, then you are probably having one of the following other problems instead:

  1. there is a different PHP error somewhere in the code, causing this to never run
  2. this code is never called, and thus it is never executed
  3. you misspelled the table name, which is causing an SQL error
  4. the table exists, but does not have a field named id
  5. both table and field exist, but there are no entries in the table
  6. some other random thing that is not coming to mind

DEBUG #1

For #1, you could try turning on error_reporting() and display_errors early in the code execution. In a normal, run of the mill PHP script you could add the following two lines somewhere early in the code:

error_reporting(E_ALL);
ini_set('display_errors', 1);

However, you are using WordPress, so you will need to do something like this in your wp-config.php file:

// find the line that looks like this and comment it out
// define('WP_DEBUG', false);

// add these two lines directly below it
define('WP_DEBUG', true);
ini_set('display_errors', 1);

DEBUG #2

Make sure your code is running. Don't be afraid to throw a die() statement directly above it, to make sure it is running. Something like this:

// add a die() before everything
die('I am running. Awesome!');

// revised code
$account = Cart66Session::get(Cart66AccountId);
global $wpdb;
$fname = $wpdb->get_var(
  $wpdb->prepare(
    'select fname from `vfp_cart66_accounts` where id = %d limit 1',
    $account
  ),
  ARRAY_N
);
echo $fname;

DEBUG #3

To debug #3, you need either access to a commandline tool for MySQL or some type of GUI interface like phpMyAdmin, so that you can run a query directly from the database. Here is the query you should run:

show tables like 'vfp_cart66_%';

This is an example of one of the only places in SQL that you should ever quote a table name in single quotes. Running this will yield a list of all the tables that start with vfp_cart66_. If you get no results, then your table name is wrong. If your results do not include vfp_cart66_accounts, then your table name is wrong. If you see vfp_cart66_accounts, you are good to go.

DEBUG #4

This one will need to be run directly from the DB or through something like phpMyAdmin also. You are trying to make sure you have the correct field name. The way you do that is:

show create table `vfp_cart66_accounts`;

Assumedly, the field you are calling id would be the auto_incremented field in the table. Thus you are looking for a line, similar to this one:

`id` bigint(20) NOT NULL AUTO_INCREMENT,

Make sure that the line that has AUTO_INCREMENT on it, begins with:

`id`

If it does not, and the name is something else other than id, then you probably have the wrong field name.

DEBUG #5

Make sure you actually have data to display. From your mysql console or phpMyAdmin, run:

select * from `vfp_cart66_accounts` limit 1;

If you bet any results, then you have data, and you are good.

DEBUG #3 - #5 (alternate methods)

Another option you have is to dump the $wpdb object, directly after you run the query, because it contains the last error you received from MySQL. You can do this like so:

$fname = $wpdb->get_var(
  $wpdb->prepare(
    'select fname from `vfp_cart66_accounts` where id = %d limit 1',
    $account
  ),
  ARRAY_N
);

// dump a readable version of the $wpdb object
echo '<pre>';
print_r($wpdb);
die('</pre>');

Often times, reading the MySQL error message helps narrow down the problem in your SQL syntax.

DEBUG #6

If none of this has helped at all, then you will need to use your experience to trackdown a random bug in either your plugins or theme, what could literally be anything. You may as well not even dig in core WP code because, while it does have a couple minor bugs unrelated to your problem, which are getting repaired as we speak, it is one of the most stable CMS platforms out there. It is used by more of the top 10 million sites on the internet than any other CMS, for a good reason. It works, it is up-to-date, and most of all, it is stable.

I really hope you found this helpful or at least learned something from it. Hopefully others find it useful as well.

1
  • Your reply was very thorough and helpful, thank you. I used your get_var suggestion with one minor tweek. I eliminated ARRAY_N and it worked like a champ! Dec 24, 2013 at 3:20
0
$fname=$wpdb->get_results(
    "SELECT * FROM `vfp_cart66_accounts` WHERE id = '$account'",
    ARRAY_N"
);
2
  • Back ticks didn't help, still returns nothing Dec 24, 2013 at 0:59
  • I should clarify, the loop returns nothing, $fname returns the word "Array" Dec 24, 2013 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.