Config php for mysql

Add MySql connection to a Configuration file

I wrote a php class which connects to a MySql DB, and then does an INSERT. I have done this, and it works. Now, i want to add the connection part of the code to a configuration file, where i could call it from here. How could i do this in PHP ?

what have you tried? also new code should not use the mysql_* functions you should learn and use PDO or mysqli.

@Tynarus I mean, to add the connection string in 1 file, and then access it where ever i want from another PHP file. (Something like a configuration file)

Use require_once to retrieve the configuration file? It will automatically run anything inside of the file you’re fetching. php.net/manual/en/function.require-once.php

Provided the configuration file is another PHP file, I should say. Apologies. HTML files work too, but then it would just display the HTML inside the file.

4 Answers 4

connect to a mysql database by using ph is very simple. you write the code in a ok way, although it is better to use MySQLi or PDO not MySQL function, because they may have some sql injection or other security problems. give your file a name like config.php and add it to whenever you want to use mysql connection. use require_once or include functions of php to use this.

Читайте также:  Python requests http adapter

yes. you must do that.however it is not necessary, it is just because standard programmers are advised not to use mysql function.this is just for security reasons, although it works ok.

also you can use a if else condition in your config file. you can set in it , what server you are working on. if you working on localhost,then you can make a connection fir this ,else if you are on a live server then make another connection for this. then your one config file works fine on both station and you will not need to edit it. if you find me helpful, please vote me:)

I don’t understand exactly but por put this parameters in a configuration file you do this Configuration File

$config['hostname'] = 'yourserver'; $config['user'] = 'youruser'; $config['password'] = 'password'; 

and then call this in your class connection

mysql_connect($config['hostname'],$config['user'],$config['password']); 

or more easy build a construct with the parameters of conexion, and look as this

function __construct($host,$user,$password,$database)

a guide detail of advantages and disadvantages of pdo and mysqli [link]net.tutsplus.com/tutorials/php/… but PDO id a library general when you unknow the database server that use and mysqli is specific for mysql

As from a notice from PHP they wont be officially deprecating the mysql_* extension till at least PHP 5.5/6.0 so you have some time to rewrite your script before those function are completely gone or E_DEPRECATED errors start appearing. http://news.php.net/php.internals/53799

So use PDO or mysqli for new scripts:

Connect with PDO

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC); >catch (Exception $e)< die('Cannot connect to database. Details:'.$e->getMessage()); > ?> 

Right now, if the above code is in your config file then you can use include(‘config.php’) and then $con will contain your connection object.

As $con is not global scope you would need to pass it to your class via the construct.

db = $con; > function someMethod($valueA,$valueB)< $sql = "INSERT INTO TABLE ( colA, colB )VALUES( :colA, :colB )"; $query = $this->db->prepare($sql); $query->bindParam(":colA", $valueA); $query->bindParam(":colB", $valueB); $query->execute(); > > $aClass = new yourclass($con); $aClass->someMethod('someValueforA','someValueforB'); ?> 

Источник

Config php for mysql

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide. Alternatives to this function include:

For compiling, simply use the —with-mysql[=DIR] configuration option where the optional [DIR] points to the MySQL installation directory.

Although this MySQL extension is compatible with MySQL 4.1.0 and greater, it doesn’t support the extra functionality that these versions provide. For that, use the MySQLi extension.

If you would like to install the mysql extension along with the mysqli extension you have to use the same client library to avoid any conflicts.

Installation on Linux Systems

Note: [DIR] is the path to the MySQL client library files (headers and libraries), which can be downloaded from » MySQL.

ext/mysql compile time support matrix

PHP Version Default Configure Options: mysqlnd Configure Options: libmysqlclient Changelog
4.x.x libmysqlclient Not Available —without-mysql to disable MySQL enabled by default, MySQL client libraries are bundled
5.0.x, 5.1.x, 5.2.x libmysqlclient Not Available —with-mysql=[DIR] MySQL is no longer enabled by default, and the MySQL client libraries are no longer bundled
5.3.x libmysqlclient —with-mysql=mysqlnd —with-mysql=[DIR] mysqlnd is now available
5.4.x mysqlnd —with-mysql —with-mysql=[DIR] mysqlnd is now the default

Installation on Windows Systems

PHP 5.0.x, 5.1.x, 5.2.x

MySQL is no longer enabled by default, so the php_mysql.dll DLL must be enabled inside of php.ini . Also, PHP needs access to the MySQL client library. A file named libmysql.dll is included in the Windows PHP distribution and in order for PHP to talk to MySQL this file needs to be available to the Windows systems PATH . See the FAQ titled «How do I add my PHP directory to the PATH on Windows» for information on how to do this. Although copying libmysql.dll to the Windows system directory also works (because the system directory is by default in the system’s PATH ), it’s not recommended.

As with enabling any PHP extension (such as php_mysql.dll ), the PHP directive extension_dir should be set to the directory where the PHP extensions are located. See also the Manual Windows Installation Instructions. An example extension_dir value for PHP 5 is c:\php\ext

Note:

