/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] DB Query dies in PHP?

[code=php]
<?php error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
ini_set(‘display_errors’,1);

$beg = ‘2004-04-15’;
$end = ‘2010-04-15’;

$dbh = mysql_connect(“host”,”username”,”password”);
$query = sprintf(”
SELECT customers_id, customers_email_address, customers_info_date_account_created
FROM db_name.customers, db_name.customers_info
WHERE customers_info_id = customers_id
AND customers_info_date_account_created
BETWEEN ‘&#37;s’ AND ‘%s’
AND (customers_email_address LIKE ‘%.edu%’)”,
mysql_real_escape_string($beg), mysql_real_escape_string($end)
);

$result = mysql_query($query);

if (!$result) {
$message = ‘Invalid query: ‘ . mysql_error() . “<br/>n”;
$message .= ‘Whole query: ‘ . $query;
die($message);
}

$token = array();
while ($row = mysql_fetch_assoc($result)) {
#print_r($row);
$token[array_pop(explode(‘@’, $row[‘customers_email_address’]))][$row[‘customers_id’]] = $row[‘customers_email_address’];
}

var_export($token);

mysql_free_result($result);

/* Dies with error
Invalid query: Query was empty
Whole query:
*/
?>
[/code]

This works fine as a query in PhpMyAdmin, but fails when I try to execute,
any errors you guys can spot in syntax I might have overlooked?? :rolleyes:

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscApr 07.2011 — I think you're missing

[code=php]
mysql_select_db('your_database');
[/code]


after your connect.

I know lots of people use classes to connect to their databases and update, insert, delete, etc...but I usually just a use a simple function like this:
[code=php]
function connect($db) {
$conn = mysql_connect('##','##','##') or error('err msg');
mysql_select_db($db) or error('err msg');
return $conn;
}
[/code]


That way, I'll never use mysql_connect and always use
[code=php]
$thisconn = connect('my_database');
[/code]


Which prevents the issue you're having because the necessary connection statements are all in the same place
Copy linkTweet thisAlerts:
@ehimeauthorApr 07.2011 — I don't think this is it, since it's returning that my query is empty?
<i>
</i>...
FROM db_name.customers, db_name.customers_info
...


Should tell it which db to connect to imho
Copy linkTweet thisAlerts:
@ehimeauthorApr 07.2011 — It looks like a problem with sprintf, as
[code=php]
$query = trim("

SELECT customers_id, customers_email_address, customers_info_date_account_created
FROM db_name.customers, db_name.customers_info
WHERE customers_info_id = customers_id
AND customers_info_date_account_created BETWEEN '2004-04-15' AND '2010-04-15'
AND (customers_email_address LIKE '&#37;.edu%')"
);
[/code]


Returns fine
Copy linkTweet thisAlerts:
@aj_nscApr 07.2011 — I don't think this is it, since it's returning that my query is empty?
<i>
</i>...
FROM db_name.customers, db_name.customers_info
...


Should tell it which db to connect to imho[/QUOTE]


touche....what's the error message it's giving you?
Copy linkTweet thisAlerts:
@ehimeauthorApr 07.2011 — It also looks like it has something to do with
AND (customers_email_address LIKE '&amp;#37;.edu%')
Copy linkTweet thisAlerts:
@ehimeauthorApr 07.2011 — [code=php]
/* Dies with error
Invalid query: Query was empty
Whole query:
*/
[/code]
Copy linkTweet thisAlerts:
@ehimeauthorApr 07.2011 — [code=php]
$query = sprintf("

SELECT customers_id, customers_email_address, customers_info_date_account_created
FROM db_name.customers, db_name.customers_info
WHERE customers_info_id = customers_id
AND customers_info_date_account_created BETWEEN '&#37;s' AND '%s'
AND (customers_email_address LIKE '%s')",

mysql_real_escape_string("$beg"),
mysql_real_escape_string("$end"),
mysql_real_escape_string("%.edu%")
);
[/code]


Works, looks like sprintf was trying to replace the %.edu%,

was there a way to escape the %?
Copy linkTweet thisAlerts:
@aj_nscApr 07.2011 — Check out number 2 and 5 in the list of specifiers:

http://php.net/manual/en/function.sprintf.php

I guess you can get away with concatenation?

EDIT: I believe you escape it by &#37;%.....not tested.
Copy linkTweet thisAlerts:
@ehimeauthorApr 07.2011 — werd. Thanks for the hand AJ, glad we got it ironed out.
×

Success!

Help @ehime spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...