Jump to content

Connect To Mysql


arunpatal

Recommended Posts

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

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

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

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

 

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

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

Archived

This topic is now archived and is closed to further replies.

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