/    Sign up×
Community /Pin to ProfileBookmark

mysql_fetch_field()

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:

[code=php]$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>nn”;

//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>nn”;
}// end while
[/code]

I get the following error meassge:
[B]
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 [/B]

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

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@The_Little_GuyAug 08.2006 — try [B]mysql_fetch_array();[/B]
Copy linkTweet thisAlerts:
@NogDogAug 09.2006 — 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:
[code=php]
$result=mysql_query($getrecords,$conn) or die("Query failed: $getrecords - ". mysql_error());
[/code]
Copy linkTweet thisAlerts:
@PittsburghRedauthorAug 09.2006 — Yes, thank you, you are correct. I added the check query line and received error:

[B]Query failed: SELECT * FROM Members - No Database Selected[/B]

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


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.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYAug 09.2006 — try this for connecting:

[code=php]
//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());
}

[/code]
Copy linkTweet thisAlerts:
@NogDogAug 09.2006 — 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. ?
Copy linkTweet thisAlerts:
@PittsburghRedauthorAug 09.2006 — OK. I backed off trying to print out the table and am concentrating on connecting to the database.

I wrote (with the above advice):
[code=php]<?
//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','[email protected]')";
$result=mysql_query($sql,$conn) or die("Query failed: $sql - ". mysql_error());

?> [/code]

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.
Copy linkTweet thisAlerts:
@PittsburghRedauthorAug 09.2006 — 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.
×

Success!

Help @PittsburghRed spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...