Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php errors

Status
Not open for further replies.

manicleek

Technical User
Joined
Jun 16, 2004
Messages
143
Location
GB
Not sure which forum I should be posting this in but...

I have just started trying to code my first php site and I get this error:

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in etc...

also happens if I use mysql_fetch_array() and mysql_result()

anyone have any ideas?
 
Well, I would need to see your code, but at first glance, it appears that you are not connected to a database object, or you ran an invalid query.

___________________________________
[morse]--... ...--[/morse], Eric.
 
Yes, you get this when the sql has failed or you didn't connect properly.

post your code and the line where you get the error
 
This is the code I used, I know that the database connects ok etc..

//select db
mysql_select_db("marinas");

//Report connection status
if (!$db) {
echo "There was a problem connecting to the database";
exit;
}
//Get results
$marmonthquery = "SELECT * FROM marmonth";
$monthresult = mysql_query($marmonthquery);
$monthrow = mysql_fetch_object($monthresult);


the last line of the code I have posted is where the problem occurs
 
Try:
$connection = mysql_select_db("dbname","username","password");
$monthresult = mysql_query($marmonthquery,$connection);





[cheers]
Cheers!
Laura
 
After the call to mysql_query add the following:
$e = mysql_error();
echo $e;

mysql_error takes a parametrer but should be ok to default it
 
mysql_select_db() should only take the database name and the database object you are using, not username and password.
Try adding "die" to your code, and also explicitily specify the connection identifer.

For instance:
Code:
$cnx = mysql_connect(xxx,xxx,xxx) or die "Unable to connect to Database";
//select db
mysql_select_db("marinas",$cnx);

//Report connection status
if (!$db) {
    echo "There was a problem connecting to the database";
    exit;
}
//Get results
$marmonthquery = "SELECT * FROM marmonth";
$monthresult = mysql_query($marmonthquery,$cnx) or die "Problem!";

while($monthrow = mysql_fetch_array($monthresult)){

 echo $monthrow['columname'];

}

SHOULD WORK. Give that a shot.

___________________________________
[morse]--... ...--[/morse], Eric.
 
Come on now, lets include error reporting here
 
got it working

Thanks for that
 
So what was the problem?

___________________________________
[morse]--... ...--[/morse], Eric.
 
Oops my bad! I made an error in my haste.
I meant to type in
Code:
$connection = mysql_connect("localhost","username","password")
    or die("Couldn't make connection.");
$db = mysql_select_db("databasename", $connection)
    or die("Couldn't select database.");
$results = mysql_query($sql,$connection);

Thanks for correcting me on that!

[cheers]
Cheers!
Laura
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top