Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/141637-mysql_query-works-but-mysqli_query-isnt/
Share on other sites

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.

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)

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.