arunpatal Posted November 24, 2012 Share Posted November 24, 2012 Hi, This is connect_sql.php code <?php DEFINE ('DB_USER', 'product'); DEFINE ('DB_PASSWORD', ''); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'product'); $mydatabase = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() ); ?> This is product.php code <?php // Connect to the MySQL database require "connect_sql.php"; $sqlCommand = "CREATE TABLE product_table ( id int(11) NOT NULL auto_increment, product_name varchar(255) NOT NULL, date_added date NOT NULL, PRIMARY KEY (id), UNIQUE KEY product_name (product_name) ) "; if (mysqli_connect($sqlCommand)){ echo "Your products table has been created successfully!"; } else { echo "CRITICAL ERROR: products table has not been created."; } ?> But i can not make product table............ Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/ Share on other sites More sharing options...
Pikachu2000 Posted November 24, 2012 Share Posted November 24, 2012 You aren't executing the query; you're trying to connect to the database again instead. mysqli_query Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394827 Share on other sites More sharing options...
arunpatal Posted November 24, 2012 Author Share Posted November 24, 2012 On 11/24/2012 at 8:33 PM, Pikachu2000 said: You aren't executing the query; you're trying to connect to the database again instead. mysqli_query then what would be the code in product.php to connect and make table??? Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394828 Share on other sites More sharing options...
Pikachu2000 Posted November 24, 2012 Share Posted November 24, 2012 Did you bother to read the manual entry I linked to? Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394831 Share on other sites More sharing options...
arunpatal Posted November 24, 2012 Author Share Posted November 24, 2012 On 11/24/2012 at 8:49 PM, Pikachu2000 said: Did you bother to read the manual entry I linked to? Oh I did but could not understand....... i am new and learning basics....... can you please tell me that how can i make this product table by connecting connect_sql.php Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394832 Share on other sites More sharing options...
Love2c0de Posted November 24, 2012 Share Posted November 24, 2012 if(mysqli_query($myDatabase,$sqlCommand)){ echo "table created."; } else{ echo "critical error, could not create table."; } You can use the variable $myDatabase in your products.php page because you have 'included/required' that file, so the variables become available to the calling script. Regards, AoTB. Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394834 Share on other sites More sharing options...
arunpatal Posted November 24, 2012 Author Share Posted November 24, 2012 Still got error Here is connect_sql.php code <?php // This file also establishes a connection to MySQL // and selects the database. // Set the database access information as constants: DEFINE ('DB_USER', 'test'); DEFINE ('DB_PASSWORD', ''); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'test'); // Make the connection: $mydatabase = @mysqli_connect ("DB_HOST", "DB_USER", "DB_PASSWORD", "DB_NAME") OR die ('Could not connect to MySQL: ' . mysqli_connect_error() ); ?> Here is products.php to make table <?php // Connect to the MySQL database require "connect_sql.php"; $sqlCommand = "CREATE TABLE product_table ( id int(11) NOT NULL auto_increment, product_name varchar(255) NOT NULL date_added date NOT NULL, PRIMARY KEY (id), UNIQUE KEY product_name (product_name) ) "; if(mysqli_query($mydatabase,$sqlCommand)){ echo "table created."; } else{ echo "critical error, could not create table."; } ?> the error i get is Could not connect to MySQL: php_network_getaddresses: getaddrinfo failed: No such host is known. Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394838 Share on other sites More sharing options...
Pikachu2000 Posted November 24, 2012 Share Posted November 24, 2012 Remove the error suppression @ operator from your connection code and forget it exists. Constants don't get enclosed in quotes. Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394840 Share on other sites More sharing options...
arunpatal Posted November 24, 2012 Author Share Posted November 24, 2012 On 11/24/2012 at 9:18 PM, Pikachu2000 said: Remove the error suppression @ operator from your connection code and forget it exists. Constants don't get enclosed in quotes. I removed @ operator from my connection code and now it look like this <?php // This file also establishes a connection to MySQL // and selects the database. // Set the database access information as constants: DEFINE ('DB_USER', 'test'); DEFINE ('DB_PASSWORD', ''); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'test'); // Make the connection: $mydatabase = mysqli_connect ("DB_HOST", "DB_USER", "DB_PASSWORD", "DB_NAME") OR die ('Could not connect to MySQL: ' . mysqli_connect_error() ); ?> and now it shows me error like this Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\script\connect_sql.php on line 12 Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\script\connect_sql.php on line 12 Could not connect to MySQL: php_network_getaddresses: getaddrinfo failed: No such host is known. Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394841 Share on other sites More sharing options...
Pikachu2000 Posted November 24, 2012 Share Posted November 24, 2012 On 11/24/2012 at 9:18 PM, Pikachu2000 said: Remove the error suppression @ operator from your connection code and forget it exists. Constants don't get enclosed in quotes. Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394843 Share on other sites More sharing options...
arunpatal Posted November 24, 2012 Author Share Posted November 24, 2012 On 11/24/2012 at 9:58 PM, Pikachu2000 said: can you please please just type the code for both files so that i can copy and paste it and then i will see how it is working.......... please............. this it the connect_sql.php <?php // This file also establishes a connection to MySQL // and selects the database. // Set the database access information as constants: DEFINE ('DB_USER', 'test'); DEFINE ('DB_PASSWORD', ''); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'test'); // Make the connection: $mydatabase = @mysqli_connect ("DB_HOST", "DB_USER", "DB_PASSWORD", "DB_NAME") OR die ('Could not connect to MySQL: ' . mysqli_connect_error() ); ?> now i want to make table the file is product.php and code in it is <?php // Connect to the MySQL database require "connect_sql.php"; $sqlCommand = "CREATE TABLE product_table ( id int(11) NOT NULL auto_increment, product_name varchar(255) NOT NULL date_added date NOT NULL, PRIMARY KEY (id), UNIQUE KEY product_name (product_name) ) "; if(mysqli_query($mydatabase,$sqlCommand)){ echo "table created."; } else{ echo "critical error, could not create table."; } ?> Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394844 Share on other sites More sharing options...
Love2c0de Posted November 24, 2012 Share Posted November 24, 2012 mysqli_connect(DB_HOST,DB_USER,DB_HOST,DB_NAME) or die("cant connect"); Regards, AoTB. Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394848 Share on other sites More sharing options...
arunpatal Posted November 24, 2012 Author Share Posted November 24, 2012 On 11/24/2012 at 10:35 PM, AoTBuNgLe said: mysqli_connect(DB_HOST,DB_USER,DB_HOST,DB_NAME) or die("cant connect"); Regards, AoTB. sorry but i still can not understand...... it would be nice if you show me how to make table in database using this connect_sql.php file this is product table file <?php // Connect to the MySQL database require "connect_sql.php"; $sqlCommand = "CREATE TABLE product_table ( id int(11) NOT NULL auto_increment, product_name varchar(255) NOT NULL date_added date NOT NULL, PRIMARY KEY (id), UNIQUE KEY product_name (product_name) ) "; if(mysqli_query($mydatabase,$sqlCommand)){ echo "table created."; } else{ echo "critical error, could not create table."; } ?> Link to comment https://forums.phpfreaks.com/topic/271125-connect-to-mysql/#findComment-1394849 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.