Skip to content

Instantly share code, notes, and snippets.

@KariTrace
Created October 1, 2012 17:50
Show Gist options
  • Select an option

  • Save KariTrace/3813322 to your computer and use it in GitHub Desktop.

Select an option

Save KariTrace/3813322 to your computer and use it in GitHub Desktop.
phpMyAdmin returns way more than real_query / use_result
class CurrentAccountsClass
{
//class properties
private $mysqliConn;
/**
*@Param object $dbConn [required] Database connection object and set to a private class var.
*/
function __construct( $dbConn )
{
$this->mysqliConn = $dbConn;
}
/**
* Make me an account, with all the related contact information
*@Param string $limit [optional] SQL formated limit paramater, optional.
*@Return array $data ASSOC array of all accounts with contacts and all related contact information
*/
public function getAccountsData( $mysqli, $limit = null )
{
//How to get email addresses: email_addresses.id -> email_addr_bean_rel.email_address_id && bean_module = Accounts || Contacts
$stmt = "
SELECT
ACC.`id` as ACC_id, ACC.`name` as ACC_name, ACC.`phone_office` as ACC_phone_office, ACC.`phone_alternate` as ACC_phone_alt,
(
SELECT `email_address`
FROM `email_addresses` AS EMADD
LEFT JOIN `email_addr_bean_rel` AS EMABR
ON EMADD.`id` = EMABR.`email_address_id`
WHERE
`bean_module` = 'Accounts' AND
`bean_id` = ACC_id
) as ACC_email,
ACCCON.`account_id` as ACCCON_accID, ACCCON.`contact_id` as ACCCON_connID,
CON.`id` as CON_id, CON.`first_name` as CON_first_name, CON.`last_name` as CON_flast_name, CON.`phone_home` as CON_phone_home, CON.`phone_mobile` as CON_phone_home, CON.`phone_work` as CON_phone_work, CON.`phone_other` as CON_phone_other,
(
SELECT `email_address`
FROM `email_addresses` AS EMADD
LEFT JOIN `email_addr_bean_rel` AS EMABR
ON EMADD.`id` = EMABR.`email_address_id`
WHERE
`bean_module` = 'Contacts' AND
`bean_id` = CON_id
) as CON_email
FROM `accounts` as ACC
LEFT JOIN `accounts_contacts` as ACCCON
ON ACC.`id` = ACCCON.`account_id`
LEFT JOIN `contacts` as CON
ON ACCCON.`contact_id` = CON.`id`
";
/*
$dbh = new PDO('mysql:host=localhost;dbname=sugarcrm', 'root', null);
$sth = $dbh->prepare( $stmt );
$sth->execute();
//Fetch all of the remaining rows in the result set
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
*/
//'cause I cant figur out why the object wont pass in
//$this->mysqliConn
if ( $mysqli->real_query($stmt)){
$counter = 0;
$data = null;
do {
if ($result = $mysqli->use_result()) {
while ($row = $result->fetch_assoc()) {
echo $counter;
$counter++;
print_r( $row );
$data[] = $row;
if( isset( $mysqli->error_list ) )
{
print_r( $mysqli->$error_list );
}
}
}
} while ($mysqli->next_result());
}
$mysqli->close();
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment