I got ID for a record from a db and stored in $foo. I'm able to mkdir with name using value in $foo. For example if echo $foo; = 29 my dir name is 29. I want to create db in the same way, but not working and no error.

$conn = new PDO("mysql:host=$db_host1;dbname=$db_database1", $db_username1, $db_password1);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO COMPANY (COMPANY_NAME, COMPANY_STREET, COMPANY_CITY, COMPANY_ZIP, COMPANY_STATE, COMPANY_COUNTRY, COMPANY_PHONE, COMPANY_EMAIL, password, securityq, securitya, vercode)
    VALUES ('$CNAME', '$CSTREET', '$CCITY', '$CZIP', '$CSTATE', '$CCOUNTRY', '$CPHONE', '$CEMAIL', '$password', '$securityq', '$securitya', '$vercode')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";

//select company id where company name is what was entered now	
 $sql =	"Select COMPANY_ID FROM COMPANY where COMPANY_EMAIL='$CEMAIL'"; 
 $CID=mysql_query($sql) or die ('query failed'.mysql_error()); 
 while($row=mysql_fetch_assoc($CID)){
 //echo $row['COMPANY_ID'];
 $foo=$row['COMPANY_ID'];
 }
	// Create database
require_once 'config2.php';
    $conn2 = new PDO("mysql:host=$db_host1", $db_username1, $db_password1);
    // set the PDO error mode to exception
$conn2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if (FALSE===mysql_connect($db_host1, $db_username1, $db_password1)) {
    die('Unable to connect to the database !');
}
mysql_connect($db_host1, $db_username1, $db_password1);
mysql_query("CREATE DATABASE '". $foo ."'");

echo $newdb;
mkdir("./users/$foo/", 0755);

I also tried

mysql_query("CREATE DATABASE  $foo"); 

but wont work. If I replace $foo with test or something

mysql_query("CREATE DATABASE test");

, db name test is created! Please help.

    First things first: [thread=10387467]stop using the MySQL extension[/thread].

    Second thing: a database name like 29 probably needs quoting. http://dev.mysql.com/doc/refman/5.6/en/identifiers.html

    Third thing: Unless all these databases are completely different (and can't be constructed by a program - maybe each user is able to design the database how they like) then your whole approach is wrong from the beginning.

    Oh, and this would appear to be a database problem for the database forum, not a coding problem for the coding forum.

      Weedpacket;11043815 wrote:

      First things first: [thread=10387467]stop using the MySQL extension[/thread].

      Second thing: a database name like 29 probably needs quoting. http://dev.mysql.com/doc/refman/5.6/en/identifiers.html

      Third thing: Unless all these databases are completely different (and can't be constructed by a program - maybe each user is able to design the database how they like) then your whole approach is wrong from the beginning.

      Oh, and this would appear to be a database problem for the database forum, not a coding problem for the coding forum.

      I'm not sure what you mean by "Unless all these databases are completely different (and can't be constructed by a program - maybe each user is able to design the database how they like)". I have a faint feeling you haven't understood my issue.

        I think I answered the problem you asked, but I have a strong feeling that your problem goes much deeper than "I can't create a database named 29". Why do you need so many databases?

          Ok in that case can you please tell me what is the correct syntax for creating the database using php code. The name of the database doesn't have to be 29 or any number. It has to be the value that is stored in $foo. As far as the correctness of approach, I want to experience what the error with this approach is first hand and for thet to happen I need to test out what I have. To test it I need a solution to my question about Syntax.
          Thanks in advance.

            Firstly, you'll need to be sure that the DB user you are connecting to the database with has the necessary permissions to create a database -- which in most apps is not something you want to allow. (Maybe if you're creating your own version of phpMyAdmin?)

            Assuming that's okay, then within the (deprecated) MySQL syntax, probably the simplest thing would be:

            $sql = "CREATE DATABASE `".mysql_real_escape_string($foo)."`";
            if(mysql_query($sql) == false) {
                throw new Exception("Create failed".PHP_EOL.mysql_error().PHP_EOL.$sql);
            }
            
              NogDog;11043835 wrote:

              Firstly, you'll need to be sure that the DB user you are connecting to the database with has the necessary permissions to create a database -- which in most apps is not something you want to allow. (Maybe if you're creating your own version of phpMyAdmin?)

              Assuming that's okay, then within the (deprecated) MySQL syntax, probably the simplest thing would be:

              $sql = "CREATE DATABASE `".mysql_real_escape_string($foo)."`";
              if(mysql_query($sql) == false) {
                  throw new Exception("Create failed".PHP_EOL.mysql_error().PHP_EOL.$sql);
              }
              

              Thank you Nogdog! It worked. You are awesome 🙂

                Write a Reply...