Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
Re: local v remote
Old
  (#1)
Jared Farrish
Guest
 
Posts: n/a
Default Re: local v remote - 06-02-2007, 08:56 PM

> 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_***oc($result)) {
>
> but on my remote i get a mysql_fetch_***oc(): 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 ***ume 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 p*** the resource to the mysql_query function
(in most but not all cases). By not p***ing it, you're telling PHP to use
any valid open connection it currently has ***ociated with the script that
is running:

// Let's first get a connection to the database
$link_identifier = mysql_connect('localhost', 'mysql_user',
'mysql_p***word');

// 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_***oc($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/feature...onnections.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." $$

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: local v remote
Old
  (#2)
M. Sokolewicz
Guest
 
Posts: n/a
Default Re: local v remote - 06-02-2007, 08:56 PM

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_***oc($result)) {
>>
>> but on my remote i get a mysql_fetch_***oc(): 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 ***ume 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 p*** the resource to the mysql_query function
> (in most but not all cases). By not p***ing it, you're telling PHP to use
> any valid open connection it currently has ***ociated with the script that
> is running:
>
> // Let's first get a connection to the database
> $link_identifier = mysql_connect('localhost', 'mysql_user',
> 'mysql_p***word');
>
> // 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_***oc($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/feature...onnections.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_***oc($result)) {
>>
>> but on my remote i get a mysql_fetch_***oc(): 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 ***ume 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 p*** the resource to the mysql_query function
> (in most but not all cases). By not p***ing it, you're telling PHP to use
> any valid open connection it currently has ***ociated with the script that
> is running:
>
> // Let's first get a connection to the database
> $link_identifier = mysql_connect('localhost', 'mysql_user',
> 'mysql_p***word');
>
> // 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_***oc($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_***oc which spits out an error as per the error
message:
"mysql_fetch_***oc(): 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/feature...onnections.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 ***esment 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.
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] Re: local v remote
Old
  (#3)
Richard Lynch
Guest
 
Posts: n/a
Default Re: [PHP] Re: local v remote - 06-02-2007, 08:56 PM

On Thu, May 31, 2007 8:07 am, M. Sokolewicz wrote:
> 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.


That does not make it a Good Practice...

I spent days fixing somebody else's code who thought they were opening
a second connection (not) and then were closing it (yep) but they were
actually closing *MY* database connection, because they were too lazy
to type that second arg.

I personally think you should never, ever, ever, write code that
relies on the default open connection.

Sooner or later, the project will grow, morph or merge into something
where you'll get your wires crossed.

It costs only a few keystrokes in a few places to do it right.

YMMV

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




Contact Us - Forum Care Forums - Archive - Top