Click to See Complete Forum and Search --> : mysql_fetch_field()


PittsburghRed
08-08-2006, 04:43 PM
I have created a tiny database as a learning tool; I plan to use a database to store membership info gathered from a webpage form.

I am trying to print out the table:

$getrecords="SELECT * FROM Members";
$result=mysql_query($getrecords,$conn);


print "<TABLE BORDER=1>\n";
print "<TR>\n";
while ($field=mysql_fetch_field($result)){
print " <TH>$field->name</TH>\n";
}// end while
print "</TR>\n\n";

//parse and print result set
while ($row=mysql_fetch_assoc($result)){
print "<TR>\n";
foreach ($row as $col=>$val){
print " <TD>$val</TD>\n";
} // end foreach
print "</TR>\n\n";
}// end while

I get the following error meassge:

Warning: mysql_fetch_field(): supplied argument is not a valid MySQL result resource in /home/endteach/public_html/database.php on line 21
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/endteach/public_html/database.php on line 27

Are these invalid functions in MySql? In the version on my server? Or is something else wrong that provokes this error response?

The Little Guy
08-08-2006, 04:49 PM
try mysql_fetch_array();

NogDog
08-08-2006, 08:52 PM
The "supplied argument is not a valid MySQL result resource" error almost always means that MySQL was unable to parse your query, thus your mysql_query() command returned FALSE instead of resource ID. Always check the result of your mysql_query() for failure, for example:

$result=mysql_query($getrecords,$conn) or die("Query failed: $getrecords - ". mysql_error());

PittsburghRed
08-09-2006, 09:23 AM
Yes, thank you, you are correct. I added the check query line and received error:
Query failed: SELECT * FROM Members - No Database Selected

Here is how I connect:
//connect to database
$conn=mysql_connect("localhost","myusername","mypassword");
mysql_select_db("mydatabasename",$conn);

using the actual username, password, and database name, of course. I don't think I am using the incorrect databasename but I will check this.

LiLcRaZyFuZzY
08-09-2006, 09:46 AM
try this for connecting:


//connect to database
$conn = mysql_connect("localhost","myusername","mypassword");
$db_selected = mysql_select_db("mydatabasename",$conn);

if (!$db_selected) {
die ('Can\'t use database : ' . mysql_error());
}

NogDog
08-09-2006, 09:59 AM
And the programming lesson to learn from all of this: always assume that if it can fail, it will fail, so code defensively and check your return values. :)

PittsburghRed
08-09-2006, 02:02 PM
OK. I backed off trying to print out the table and am concentrating on connecting to the database.
I wrote (with the above advice):
<?
//connect to database
$conn = mysql_connect("localhost","endteach_PghRed","password");
$db_selected = mysql_select_db("endteach_NAPTAmembers",$conn);

if (!$db_selected) {
die ('Can\'t use database : ' . mysql_error());
}

//create query
$sql="INSERT INTO NAPTAmembers
VALUES ('Smith','Jane','AppletStu@aol.com')";
$result=mysql_query($sql,$conn) or die("Query failed: $sql - ". mysql_error());

?>
NO ERROR MESSAGES were given but the transaction did not take place. I'm simply not getting a connection to the database. I'm going to see what's in $conn.

PittsburghRed
08-09-2006, 06:02 PM
Well guys ...Putting the trace on $conn revealed that username and password did not have access to the database. At the time of creation through my server panel, I gave me a username and a password at its request.They do not work. I substituted my username and password that I use to access the server in the first place and voila! Apparently, that's what I should have responded in the first place. Am a stupid?

Anyway the table prints out perfectly. And I have a little form that correctly passes a record to the database. Now to build the real thing. Thanks for listening.

LiLcRaZyFuZzY
08-09-2006, 06:34 PM
;)