Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, I've set up a database using phpMyAdmin and am now trying to access it from a PHP file but I get the following error. "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /files/home3/steventownley/househunting/search_response.php on line 59" Here is the relevant code extract. Line 59 is the last one shown. /* Declare database parameters */ $db_host = "rumpus"; $username = "steventownley"; $password = // removed from this email! $db_name = "steventownley_pn"; /* Set up database connection */ $db = mysql_connect($db_host, $username, $password, $db_name); /* Set up query */ $query = 'select * from Houses where 1'; /* Send query to database */ $result = mysql_query($db, $query); All help much appreciated. Thanks. Steve -- ---- Steven Townley Stockport, England -- ---- Steven Townley Stockport, England MSN Messenger: steven@czechbook.freeserve.co.uk
Post Follow-up to this messageOn Thu, 21 Apr 2005 22:49:41 +0100, "Steve T" < pedallingminstrel@NOSPAMpedallingminstre l.fsworld.co.uk> wrote: >I've set up a database using phpMyAdmin and am now trying to access it from >a PHP file but I get the following error. > >"Warning: mysql_query(): supplied argument is not a valid MySQL-Link >resource in /files/home3/steventownley/househunting/search_response.php on >line 59" > >Here is the relevant code extract. Line 59 is the last one shown. > > /* Declare database parameters */ > $db_host = "rumpus"; > $username = "steventownley"; > $password = // removed from this email! > $db_name = "steventownley_pn"; > > /* Set up database connection */ > $db = mysql_connect($db_host, $username, $password, $db_name); You've not bothered to check for errors here. > /* Set up query */ > $query = 'select * from Houses where 1'; > /* Send query to database */ > $result = mysql_query($db, $query); .. and so you're probably passing 'false' to the function here. To see why the connect is failing, use mysql_error(). $db = mysql_connect($db_host, $username, $password, $db_name) or die(mysql_error()); -- Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Post Follow-up to this messageThanks for that. In fact it appears that the call to mysql_connect() had too many arguments (perhaps because I've been reading a book on PHP5 and my ISP is running PHP4?) I've now changed it to connect and then specify a database in two stages, like this (below), with the call to mysql_connect taking only three arguments (not four). /* Set up database connection */ mysql_connect($db_host, $username, $password); mysql_select_db($db_name); This works. Thanks again Steve "Andy Hassall" <andy@andyh.co.uk> wrote in message news:ca8g61lrau10mba17u3rrdtm67ghqnhlde@ 4ax.com... > On Thu, 21 Apr 2005 22:49:41 +0100, "Steve T" > < pedallingminstrel@NOSPAMpedallingminstre l.fsworld.co.uk> wrote: > > > You've not bothered to check for errors here. > > > ... and so you're probably passing 'false' to the function here. > > To see why the connect is failing, use mysql_error(). > > $db = mysql_connect($db_host, $username, $password, $db_name) > or die(mysql_error()); > > -- > Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk> > <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Post Follow-up to this messageSteve, It doesn't look like you have a valid "filter" for your query. $query = 'select * from Houses where 1'; I think it should relate to one of your database field tables, like $query = 'select * from Houses where number_bedroom==1'; where "number_bedroom" is a field in your database table. You should be able to use a simplified result: $result = mysql_query($query); Joe G. "Steve T" < pedallingminstrel@NOSPAMpedallingminstre l.fsworld.co.uk> wrote in message news:42681ff2$0$539$ed2619ec@ptn-nntp-reader03.plus.net... > Hi, > > I've set up a database using phpMyAdmin and am now trying to access it > from > a PHP file but I get the following error. > > "Warning: mysql_query(): supplied argument is not a valid MySQL-Link > resource in /files/home3/steventownley/househunting/search_response.php on > line 59" > > Here is the relevant code extract. Line 59 is the last one shown. > > /* Declare database parameters */ > $db_host = "rumpus"; > $username = "steventownley"; > $password = // removed from this email! > $db_name = "steventownley_pn"; > > /* Set up database connection */ > $db = mysql_connect($db_host, $username, $password, $db_name); > /* Set up query */ > $query = 'select * from Houses where 1'; > /* Send query to database */ > $result = mysql_query($db, $query); > > All help much appreciated. > > Thanks. > > Steve > > -- > ---- > Steven Townley > Stockport, England > > > > > -- > ---- > Steven Townley > Stockport, England > MSN Messenger: steven@czechbook.freeserve.co.uk >
Post Follow-up to this messageReferences: <42681ff2$0$539$ed2619ec@ptn-nntp-reader03.plus.net> <Mu6dnc-Vwt BVD-vfRVn-tA@wideopenwest.com> In-Reply-To: <Mu6dnc-VwtBVD-vfRVn-tA@wideopenwest.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Lines: 69 NNTP-Posting-Host: 84.12.81.161 X-Trace: sv3-hET7UTs4fyTVl5aNmyn5/lj8mdcymNh3cM6AGyFZFfZnp7bvOT9m6xgo3Se/OOo Rj5mKXQtVKUEm1ub!GVEa7AVuYHBwD6wguNbJT3k b8Sv/PtEMs4reZRpEgKSsEHClQKzSH8Xp6J6 Snt3UjCS0Q7WGXlM= X-Complaints-To: abuse@dsl.pipex.net X-DMCA-Complaints-To: abuse@dsl.pipex.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: number1.nntp.dca.giganews.com alt.php.sql:23111 Joe Gazda wrote: > Steve, > > It doesn't look like you have a valid "filter" for your query. > > $query = 'select * from Houses where 1'; > I think it should relate to one of your database field tables, like > > $query = 'select * from Houses where number_bedroom==1'; > > where "number_bedroom" is a field in your database table. > > You should be able to use a simplified result: > > $result = mysql_query($query); > > Joe G. > > "Steve T" < pedallingminstrel@NOSPAMpedallingminstre l.fsworld.co.uk> wrote in > message news:42681ff2$0$539$ed2619ec@ptn-nntp-reader03.plus.net... > > > >
Post Follow-up to this messageReferences: <42681ff2$0$539$ed2619ec@ptn-nntp-reader03.plus.net> <Mu6dnc-Vwt
BVD-vfRVn-tA@wideopenwest.com>
In-Reply-To: <Mu6dnc-VwtBVD-vfRVn-tA@wideopenwest.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 67
NNTP-Posting-Host: 84.12.81.161
X-Trace: sv3- s52tVurQsS5VVtPqSy42rAm7z1fv0EJzpuP6dbqc
EaDw5FIeOYzVk9Q/yUJQQjM
6b82hsL6i9WnBuoQ!zII02Tnkw3uVGXJN8PesTVe
0p0AbVotsrKM1aNnkuX1WV7qtaUv8cmhsNRH
+g3oQjnWbUG7uxpM=
X-Complaints-To: abuse@dsl.pipex.net
X-DMCA-Complaints-To: abuse@dsl.pipex.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint
properly
X-Postfilter: 1.3.32
Xref: number1.nntp.dca.giganews.com alt.php.sql:23112
*Sorry, I posted the wrong copy - I somehow opened two :-(*
Joe Gazda wrote:
> It doesn't look like you have a valid "filter" for your query.
>
> $query = 'select * from Houses where 1';
Hi Steve,
The SQL is valid, it will simply return every record in the database
> I think it should relate to one of your database field tables, like
>
> $query = 'select * from Houses where number_bedroom==1';
>
> where "number_bedroom" is a field in your database table.
>
> $query = 'select * from Houses where number_bedroom==1';
Should that not read "..... number_bedroom = 1" ?? Note the single = sign.
<snip>
> "Steve T" < pedallingminstrel@NOSPAMpedallingminstre
l.fsworld.co.uk> wrote
in
> message news:42681ff2$0$539$ed2619ec@ptn-nntp-reader03.plus.net...
This is usually a symptom of a previous statement (your connection, or
your database selection) failing.
The line above looks like your problem - the type for the fourth
parameter should be boolean (True, or False), you are passing a String.
You will need to use mysql_connect() then mysql_select_db() *but*
after each call you should check the return value from the function...
if it is false, the function has failed. You can use a simple die......
if you wish, but it is often nicer to handle the error more gracefully.
/* Declare database parameters */
$db_host = "rumpus";
$username = "steventownley";
$password = // removed from this email!
$db_name = "steventownley_pn";
/* Set up database connection */
$connection = mysql_connect($db_host, $username, $password);
if($connection == false)
{
// Handle your error here, use the functions
// to tell you what is wrong.
echo "There was an error : ".mysql_errno()." : ".mysql_error();
// Then do something useful before ending the script
exit();
}
// Or using die which will terminate the script instantly
$db = mysql_select_db($db_name) or die ("There was an error : "
.mysql_errno()." : ".mysql_error());
Also use the error checking after your call to mysql_query... etc.
HTH, regards,
Andy
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.