954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ERROR: supplied argument is not a valid MySQL result resource

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in config.php on line 60


I have checked a previous thread and they said it was due to the '' in the select statement.

But I dont think this is the case here.

All I did was to get this error was to make functions and use those funcitons to run mysql commands like mysql_query, mysql_num_rows, etc in functions and then call those functions rather than calling the commands to be run over and over again (I tired to make it abit more OO but have run into this error).

Any help be appericated? Even a small example on how to use a mysql command in a php function and then call that function to be used in the main, thanks.

Thanks, Regards X

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

You have something wrong in your query. Echo the query and execute it in phpmyadmin/mysql console. You will see the problem! :)

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Nav to the rescue again!

But the thing is nav, the query worked when i was using mysql_fetch_array and now that I have put mysql_fetch_array in a function and call the function it wont work.

Even if I put the query in phpmyadmin I would just get the same output I was getting previously and not the error?

That correct or ???

Possible you can give me a small example of using a function to call a mysql command like mysql_query, etc.

Regards, X

PS: In the meantime ill try debug the error

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

In my function it is returning a 1, why is that? any ideas?

It also seems to only be relevant to mysql_fetch_assoc and mysql_fetch_array functions not mysql_query functions.

Also ran the query in phpMyadmin worked perfectly fine.

Back to debugging...

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Hmm.. Well, It depends on your function definition. Can you post relevant code so that we can take a look ?

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
<?php
$connection = Connection();
$query = "select * from attend";
$result = executequery($query);
while($row = mysql_fetch_array($result)) {
	print "<pre>";
	print_r($row);
	print "</pre>";
}
print "";
$result2 = executequeryandreturnrows($query);
print "<pre>";
print_r($result2);
print "</pre>";
function Connection() {
	$conn = mysql_connect("localhost","root");
	mysql_select_db("test");
	return $conn;
}
function executequery($query) {
	$result = mysql_query($query);
	return $result;
}
function executequeryandreturnrows($query) {
	$result = mysql_query($query);
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$array[] = $row['name'];
	}
	return $array;
}
?>

In the above example, I have 3 functions. One for connection(which is pretty straightforward), the second returns the resultset and the third returns the array result.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

You see your function:

function executequeryandreturnrows($query) {	$result = mysql_query($query);	$array = array();	while($row = mysql_fetch_array($result)) {		$array[] = $row['name'];	}	return $array;}


The error im getting is with fetch_assoc.

I was using

$query = "select * from table";
$result = fetchAssoc($query);

function fetchAssoc($sql) {
 return mysql_fetch_assoc($sql) or die("Error!!!");
}


Can I use it like that or not?

I was reading a book that showed the example used the code like that?

Thanks, Regards X

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

It doesn't really matter if you are using mysql_fetch_assoc or mysql_fetch_array. The problem with your function is, you don't have mysql_query in it. So, it will not execute the query at all ! :)
You can do it this way.

$query = "select * from table";
$result = fetchAssoc($query);

function fetchAssoc($sql) {
$result = mysql_query($sql);
 return mysql_fetch_assoc($result) or die("Error!!!");
}
nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Sorry my fault I forgot to put in:

$result = mysql_query($sql);


I had that in my main. I have a seperate function for each sql command run so one runs mysql_query another mysql_fetch_assoc and so on?

so if my code is like:

function fetchAssoc($sql) {
 return mysql_fetch_assoc($sql) or die("Error!!!");
}

$query = "select * from table";
$result = mysql_query($query);
$output = fetchAssoc($result); 

echo $result['column'];


Will this work or ?

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Yep. It will work. But it will return only 1 record, that is the first one (in mysql_fetch_assoc).

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

lol

Your a genius nav!

So I gather thats why its returning a value of 1 from that function and im getting that error.

So how should I fix the code? return an array varialbe or ???

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Yep ! Have a while loop, insert the values to an array and return the array.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Nav you are a genius!!!

Thats why I keep getting an output of 1.

So what would I need to do to fix it, intial a temp array variable and return it?

Can you show me please so I know for sure that I got it right this time?

Thanks, Regards X

PS: My internet has been capped and thinking I am lagging very bad :(

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

As I have shown you in my example, where I am assigning $row['name'] to $array (which is an array), you can do the same !
You can do,

function fetchAssoc($sql) {
$array = array();
while($row = mysql_fetch_assoc($sql)) {
$array[] = $row;
}
return $array;
}

Then iterate through $array.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Thanks nav, my internet connection isnt helping.

I cant do much now let alone test and debug, ill get onto it and let you know how I go.

Thanks, nav 'the php guru' :)

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Lol..I am not a guru :D ! There are people here who knows php much better than I do.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

lol eh nav same problem came up...

Any ideas?

Is it really worth using functions to use mysql commands to be reused over and over?

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

Yeah.. To reuse your code, functions are always good.. And, the function works fine for me..

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

well ill sleep on it and try it over the weekend.

goodnight

OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
 

:) Okay ! Goodnite!

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You