Re: local v remote



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

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$


Relevant Pages

  • Re: Object question
    ... here is the dangling open db connection his object opens but never calls to ... in php and/or his db. ... responsibilities to explicitly releasing resources in use by an instanciated ... of course the resource will be different as it points to a new ...
    (alt.php)
  • RE: [PHP] OOB problem, super stumped. (resolved)
    ... [PHP] OOB problem, super stumped. ... So the next query has a null resource and so fails and generates ... your connection() method so even if you did return the resource ... scalable system for accessing system services | ...
    (php.general)
  • Re: SLOW
    ... shortcut or connection to a resource which user B owns? ... Like a shortcut to user Bs redirected printer or something like it? ... session is constantly looking for a resource which it can't find. ... MCSE, CCEA, Microsoft MVP - Terminal Server ...
    (microsoft.public.windows.terminal_services)
  • Re: I am a php noob and have a quick question
    ... $link, when a connection is established, is a resource id. ... for you to refer to this particular database connection. ... If you look up the mysql functions you will discover that some of them have ...
    (comp.lang.php)
  • Re: Dispose/Finalize
    ... As you point out the close command needs to be sent to the database, ... So for instance if you have a network connection, ... If I were to use an resource in one of my classes that needed cleaning ... If I was writing my own Dispose/Finalize, ...
    (microsoft.public.dotnet.framework.clr)