If when starting the web server an error similar to the following occurs: «Unable to load dynamic library ‘./php_mysql.dll'» , this is because php_mysql.dll and/or libmysql.dll cannot be found by the system.

PHP 5.3.0+

The MySQL Native Driver is enabled by default. Include php_mysql.dll , but libmysql.dll is no longer required or used.

MySQL Installation Notes

Crashes and startup problems of PHP may be encountered when loading this extension in conjunction with the recode extension. See the recode extension for more information.

Note:

If you need charsets other than latin (default), you have to install external (not bundled) libmysqlclient with compiled charset support.

Источник

Config php for mysql

The mysqli extension was introduced with PHP version 5.0.0. The MySQL Native Driver was included in PHP version 5.3.0.

Installation on Linux

The common Unix distributions include binary versions of PHP that can be installed. Although these binary versions are typically built with support for the MySQL extensions, the extension libraries themselves may need to be installed using an additional package. Check the package manager that comes with your chosen distribution for availability.

For example, on Ubuntu the php5-mysql package installs the ext/mysql, ext/mysqli, and pdo_mysql PHP extensions. On CentOS, the php-mysql package also installs these three PHP extensions.

Alternatively, you can compile this extension yourself. Building PHP from source allows you to specify the MySQL extensions you want to use, as well as your choice of client library for each extension.

The MySQL Native Driver is the recommended client library option, as it results in improved performance and gives access to features not available when using the MySQL Client Library. Refer to What is PHP’s MySQL Native Driver? for a brief overview of the advantages of MySQL Native Driver.

The /path/to/mysql_config represents the location of the mysql_config program that comes with MySQL Server.

mysqli compile time support matrix

PHP Version Default Configure Options: mysqlnd Configure Options: libmysqlclient Changelog
5.4.x and above mysqlnd —with-mysqli —with-mysqli=/path/to/mysql_config mysqlnd is the default
5.3.x libmysqlclient —with-mysqli=mysqlnd —with-mysqli=/path/to/mysql_config mysqlnd is supported
5.0.x, 5.1.x, 5.2.x libmysqlclient Not Available —with-mysqli=/path/to/mysql_config mysqlnd is not supported

Note that it is possible to freely mix MySQL extensions and client libraries. For example, it is possible to enable the MySQL extension to use the MySQL Client Library (libmysqlclient), while configuring the mysqli extension to use the MySQL Native Driver. However, all permutations of extension and client library are possible.

Installation on Windows Systems

On Windows, php_mysqli.dll DLL must be enabled in php.ini .

As with enabling any PHP extension (such as php_mysqli.dll ), the PHP directive extension_dir should be set to the directory where the PHP extensions are located. See also the Manual Windows Installation Instructions. An example extension_dir value is c:\php\ext .

Note:

If when starting the web server an error similar to the following occurs: «Unable to load dynamic library ‘./php_mysqli.dll'» , this is because php_mysqli.dll cannot be found by the system.

User Contributed Notes

Источник

Config php for mysql

The behaviour of these functions is affected by settings in php.ini .

MySQL Configuration Options

Name Default Changeable Changelog
mysql.allow_local_infile «1» PHP_INI_SYSTEM
mysql.allow_persistent «1» PHP_INI_SYSTEM
mysql.max_persistent «-1» PHP_INI_SYSTEM
mysql.max_links «-1» PHP_INI_SYSTEM
mysql.trace_mode «0» PHP_INI_ALL
mysql.default_port NULL PHP_INI_ALL
mysql.default_socket NULL PHP_INI_ALL
mysql.default_host NULL PHP_INI_ALL
mysql.default_user NULL PHP_INI_ALL
mysql.default_password NULL PHP_INI_ALL
mysql.connect_timeout «60» PHP_INI_ALL

For further details and definitions of the PHP_INI_* modes, see the Where a configuration setting may be set.

Here’s a short explanation of the configuration directives.

Allow accessing, from PHP’s perspective, local files with LOAD DATA statements

Whether to allow persistent connections to MySQL.

The maximum number of persistent MySQL connections per process.

The maximum number of MySQL connections per process, including persistent connections.

Trace mode. When mysql.trace_mode is enabled, warnings for table/index scans, non free result sets, and SQL-Errors will be displayed. (Introduced in PHP 4.3.0)

The default TCP port number to use when connecting to the database server if no other port is specified. If no default is specified, the port will be obtained from the MYSQL_TCP_PORT environment variable, the mysql-tcp entry in /etc/services or the compile-time MYSQL_PORT constant, in that order. Win32 will only use the MYSQL_PORT constant.

The default socket name to use when connecting to a local database server if no other socket name is specified.

The default server host to use when connecting to the database server if no other host is specified. Doesn’t apply in SQL safe mode.

The default user name to use when connecting to the database server if no other name is specified. Doesn’t apply in SQL safe mode.

The default password to use when connecting to the database server if no other password is specified. Doesn’t apply in SQL safe mode.

Connect timeout in seconds. On Linux this timeout is also used for waiting for the first answer from the server.

User Contributed Notes

Источник

Оцените статью