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

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

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

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...

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

<?php
$connection = Connection();
$query = "select * from attend";
$result = executequery($query);
while($row = mysql_fetch_array($result)) {
	print "<pre>";
	print_r($row);
	print "</pre>";
}
print "<br />";
$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.

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

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!!!");
}

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 ?

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

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 ???

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 :(

As I have shown you in my example, where I am assigning $row 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.

Comments
php guru to the rescue!

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' :)

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?

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

Na I cant get it to work still the same error, any ideas why like I told you the previous code in the book im reading had that and it was working fine (they were using variables by references rather than value but that was the only difference I can see) so any ideas? I really want to use functions to use mysql commands but at the moment I think ill have to stick to plain sql commands :(

sure

CODE 1:
 include("config.php");
 $select="SELECT column from table";
 $query=queryFunction($select);
 $fetchAssoc=FetchAssocFunction($query);
 $update="UPDATE table SET column = column + 1";
 $query1=queryFunction($update);

CODE 2:
 echo $fetchAssocFunction[column];

The xxxFunctions are in the config file included.

Anything else I can do to help, let me know.

Thanks, Regards X

PS: You know how I have coded my functions on previous posts (just return mysql_xxx)

I have been doing some process of elimination and it seems that everything is working fine minus the arrays (mysql_fetch_array and mysql_fetch_assoc).

The array I am using is the one you told me to use BUT i just reread the forum and there was one difference I noticed.

while($row = mysql_fetch_array($result)) {
 $array[] = $row['name'];
}

AND

while($row = mysql_fetch_array($result)) {
 $array[] = $row;
}

There the only two functions left (mysql_fetch_array and mysql_fetch_assoc) to debug and get working. I can work on the other ones later.

So that help you find the problem?

while($row = mysql_fetch_array($result)) {
 $array[] = $row['name']; // this will assign the values of only the column "name" to the array $array.
}

AND

while($row = mysql_fetch_array($result)) {
 $array[] = $row; //this will assign the array $row to the array $array. 
}

I don't understand what is not working and I don't see any problem.. :S

I keep getting this error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:\Program Files\Apache2\htdocs\config.php on line 55

This the function:

function dbFetchAssoc($result) {
 $array = array();
 while($row = mysql_fetch_assoc($result)) {
  $array[] = $row;
 }
 return $array;
}

You got any ideas? or should I give up using functions?

PS:
I tried copying your example for page 1 exactly and it gave me the same error.
Could it be due to certain, files commented out in the setup of php, or something similar?
Does that help?

You are passing a wrong value to the function. Are you sure you are passing the result you get when you use mysql_query ?
ie., $result = mysql_query($query); I really think you are doing something wrong right there (the function is fine though!)

Edit: Check your queryFunction. See what it returns. Maybe its returning a wrong value ?

Maybe I am looking at it wrong.

I echo the output from my mysql_query and I got an output of 1?

Why is this?

Then I do the mysql_fetch_assoc and I gather thats why it errors?

How do I change or fix my mysql_query output? because when I dont use a function to do the query and just use straight mysql_query it works fine?

My query function code:

function queries($qeer) {
 return mysql_query($qeer) or die();
}

See anything wrong with that? (straight from the book im using as a guide)

Thanks nav, Regards X

PS: At least we makign progress :D

Yep. Thats the error. If you have update/delete/insert query, mysql_query will return 1 and its definitely not a valid resource. So, mysql_fetch_array / mysql_fetch_assoc wouldn't work. Try passing a select query to your function and see what it returns. It should return a resource :)

Umm are you sure?

Because for me its working the other way around.

My update query works and updates the database.

The error im getting is from a simple
"select column from table" query.

And were back at square one :(

This question has already been answered. Start a new discussion instead.