Jump to content

[SOLVED] mysql_query with PHP constant


nicangeli

Recommended Posts

Hello all, i am a complete n00b so take it easy.

 

I am writign a script and have come to an issue. I am attempting to write data to a mysql database, but where i have the table name and field names etc i wish to use constants. I hope this makes sense. Below is code.

 

$query = mysql_query("INSERT INTO TABLE_NAME (FIELD_NAME)");

 

where table_name and field_name are constants. Please help.

 

If i have not explained this properly please just ask.

 

Thanks again...

Link to comment
https://forums.phpfreaks.com/topic/58073-solved-mysql_query-with-php-constant/
Share on other sites

I think that PHP only interprets variables in double-quoted strings; not constants. Instead of defining TABLE_NAME and FIELD_NAME as constants, why not just define variables instead? It seems like that would be more standard procedure....

 

Even so, though, you could fix the problem by concatenating:

 

<?php
...
$query = mysql_query("INSERT INTO ".TABLE_NAME." (".FIELD_NAME.")");
...
?>

thanks very much. That worked perfectly, (i knew i was overlookign something).

 

The reason for me too use constants instead of variables is that i was not thinking at the time when i defined all the parts of my database in one file so that if i needed to rename parts of the database, then i would only have to had changed one line of code. Also i believed that a constant should be used as it cannot be changed by the script unlike a variable, however know i realise it make nout difference.

 

Thanks again,

 

Nick

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.