Page 1 of 1

Access MySQL database problem!

Posted: Wed Feb 02, 2011 3:36 pm
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

Re: Access MySQL database problem!

Posted: Wed Feb 02, 2011 4:08 pm
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);                           
                }

 


Re: Access MySQL database problem!

Posted: Thu Feb 03, 2011 9:25 am
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 ?

Re: Access MySQL database problem!

Posted: Thu Feb 03, 2011 9:50 am
by Peec
Have you forgotten to select database ?

Code: Select all

mysql_select_db("database", $link);


Re: Access MySQL database problem!

Posted: Thu Feb 03, 2011 9:56 am
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());

Re: Access MySQL database problem!

Posted: Wed Feb 23, 2011 2:26 pm
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

Re: Access MySQL database problem!

Posted: Thu Feb 24, 2011 9:53 am
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);
}
 

Re: Access MySQL database problem!

Posted: Thu Feb 24, 2011 4:40 pm
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.

Re: Access MySQL database problem!

Posted: Thu Feb 24, 2011 5:59 pm
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.

Re: Access MySQL database problem!

Posted: Thu Feb 24, 2011 6:16 pm
by mikosiko
sure you were quoting Mcinfo just to address to the OP (driverm) right? :wink:

Re: Access MySQL database problem!

Posted: Thu Feb 24, 2011 7:30 pm
by califdon
Definitely. The "you" in my post refers to the OP.