Page 1 of 1
MySQL_Num_Rows issue with multiple DB and Functions
Posted: Thu Sep 18, 2008 2:01 pm
by rbc
I have a situation where I'm running queries on several different DB's from the same file. I've gotten everything working properly, but have noticed that I get a "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource" error from the following code. I need the $$dbconnect to supply the query with the right DB info to link. Without it it doesn't connect to the right DB to run the query against. I've tried a dozen ways to write this with no luck.
Code: Select all
$query = "SELECT x FROM table WHERE y=z";
$result = mysql_query ($query, $$dbconnect);
if (mysql_num_rows($result) == 0) {
This works prefect, but mysql_num_rows won't let me do it this way.
Code: Select all
if (mysql_affected_rows($$dbconnect) == 1) {
Any ideas?
Re: MySQL_Num_Rows issue with multiple DB
Posted: Thu Sep 18, 2008 2:26 pm
by rbc
Ok, just figured out is has to do with the query running within a function. The variable $$dbconnect isn't passed inside the the function to be used in the query. Now to figure out how to pass that into the query and see if that is the real issue.
Re: MySQL_Num_Rows issue with multiple DB
Posted: Thu Sep 18, 2008 2:59 pm
by rbc
I believe that is the issue, however, I still can't get the query to execute. It says: mysql_query(): supplied argument is not a valid MySQL-Link resource. I use this same technique outside the function and it works flawlessly. I've checked $dbconnect variable inside the function and it contains the proper data, so what gives?
Code: Select all
function create($dbconnect) {
// Check against dbase.
$q_code = "SELECT id FROM table WHERE id=id'";
$r_code = mysql_query ($q_code, $$dbconnect);
// Code does not exist.
if (mysql_num_rows($result) == 0) {}
if ($code == FALSE) {
create($dbconnect);
}
Re: MySQL_Num_Rows issue with multiple DB
Posted: Thu Sep 18, 2008 3:17 pm
by VladSun
Remove the second $ char from $$dbconnect.
You are trying to pass variable to mysql_query, that has name equal to the value of $dbconnect
Re: MySQL_Num_Rows issue with multiple DB
Posted: Thu Sep 18, 2008 3:30 pm
by rbc
It's supposed to be that way as I'm trying to pass a variable reference to another variable. For example: I set up several DB connections.
$db1 = mysql_connect () OR die (mysql_error());
$db2 = mysql_connect () OR die (mysql_error());
$dbconnect is a variable that house which DB to connect to from another DB. So in order to get it to pass properly it has to be sent as $$dbconnect since it is a variable referencing another variable.
Anyway, I figure out that the function isn't able to access the MySQL connection script that holds $db1 and $db2 info and thus couldn't execute the query. Not sure why it couldn't access it within the function as I thought it was global, but guess not. Thanks for the help though.