Jump to content

Undefined function problem


ebizdynamix

Recommended Posts

Hello

 

I was wondering if someone could help. I’m essentially trying to connect to a MYSQL database using some PHP code in 2 PHP files.

 

What should happen is that the PHP in one file should call the code in the other, resulting in a message on a web page stating how many records were found in a database.

 

What’s actually happening is that an error message is being generated on the resulting web page.

 

This is the web page message generated as a result of trying to run the code in

MYSQL01.PHP using as an include that includes the file CONN2_MYSQL.INC.PHP:

 

 

Fatal error: Call to undefined function dbConnect() in C:\wamp\www\phpprojects\mysql\mysql01.php on line 5

 

The message is confusing because so far as I can see the function dbConnect is fully defined.

 

This is the code in MYSQL01.PHP

 

<?php

include('conn2_mysql.inc.php'); 

//connect to MySQL
$conn = dbConnect('admin');

// prepare the SQL query
$sql = 'SELECT * FROM images';

// submit the query and capture the result
$result = mysql_query($sql) or die(mysql_error());

// find out how many records were retrieved
$numRows = mysql_num_rows($result);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Connecting with MySQL extension</title>
</head>

<body>
<p>A total of <?php echo $numRows; ?> records were found.</p>
</body>

</html>

 

 

This is the code in CONN2_MYSQL.INC.PHP

 

<?php
function dbConnect($type){ 
   if ($type == 'query'){
      $user == 'psquery';
  $pwd == 'fuji';
  }
elseif ($type == 'admin'){
  $user == 'psadmin';
  $pwd == 'kyoto';
  }
else {
 exit('Unrecognized connection type');
 }
 //$conn = mysql_connect('localhost', $user, $pwd) or die ('Cannot connect to server');
 $conn = mysql_connect('localhost', 'psadmin', 'kyoto') or die ('Cannot connect to server');
 mysql_select_db('phpsolutions') or die ('Cannot open database');
 return $conn;
 }
 ?>

 

Additional info:

 

I’m using the following:

 

PHP version 5.2.11

MYSQL version 5.1.36

Apache Server version 2.2.11

Wamp Server software version 2.0

 

 

 

PS. - I tried to use the solution suggested in one post dealing with undefined functions and that only made things worse (and now seemingly permanently unfixable (php.ini damaged)):

 

New error messages:

 

Notice: Undefined variable: user in C:\wamp\www\phpprojects\mysql\conn2_mysql.inc.php on line 8

 

Notice: Undefined variable: pwd in C:\wamp\www\phpprojects\mysql\conn2_mysql.inc.php on line 9

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'psadmin'@'localhost' (using password: YES) in C:\wamp\www\phpprojects\mysql\conn2_mysql.inc.php on line 15

Cannot connect to server

 

 

 

I hope someone can help and many thanks in advance for any assistance or suggestions.

 

 

Link to comment
https://forums.phpfreaks.com/topic/209504-undefined-function-problem/
Share on other sites

You're using all comparison ( == ) operators in the function, when 4 of them should be assignment ( = ) operators.

 

 

if( $type == 'query' ) {
     $user = 'psquery';
     $pass = 'fuji';
}
// And so forth . . . 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.