Mysql "insert table name ......" Can table names make variables?

Source: Internet
Author: User
Mysql "insert table name ......" Can table names make variables? Mysql "insert table name ......" Can table names make variables? How to implement it?


Reply to discussion (solution)

$ TableName = 'member ';

Mysql_query ("insert into". $ tableName. "values ()");

$ TableName = 'member ';

Mysql_query ("insert into". $ tableName. "values ()");
For example, each user generates a corresponding data table at the same time during registration. after a 0101 user logs on, I set 0101 (both the user name and the table name) save it to $ _ SESSION ['MSG ']. then I write it like this:
$ TableName = $ _ SESSION ['MSG '];
Mysql_query ("insert ". $ tableName. "(id, name, telephone) values ('$ counters [0]', '$ counters [1]', '$ counters [2]')");
Why are there errors?

What error is prompted?

Before you use session

Session_start?

Mysql_query ("insert ". $ tableName. "(id, name, telephone) values ('$ counters [0]', '$ counters [1]', '$ counters [2]')");

Print your SQL to see what is output

Mysql_query ("insert ". $ tableName. "(id, name, telephone) values ('$ counters [0]', '$ counters [1]', '$ counters [2]')");
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '000000' (id, name, telephone) values ('? 1 ', 'Wang gang', '123')' at line 1
Session_start ();

The table name does not need to be enclosed in single quotes and the SQL statement can be output directly.

Print your SQL to see what is output
Output: insert into '000000' (id, name, telephone) values ('? 1 ', 'Wang gang', '123 ')

mysql_query("insert into `$tableName` (id,name,telephone) values ('$counters[0]','$counters[1]','$counters[2]')");

The table name does not need to be enclosed in single quotes and the SQL statement can be output directly.

Thank you. according to what you said, the debugging was completed,
$ TableName = $ _ SESSION ['MSG '];
$ SQL = mysql_query ('set names utf8 ');
Mysql_query ($ SQL );
$ SQL = "insert into $ tableName (id, name, telephone) values ('$ counters [0]', '$ counters [1]', '$ counters [2]') ";

[Img = http://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/001/face/12.gif "] [/img]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.