Boo-urns Posted January 20, 2009 Share Posted January 20, 2009 I setup 2 classes one for common mysql functions then decided that I should move to mysqli. Only problem when I changed everything up to use mysqli is that sometimes I'm getting: Fatal error: Cannot redeclare class Or if I mess around with it I can get it to just timeout. Both things we don't need! Any ideas? Thanks, -Corey Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2009 Share Posted January 20, 2009 A 1,000,000 different programmers could have written the post you just made and there could be a different problem in each of their programs that produce those same symptoms. To get specific help with what you are doing wrong in your code, you need to post your code and the corresponding error messages. Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/#findComment-741391 Share on other sites More sharing options...
Boo-urns Posted January 20, 2009 Author Share Posted January 20, 2009 Form page processing: <?php if(isset($_POST['submitConfirm'])) { echo "SUBMITTING"; // including the classes require_once('classes/class.mysqli.php'); require_once('classes/class.validate.php'); // Database name used for mysql class $dbName = 'dbname'; // --- Start the class instances --- // $db = new MySQLI($host, $dbUser, $dbPass, $dbName); // new mysqli class instance // host / user / pass is from an include } ?> Part of the class (error is: Fatal error: Cannot redeclare class mysqli in [path]/class.mysqli.php on line 3) (the declaration of the class line) <?php class MySQLI { /* FREQUENT USED SQL FUNCTIONS */ function MySQLI($host, $user, $pass, $db) { $startTime = $this->getMicroTime(); // Try to make a connection to the server if (!$this->connection = @mysqli_connect($host,$user,$pass,true)){ $this->errorCode = mysqli_errno(); $this->errorMsg = mysqli_error(); echo $this->errorMsg; return false; } // Now select the database if (!@mysqli_select_db($db,$this->connection)){ $this->errorCode = mysqli_errno(); $this->errorMsg = mysqli_error(); echo $this->errorMsg; @mysqli_close($this->connection); return false; } $this->totalTime += $this->getMicroTime() - $startTime; return true; } } ?> However something must be off because it works fine when i switch to using mysql. (not mysqli) Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/#findComment-741424 Share on other sites More sharing options...
RussellReal Posted January 20, 2009 Share Posted January 20, 2009 you're including mysqli.php more than once Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/#findComment-741433 Share on other sites More sharing options...
Boo-urns Posted January 20, 2009 Author Share Posted January 20, 2009 So is it best to include everything outside of the form submission? I put everything outside and every other class is working besides the mysqli class. Still giving me the same error about redeclaring. Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/#findComment-741470 Share on other sites More sharing options...
PFMaBiSmAd Posted January 20, 2009 Share Posted January 20, 2009 mysqli is the name of a built in class. You need to name your class something else. To switch between using mysql/mysqli functions in a class you would typically just change the code in a class and just include the correct class, but keep the class name the same so that you don't need to go through the application code and change more of it than necessary. Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/#findComment-741471 Share on other sites More sharing options...
Boo-urns Posted January 20, 2009 Author Share Posted January 20, 2009 Yup that was it. Does mysql not have a built in class? Thanks, Corey Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/#findComment-741488 Share on other sites More sharing options...
Philip Posted January 20, 2009 Share Posted January 20, 2009 MySQL doesn't have a built in class. MySQLi does, as it was aimed at improving MySQL & making it more OOP style Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/#findComment-741490 Share on other sites More sharing options...
Mchl Posted January 20, 2009 Share Posted January 20, 2009 Also note, that if you're using mysqli, the you're programming in PHP5 (as mysqli was not available in PHP4), but your code is using PHP4 syntax for classes. It has changed a bit in PHP5 and while old syntax will work (for some time, probably until PHP6), you should try to use the new one. Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/#findComment-741526 Share on other sites More sharing options...
Boo-urns Posted January 20, 2009 Author Share Posted January 20, 2009 So for my classes is the main change in php5 public/private function and use of extends, constructors...? http://us.php.net/zend-engine-2.php I am very new to OOP programming. Thanks for the guidance. Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/#findComment-741537 Share on other sites More sharing options...
Mchl Posted January 20, 2009 Share Posted January 20, 2009 Yup. These are those. extends was actually available in PHP4. The important change (at the beginner's level of OOP) is use of visibility declarations (public/protected/private) as well as static methods and properties. Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/#findComment-741545 Share on other sites More sharing options...
Boo-urns Posted January 20, 2009 Author Share Posted January 20, 2009 So if I'm in a class and i want to use mysqli_query in that class I should use the procedural version and not the oop style since it is already in a class? i.e. $link = mysqli_connect("localhost", "user", "password", "db"); Quote Link to comment https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/#findComment-741561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.