PHP创建了mysql表,但也给出了“Table not created”错误

PHP创建了mysql表,但也给出了“Table not created”错误

问题描述:

I have the following script to create tables in a mysql database

$dbhost = 'localhost';
$dbuser = 'root';
$con = "mysql_connect($dbhost, $dbuser, '')";
$create=mysql_query("CREATE TABLE `$table_name` (ID int NOT NULL AUTO_INCREMENT KEY,high TEXT ,low TEXT ,Divergence TEXT ,Change_Percent TEXT ,Low_Price TEXT ,High_Price TEXT ,Low_Proximity TEXT ,Vol_Index TEXT ,Vol TEXT ,Purchase_Value TEXT)");

                    if (mysql_query($con,$create))
                        {
                        echo "Table created successfully";
                        }
                    else
                        {
                        echo "Error creating table: " . mysql_error($conn);
                        }

Executing the script does create an the desired table, however it also gives the following error

Warning: mysql_query() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\test\test.php on line 136
Error creating table:

Now according to me this means $create returns "FALSE" which would be the case if the table was not created, however in this case the table has been created, what then is making it a Boolean? and How can I stop this error??

我有以下脚本在mysql数据库中创建表 p>

  $ dbhost ='localhost'; 
 $ dbuser ='root'; 
 $ con =“mysql_connect($ dbhost,$ dbuser,'')”; 
 $ create = mysql_query(“CREATE TABLE` $ table_name  `(ID int NOT NULL AUTO_INCREMENT KEY,高TEXT,低TEXT,Divergence TEXT,Change_Percent TEXT,Low_Price TEXT,High_Price TEXT,Low_Proximity TEXT,Vol_Index TEXT,Vol TEXT,Purchase_Value TEXT)“); 
 
 if(mysql_query(  $ con,$ create))
 {
 echo“表创建成功”; 
} 
 else 
 {
 echo“创建表错误:”。  mysql_error($ conn); 
} 
  code>  pre> 
 
 

执行脚本会创建所需的表,但是它也会出现以下错误 p> \ n

 警告:mysql_query()期望参数2是资源,布局在第136行的C:\ xampp \ htdocs \ test \ test.php中给出
创建表:
  code>   pre> 
 
 

现在根据我的意思,这意味着$ create返回“FALSE”,如果没有创建表就会出现这种情况,但是在这种情况下表已经创建了,那么它是什么呢? ? 以及如何停止此错误? p> div>

the condition should be

                    if ($create)
                    {
                    echo "Table created successfully";
                    }

u have already run the query and u are pasing again the mysql_query($con,$create)

Your connection is not a connection, it's a string:

$con = "mysql_connect($dbhost, $dbuser, '')";

Try creating a real connection instead:

$con = mysql_connect($dbhost, $dbuser, '');

You are also running mysql_query twice. The first is without a connection parameter. Try running it once instead. And the connection should be the second, not the first, parameter:

$create = "CREATE TABLE ...";
if (mysql_query($create, $con)) {

You are running the $create query twice. try this:

 if ($create)
 {
    echo "Table created successfully";
 }

You supply second parameter for mysql_query() which causes an error. Also, you misspelled $con in mysql_error(). Use this code instead:

$dbhost = 'localhost';
$dbuser = 'root';
$con = mysql_connect($dbhost, $dbuser, ''); // error: string instead of function
$create=mysql_query("CREATE TABLE `$table_name` (ID int NOT NULL AUTO_INCREMENT KEY,high TEXT ,low TEXT ,Divergence TEXT ,Change_Percent TEXT ,Low_Price TEXT ,High_Price TEXT ,Low_Proximity TEXT ,Vol_Index TEXT ,Vol TEXT ,Purchase_Value TEXT)");

                    if ($create) // error: second mysql_query()
                        {
                        echo "Table created successfully";
                        }
                    else
                        {
                        echo "Error creating table: " . mysql_error($con); // error: misspelled con
                        }