Jump to content

Can I Use Php Variables In Mysql Code? (solved)


migroo

Recommended Posts

Okay I don't know if this is the correct way of using PHP variables in my mysql code:

$sql = "CREATE TABLE zpost_".$t.$r."		(		ID int NOT NULL AUTO_INCREMENT,		Content text,		Date VARCHAR(20),		PRIMARY KEY (ID)		)";		// Execute query		mysql_query($sql,$con);

Its not creating the database.I declare the variables in the header. $t is a name I have the user impute in a forum and $r is a random number.Thanks!P.S Not sure if I should post this here or in the PHP thread. If this is the wrong place please tell me.

Link to comment
Share on other sites

Yes you can use variables but you dont need all the "".... business when in a mysql query, you can just use the variableBest to try and combine your varibles before hand though, then you only have 1 variable that is the complete table name rather than part text and part variable combo in the mysql query

Link to comment
Share on other sites

Okay here is what I have done:

$m="zpost_".$t.$r;$sql = "CREATE TABLE $m;		(		ID int NOT NULL AUTO_INCREMENT,		Content text,		Date VARCHAR(20),		PRIMARY KEY (ID)		)";

I have it echo my $m variable and it looks like it should but it still is not creating my data table. Also if I don't use the $m and instead I just write test it creates the data table test. I don't know what I am doing wrong.

Link to comment
Share on other sites

I figured it out I just needed to omit the semicolon after the $m variable.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...