Using variable to intert sql data

Hi Guys

just wondering if this is possible…i have a form
i have a databse.inc file which has all the info of what is to be added into the database…thsi all works fine but i wanted to pull some things out to create a config.php file

so i had this peice of code

$sql = “INSERT INTO TABLE.relationentity (relation_id, entfirst_id, entsecond_id, priority, relation_type, rec_status)”;
$sql .= “VALUES (E_RELATIONENTITY.Get_New_ID(), 100, $entity_id , 1, :RELATION_TYPE, ‘ACT’)”;

in my config file i created a variable
$entityID = 100;

and in my database.inc file at the top i put in the code
<?php include(‘config.php’); ?>

and added edited the code to

$sql = “INSERT INTO TABLE.relationentity (relation_id, entfirst_id, entsecond_id, priority, relation_type, rec_status)”;
$sql .= “VALUES (E_RELATIONENTITY.Get_New_ID(), $entityID, $entity_id , 1, :RELATION_TYPE, ‘ACT’)”;

will this work? :x

You tell us when you’ve run it.

Personally, I’d look at being a little bit more explicit that the item is a config value.


#config.inc
$config = array(
    'entity_id'    => 100
);


#other.php
include('config.inc');
$sql = 'SELECT * FROM table WHERE entity_id = ' . $config['entity_id'] . ' AND active = 1';

Many newbies don’t understand this question have no relation to SQL
But merely to the very basic strings operations in PHP.
And can be shortened to

//i had this piece of code
echo  "Hello World";
// i created a variable
$name = "World";
//and added edited the code to
echo  "Hello $name";
//will this work?

hmm im trying to test it but it doesnt seem to be picking up the variables in my config.php file

this is a .inc file and my config is a .php file

i have included the config.php into the databse.inc

i have this function in my database.inc

/**

  • Prints error.
    */
    function print_not_allowed() {
    if ($DEBUG > 1) {
    print ‘<br />printing not allowed page<br />’;
    }
    $content = “You can only register from within a registered $name”;
    print_template($content);
    }

$name is a defined variable in my config.php file but this is being printed as “$name” in my html instead of the string assigned to the variable :shifty:

sorry if my questions seem strange i am a newb

Your variables don’t have scope inside a function. I think this page can explain it much better than I will/can. :wink:

how do i turn this into a global variable :injured:

wand what is the correct way to include a file within a function because right now i don’t think its picking up the included file?

i tried this

/**
 * Print error.
 */
function print_not_allowed() {
  global $name;
  include 'config.php';
  if ($DEBUG > 1) {
    print '<br />printing not allowed page<br />';
  }
  $content = "You can only register from within a registered $name";
  print_template($content);
}

still doesnt work :S

do you call your ini file directly from the browser?
If not - why don’t you ease your file structure a bit? To at least 2 nested includes?

Also 2 advises:

  • never say “doesnt work”, but describe in detail, what happened
  • use PHP ability of reporting errors at max.

ini_set(‘display_errors’,1);
error_reporting(E_ALL);

put in your main script is the primer.