Re: local v remote

This is a discussion on Re: local v remote within the PHP General forums, part of the PHP Programming Forums category; > On my localhost this works fine > > $result= mysql_query("SELECT date_format(date, '%d/%m/%Y') as date, ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-31-2007
Jared Farrish
 
Posts: n/a
Default 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/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." $$

Reply With Quote
  #2 (permalink)  
Old 05-31-2007
M. Sokolewicz
 
Posts: n/a
Default Re: local v remote

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/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_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/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 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.
Reply With Quote
  #3 (permalink)  
Old 05-31-2007
Richard Lynch
 
Posts: n/a
Default Re: [PHP] Re: local v remote

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?
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:28 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0