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
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.")");
...
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.