7 Replies - 2199 Views - Last Post: 27 February 2013 - 02:15 PM Rate Topic: -----

#1 Log1ck  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 13

Problem POSTING to SQL database(quick reply)

Posted 27 February 2013 - 01:19 AM

Im trying to post something to my database. The only way that i can possibly do it at this point is with this code.

<?php

include_once('config.php');

mysql_connect('localhost','admin','password');
mysql_select_db('news');
?>



With this code, it will post to the SQL database;however, this is not my goal.

As you noticed, I have a config.php file.
This file holds all of my SQL login data.

<?php

$config['db_host'] = 'localhost';
$config['db_user'] = 'admin';
$config['db_pass'] = 'password';
$config['db_name'] = 'news';

foreach ( $config as $k => $v ) {
		define (strtoupper($k), $v);
}
?>


I am trying to use my config.php file within my init.php file with this code.

<?php

include_once('config.php');

mysql_connect(DB_HOST,DB_USER,DB_PASS);
mysql_select_db(DB_NAME);

?>


When i input this code I get the an error

********
Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in C:\xampp\htdocs\resources\init.php on line 5
Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in C:\xampp\htdocs\resources\init.php on line 5
Notice: Use of undefined constant DB_PASS - assumed 'DB_PASS' in C:\xampp\htdocs\resources\init.php on line 5
********

What can i do to fix this problem?
If anyone could help i would greatly appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem POSTING to SQL database(quick reply)

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem POSTING to SQL database(quick reply)

Posted 27 February 2013 - 01:37 AM

Try using single quotes for them and see if it helps.

Also, I believe PHP is case sensitive...so give the same case that you used when declaring the variables...

regards,
Raghav

This post has been edited by raghav.naganathan: 27 February 2013 - 01:39 AM

Was This Post Helpful? 2
  • +
  • -

#3 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 799
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Problem POSTING to SQL database(quick reply)

Posted 27 February 2013 - 03:13 AM

Are you sure the include path is correct? What happens if you var_dump($config) on line 4 of init.php?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: Problem POSTING to SQL database(quick reply)

Posted 27 February 2013 - 04:55 AM

This isn't really a Database question, it's a PHP question, so I'll move it there.

Is there a compelling reason you're converting values to constants here? That's just an utter waste of CPU time if that's all you're doing.
Was This Post Helpful? 2
  • +
  • -

#5 Sho Ke  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Problem POSTING to SQL database(quick reply)

Posted 27 February 2013 - 08:07 AM

raghav.naganathan is right, db_name !== DB_NAME .
EDIT: Just noticed the use of strtoupper(). It could be possible that config.php isn't isn't being included in init.php(the code ran fine for me when I put both files in the same directory). Try echoing something in config.php while viewing init.php and see if the result comes up on screen.
$config['db_host'] = 'localhost';
$config['db_user'] = 'admin';
$config['db_pass'] = 'password';
$config['db_name'] = 'news';
 
foreach ( $config as $k => $v ) {
        define (strtoupper($k), $v);
}


What's the point of this? Why not just use:
$config['db_host'] = 'localhost';
$config['db_user'] = 'admin';
$config['db_pass'] = 'password';
$config['db_name'] = 'news';

mysql_connect($config['db_host'],$config['db_user'],$config['db_pass']);
mysql_select_db($config['db_name']);



Or:
define('db_host', 'localhost');
define('db_user', 'admin');
define('db_pass', 'password');
define('db_name', 'news');

mysql_connect(db_host, db_user, db_pass);
mysql_select_db(db_name);



Manually creating an array to hold database information and making constants for each piece of information is a waste of resources as stated by JackOfAllTrades.

If you ever plan on using user-given data to perform queries, you should also probably start to learn something like mysqli or PDO, which can handle Prepared Statements.

This post has been edited by Sho Ke: 27 February 2013 - 08:14 AM

Was This Post Helpful? 2
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem POSTING to SQL database(quick reply)

Posted 27 February 2013 - 08:18 AM

Yeah sorry...I guess I missed the strtoupper() as well :oops:

Nevertheless, an excellent suggestion Sho Ke You have my +1 for that :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#7 Log1ck  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 13

Re: Problem POSTING to SQL database(quick reply)

Posted 27 February 2013 - 11:50 AM

Thanks everyone! was lots of help.

I ended up going with Sho Ke suggestion.

The init.php file was completely pointless. So i took that out of the equation altogether and linked the config.php and it worked fine with the code he suggested.

JackOfAllTrades The reason i used the array and constants was because i was following a tutorial. Im also the stubborn type that tends to stay on one topic till i fully understand it. :P
Was This Post Helpful? 0
  • +
  • -

#8 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 799
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Problem POSTING to SQL database(quick reply)

Posted 27 February 2013 - 02:15 PM

Jack, in that case it is trivial, but there are some cases where it can be handy. Here's a snippet from my code where I use this technique for pathing:
$path_array = array();
$path_array['APPLICATION']			= 'application/';
	$path_array['CONTROLLERS']			= $path_array['APPLICATION'] . 'Controllers/';
	$path_array['MODELS']				= $path_array['APPLICATION'] . 'Models/';
	$path_array['VIEWS']				= $path_array['APPLICATION'] . 'Views/';
$path_array['ASSETS']               = 'assets/';
	$path_array['FONTS']                = $path_array['ASSETS'] . 'Fonts/';
	$path_array['IMAGES']				= $path_array['ASSETS'] . 'Images/';
	$path_array['SCRIPTLIBRARIES']      = $path_array['ASSETS'] . 'ScriptLibraries/';
	$path_array['STYLES']				= $path_array['ASSETS'] . 'Styles/';


foreach ($path_array as $key => $value)
{
	define('PATH_' . $key, $value);
	define('URI_' . $key, URI_ROOT . $value);
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1