Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未选择数据库phpmyAdmin_Php_Mysql_Database - Fatal编程技术网

未选择数据库phpmyAdmin

未选择数据库phpmyAdmin,php,mysql,database,Php,Mysql,Database,我使用php创建了一个表,它是在我的数据库中创建的(我已经看到了),然后我将代码连接到本地主机,它也成功了,但是,当我尝试使用sql_query插入表中的变量时,它给了我“未选择数据库” 表格声明: require "E:/E-Commerce/OnlineStore/StoreScript(dynamic)/ConnectToMySQL.php"; //Won't exceed unless everything is fine with the "required" $sqlComman

我使用php创建了一个表,它是在我的数据库中创建的(我已经看到了),然后我将代码连接到本地主机,它也成功了,但是,当我尝试使用sql_query插入表中的变量时,它给了我“未选择数据库”

表格声明:

require "E:/E-Commerce/OnlineStore/StoreScript(dynamic)/ConnectToMySQL.php";
//Won't exceed unless everything is fine with the "required"


$sqlCommand = "CREATE TABLE products (
                 id int(11) NOT NULL AUTO_INCREMENT,
                 productName varchar(255) NOT NULL,
                 productPrice varchar(16) NOT NULL,
                 productDetails text NOT NULL,
                 category varchar(16) NOT NULL,
                 subCategory varchar(16) NOT NULL,
                 dateAdded date NOT NULL,
                 PRIMARY KEY (id),
                 UNIQUE KEY productName (productName)
                 ) ";
 echo 'done ';
守则本身:

if (isset($_POST['productName'])) {

    $productName = mysql_real_escape_string($_POST['productName']);
    echo 'name ';
    $productPrice = mysql_real_escape_string($_POST['productPrice']);
    echo 'price ';
    $category = mysql_real_escape_string($_POST['category']);
    echo 'category ';
    $subCategory = mysql_real_escape_string($_POST['subCategory']);
    echo 'subcategory ';
    $productDetails = mysql_real_escape_string($_POST['productDetails']);
    echo 'details ';
    // See if that product name is an identical match to another product in the system
    $sql = mysql_query("SELECT id FROM products WHERE productName='$productName' LIMIT 1");
    echo ' $productName= '.$productName;
    echo ' got the id from the table ';
    $productMatch = mysql_num_rows($sql); // count the output amount
    echo ' performed matching (num rows) ';
    if ($productMatch > 0) {
        echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';
        exit();
    }
    // Add this product into the database now
    include_once "E:\E-Commerce\OnlineStore\StoreScript(dynamic)\CreateProductsTable.php";

    $sql = mysql_query("INSERT INTO products (productName, productPrice, productDetails, category, subCategory, dateAdded) 
        VALUES('$productName','$productPrice','$productDetails','$category','$subCategory',now())",$con) or die (mysql_error());
    echo ' Inserted the variables in the database ';
     $pid = mysql_insert_id();
     echo ' got the id ';
    // Place image in the folder 
    $newname = "$pid.jpg";
    echo ' saved the image in a variable ';
    move_uploaded_file( $_FILES['FileField']['tmp_name'], "../InventoryImages/$newname");
    echo ' saved the image in a folder ';
    header("location: InventoryList.php"); 
    echo ' refreshed ';
    exit();
}
所有回显行都会出现,直到在表声明中出现“已执行匹配(num行)”和“已完成”,然后它会显示“未选择任何数据库”


任何连接到服务器的想法并不意味着连接到它所包含的数据库。 您的问题很可能是由于您确实连接到了SQL server,但由于一台服务器可以包含多个数据库,因此您没有指定查询应该对哪个数据库执行操作。 您可以使用
mysql\u select\u db
(php代码)选择数据库,也可以在SQL server中运行
USE*database name here*
查询(在任何其他查询之前)

还有另一种选择。无论您使用的是哪一个数据库,在表名之前添加数据库名(中间有一个点)都会为该操作选择所述数据库。 例如,
SELECT*FROM Alphabet.Numbers
将从数据库“Alphabet”中的表“Numbers”中进行选择,而不管您以前选择的数据库是什么


我希望这个答案会有用。

您永远不会执行
create
查询。您只需将一些sql填入字符串并说“完成”。如果出现错误,您可能会在某个地方缺少一个设置默认数据库的
mysql\u select\u db
调用;为什么?那么phpmyadmin又有什么关系呢?嗯,我只使用了mysql,但是当它引起很多警告时,我把它们改成了mysqli,然后我又犯了错误,所以我又改了一次。。这会导致任何错误吗?对于phpmyAdmin,通过它,我可以看到这个表,并确保它是在不同的MySQL API不混合的情况下创建的。从连接到查询使用相同的连接;中间没有灰色区域。@Fred ii-你是什么意思?请解释一下啊,它终于起作用了:)谢谢,但数据库仍然是空的:(在第一个代码段中,有CREATE查询的代码段。我注意到您编写了查询,但从未实际执行过。编写查询后,使用
mysqli\u查询(*connection*,*query*)
在服务器上运行查询。
$db_host="localhost";
$db_username="root";
$db_pass="";
$db_name="store";

/*This is a function that connects the code to the online server with the host, username, and password given, or it will display the message associated with die()*/
$con=mysqli_connect($db_host,$db_username,$db_pass, $db_name)or die("Could not connect to mySQL"); 
echo ' connected ';