MySQL_Num_Rows issue with multiple DB and Functions

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
rbc
Forum Newbie
Posts: 22
Joined: Tue Sep 02, 2008 6:00 pm

MySQL_Num_Rows issue with multiple DB and Functions

Post 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?
Last edited by rbc on Thu Sep 18, 2008 3:31 pm, edited 1 time in total.
rbc
Forum Newbie
Posts: 22
Joined: Tue Sep 02, 2008 6:00 pm

Re: MySQL_Num_Rows issue with multiple DB

Post 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.
rbc
Forum Newbie
Posts: 22
Joined: Tue Sep 02, 2008 6:00 pm

Re: MySQL_Num_Rows issue with multiple DB

Post 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);
}
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: MySQL_Num_Rows issue with multiple DB

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
rbc
Forum Newbie
Posts: 22
Joined: Tue Sep 02, 2008 6:00 pm

Re: MySQL_Num_Rows issue with multiple DB

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