Access MySQL database problem!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
driverm
Forum Newbie
Posts: 3
Joined: Wed Feb 02, 2011 3:22 pm

Access MySQL database problem!

Post by driverm »

Hi. I am getting crazy here, I would be very pleased if any of you can help me with this problem. Here is the thing...
The following code works perfectly:

Code: Select all

                $user_name = "*****"; // replace your user name within the 2x "
		$password = "*****"; // replace your password within the 2x "
		$database_name = "*****"; // replace the database name within the 2x "
		$table_name = "*****"; // replace the table name within the 2x "
		
		$dbh = mysql_connect("*****", $user_name, $password, $database_name);

		$query="SELECT streamLink FROM `$database_name`.`$table_name` WHERE streamName = 'Sands'";
		$result = mysql_query($query, $dbh);
		$num=mysql_num_rows($result);
		echo ($num);				

However when I try to use a function for the query and the rest, I have a problem there.. For instance, when I try this:

Code: Select all

                $user_name = "*****"; // replace your user name within the 2x "
		$password = "*****"; // replace your password within the 2x "
		$database_name = "*****"; // replace the database name within the 2x "
		$table_name = "*****"; // replace the table name within the 2x "
		
		$dbh = mysql_connect("*****", $user_name, $password, $database_name);
		
		function findStreamLink($stream_name)
		{
			$query="SELECT streamLink FROM `$database_name`.`$table_name` WHERE streamName = '$stream_name' ";
			$result = mysql_query($query, $dbh);
			$num=mysql_num_rows($result);
			echo ($num);				
		}

I face with the following problem:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in *****/index.php on line 42
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ******/index.php on line 43

then I tried to change the code a bit to check where the problem is:

Code: Select all

                $user_name = "*****"; // replace your user name within the 2x "
		$password = "*****"; // replace your password within the 2x "
		$database_name = "*****"; // replace the database name within the 2x "
		$table_name = "*****"; // replace the table name within the 2x "
		
		$dbh = mysql_connect("*****", $user_name, $password, $database_name);
		
		function findStreamLink($stream_name)
		{
                        if(!$dbh)
                        {
                                 echo('sth wrong');
                        }
                        else
                        {
			$query="SELECT streamLink FROM `$database_name`.`$table_name` WHERE streamName = '$stream_name' ";
			$result = mysql_query($query, $dbh);
			$num=mysql_num_rows($result);
			echo ($num);
                        }				
		}

after this it returned "sth wrong". So, i guess i m having a database connection problem while i try to use mysql_query in the function.

Thank you very much for your suggestions...

--
Pasha Latzo
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Access MySQL database problem!

Post by Jade »

You have to make the variable global to the function by passing it as a parameter or declaring it global.

Code: Select all

                $user_name = "*****"; // replace your user name within the 2x "
                $password = "*****"; // replace your password within the 2x "
                $database_name = "*****"; // replace the database name within the 2x "
                $table_name = "*****"; // replace the table name within the 2x "
               
                $dbh = mysql_connect("*****", $user_name, $password, $database_name);
               
                function findStreamLink_byParameter($stream_name, $dbh)
                {
                        $query="SELECT streamLink FROM `$database_name`.`$table_name` WHERE streamName = '$stream_name' ";
                        $result = mysql_query($query, $dbh);
                        $num=mysql_num_rows($result);
                        echo ($num);                           
                }

// or you can do it this way

                function findStreamLink_byGlobal($stream_name)
                {
                        global $dbh;
                        $query="SELECT streamLink FROM `$database_name`.`$table_name` WHERE streamName = '$stream_name' ";
                        $result = mysql_query($query, $dbh);
                        $num=mysql_num_rows($result);
                        echo ($num);                           
                }

 

driverm
Forum Newbie
Posts: 3
Joined: Wed Feb 02, 2011 3:22 pm

Re: Access MySQL database problem!

Post by driverm »

i tried to do it and it gives me the error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /*****/index.php on line 44

what should I do now ? any suggestions ?
Peec
Forum Commoner
Posts: 33
Joined: Fri Feb 22, 2008 3:58 am

Re: Access MySQL database problem!

Post by Peec »

Have you forgotten to select database ?

Code: Select all

mysql_select_db("database", $link);

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Access MySQL database problem!

Post by John Cartwright »

It is always good practice to attach or die(mysql_error()) on your mysql commands to see if there is an error.

I.e.,

Code: Select all

$dbh = mysql_connect("*****", $user_name, $password, $database_name) or die(mysql_error());
$result = mysql_query($query, $dbh) or die(mysql_error());
mysql_select_db($database, $dbh) or die(mysql_error());
driverm
Forum Newbie
Posts: 3
Joined: Wed Feb 02, 2011 3:22 pm

Re: Access MySQL database problem!

Post by driverm »

Still having the same problem. For a quick review, this is the code that is working:

Code: Select all

$user_name = "*****";
$password = "*****";
$database_name = "*****";
$table_name = "*****";
                
$dbh = mysql_connect("*****", $user_name, $password, $database_name);

$query="SELECT streamLink FROM `$database_name`.`$table_name` WHERE streamName = 'Sands'";
mysql_select_db($database_name, $dbh);
$result = mysql_query($query, $dbh);
$num=mysql_num_rows($result);
$variable=mysql_result($result,0,"streamLink");		
echo ($variable);                      
I am trying to use a function for checking the "streamlink" such as:

Code: Select all

$user_name = "*****";
$password = "*****";
$database_name = "*****";
$table_name = "*****";
                
$dbh = mysql_connect("*****", $user_name, $password, $database_name);

function findStreamLink($stream_name, $dbh)
{
  $query="SELECT streamLink FROM `$database_name`.`$table_name` WHERE streamName = 'Sands'";
  mysql_select_db($database_name, $dbh);
  $result = mysql_query($query, $dbh);
  $num=mysql_num_rows($result);
  $variable=mysql_result($result,0,"streamLink");		
  echo ($variable);
}                      
Using function with: <?php findStreamLink('Sands', $dbh);?> but it gives me the error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in *****/index.php on line 34
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in *****/index.php on line 35

I also tried to use "global $dbh;", no luck there again. nothing changes.

please please help me with this problem. I need to find a solution...
thank you very much for all ur efforts..

--
Pasha Latzo
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Access MySQL database problem!

Post by Jade »

You need to put or die (mysql_error()) after all of your queries like we told you to before:

Code: Select all

function findStreamLink($stream_name, $dbh)
{
  $query="SELECT streamLink FROM `$database_name`.`$table_name` WHERE streamName = 'Sands'";
  mysql_select_db($database_name, $dbh) or die (mysql_error());
  $result = mysql_query($query, $dbh) or die (mysql_error());
  $num=mysql_num_rows($result) or die (mysql_error());
  $variable=mysql_result($result,0,"streamLink") or die (mysql_error());              
  echo ($variable);
}
 
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Access MySQL database problem!

Post by McInfo »

The problem is still variable scope. $database_name and $table_name are defined in the global scope, but not in the function scope.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Access MySQL database problem!

Post by califdon »

McInfo wrote:The problem is still variable scope. $database_name and $table_name are defined in the global scope, but not in the function scope.
...which the "or die(mysql_error())" would have pointed to if you had followed the instructions that you were given. We can only help you if you pay attention to what we say.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Access MySQL database problem!

Post by mikosiko »

sure you were quoting Mcinfo just to address to the OP (driverm) right? :wink:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Access MySQL database problem!

Post by califdon »

Definitely. The "you" in my post refers to the OP.
Post Reply