Jared Farrish wrote:
Jared Farrish wrote:
On my localhost this works fine
$result= mysql_query("SELECT date_format(date, '%d/%m/%Y') as date,
title,
id, display FROM NEWS");
while ($row = mysql_fetch_assoc($result)) {
but on my remote i get a mysql_fetch_assoc(): supplied argument is not a
valid MySQL result resource
Can someone expalin the problem? PHP version problem?
Check your connection resource, as I think it's referring to the optional
second variable for mysql_query($sql, $resource).
How are you connecting? I assume if you're on your local machine, you're
probably connecting to a locally-hosted mysql installation.
Are you using the same connection string when you upload? Are you even
providing one (even a background, default connection)?
You should also always try to pass the resource to the mysql_query function
(in most but not all cases). By not passing it, you're telling PHP to use
any valid open connection it currently has associated with the script that
is running:
// Let's first get a connection to the database
$link_identifier = mysql_connect('localhost', 'mysql_user',
'mysql_password');
// Next, look at the second, *optional* $link_identifier
// This tells PHP which connection to use to perform the query
// And also tells it what connection to use to get the result
resource mysql_query ( string $query [, resource $link_identifier] )
If you don't provide the $link_identifier as a valid connection resource,
and there's none in the background already connected, you get an invalid
resource response like you received.
To test, you can
<code>
$result= mysql_query("SELECT date_format(date, '%d/%m/%Y') as date, title,
id, display FROM NEWS");
echo '<p>Test: before query</p>'
while ($row = mysql_fetch_assoc($result)) {
// Do stuff
}
</code>
Where you get the error output will clue you in to which function is
causing
the error (_query() or _fetch())?
To check if a resource has connected, you can check the resource by type:
if (is_resource($link_identifier) === true) {
// Do database stuff that needs a connection
}
IMPORTANT: Do not use mysql_pconnect() without first reading about it:
- http://us2.php.net/manual/en/features.persistent-connections.php
- http://us2.php.net/manual/en/function.mysql-connect.php
- http://us2.php.net/manual/en/function.mysql-query.php
- http://us2.php.net/manual/en/function.is-resource.php
On my localhost this works fine
$result= mysql_query("SELECT date_format(date, '%d/%m/%Y') as date,
title,
id, display FROM NEWS");
while ($row = mysql_fetch_assoc($result)) {
but on my remote i get a mysql_fetch_assoc(): supplied argument is not a
valid MySQL result resource
Can someone expalin the problem? PHP version problem?
Check your connection resource, as I think it's referring to the optional
second variable for mysql_query($sql, $resource).
huh? what?
It does say "not a valid _mysql result resource_" right? This is just
one of those cases where $result is not a _mysql result resource_ but
something else (usually the boolean false value). As many people already
pointed out, mysql_error() will explain why mysql_query returned false.
How are you connecting? I assume if you're on your local machine, you're
probably connecting to a locally-hosted mysql installation.
Are you using the same connection string when you upload? Are you even
providing one (even a background, default connection)?
You should also always try to pass the resource to the mysql_query function
(in most but not all cases). By not passing it, you're telling PHP to use
any valid open connection it currently has associated with the script that
is running:
// Let's first get a connection to the database
$link_identifier = mysql_connect('localhost', 'mysql_user',
'mysql_password');
// Next, look at the second, *optional* $link_identifier
// This tells PHP which connection to use to perform the query
// And also tells it what connection to use to get the result
resource mysql_query ( string $query [, resource $link_identifier] )
If you don't provide the $link_identifier as a valid connection resource,
and there's none in the background already connected, you get an invalid
resource response like you received.
In case you didn't know, 99% of code on this planet using mysql_query
does not supply the secondary argument as most code-bases don't use > 1
connection in the same script.
To test, you can
<code>
$result= mysql_query("SELECT date_format(date, '%d/%m/%Y') as date, title,
id, display FROM NEWS");
echo '<p>Test: before query</p>'
while ($row = mysql_fetch_assoc($result)) {
// Do stuff
}
</code>
Where you get the error output will clue you in to which function is
causing
the error (_query() or _fetch())?
it's the mysql_fetch_assoc which spits out an error as per the error
message:
"mysql_fetch_assoc(): supplied argument is not a
valid MySQL result resource"
To check if a resource has connected, you can check the resource by type:
if (is_resource($link_identifier) === true) {
// Do database stuff that needs a connection
}
IMPORTANT: Do not use mysql_pconnect() without first reading about it:
- http://us2.php.net/manual/en/features.persistent-connections.php
- http://us2.php.net/manual/en/function.mysql-connect.php
- http://us2.php.net/manual/en/function.mysql-query.php
- http://us2.php.net/manual/en/function.is-resource.php
Please, if you have little experience with the mysql functions, first
check if what you're saying actually holds true. Your advice is sound
and shows you have thought about it well, but your initial assesment of
the cause of the OP's problem is unfortunately incorrect.
Note to the OP: Jared has pointed out many good points in his long post,
though it won't help you resolve your problem, you would be wise to read
it as it will help you code better :)
- Tul
P.S. This is meant as a fyi, I did not mean to bitch on anyone.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php