One MySQL Database & Multiple Table
How to connect to a certain table in MySQL database?
I have tried :
mysql_select_db("baybeestore",$connection);
but it gives me an error :-
"Error : Table 'baybeestore.form' doesn't exist"
But I've created a table named as order
. Are there any PHP codes to connect to my table order
in the same database that have multiple databases?
Is it wise enough to create multiple database & one table for one website or multiple table with one database?
FULL CODE :
$connection = mysql_connect("localhost","user","1234"); if(!$connection) { die('Failed to connect to MySQL :' . mysql_error()); } mysql_select_db("baybeestore",$connection) $sql = "INSERT INTO form(name, address, email, handphone, item, payment) VALUES ('$strname', '$straddress', '$stremail', '$strhandphone', '$stritem', '$strpayment')"; if(!mysql_query($sql, $connection)) { die('Error : ' . mysql_error()); } echo "Data have been saved."; mysql_close($connection);
As per your edit/added code. Your originally posted code
You're using two different tables for your queries. Result: ERROR
You have SELECT * FROM order
and INSERT INTO form
2 different animals altogether.
If anything, that should be INSERT INTO order
or SELECT * FROM form
yet... ORDER
is a reserved word and should be enclosed with backticks.
Ie:
INSERT INTO `order`
This is a very confusing question. mysql_select_db("baybeestore",$connection);
has nothing to do with table names, so your error must be coming from somewhere else. Also, the error refers to the table form
, but you say you have created a table named order
.
To answer your questions:
You don't connect to tables; you connect to databases and run queries against tables. Connect to my_database
or whatever, then run queries like SELECT * FROM my_table
.
One database, multiple tables -- definitely not multiple databases, which is much more complicated, generates a ton of overhead, and doesn't help in any way.
Maybe you are confusing things. You are supposed to connect to a database and then run queries against one or multiple tables in that database.
So if you have database My_Database
and tables table1
, table2
you could run
mysqli_select_db($connection, "My_Database");
and then run queries like
$result = mysqli_query("SELECT * FROM table1 WHERE 1 LIMIT 1");
please note that I have changed the function names to mysqli_*
because mysql_*
functions are deprecated and should not be used anymore
上一篇: 声明变量时未定义的索引
下一篇: 一个MySQL数据库和多表