Using Connection Strings inside PHP to access MySQL

One of the main forms of coding used on our servers right now is PHP. With PHP there are several different options to use it to create a higher quality website. One of the main options is use PHP with MySQL databases. Using PHP, you have the ability to use coding to connect and run different aspects of the databases.

Changing Connection Strings

For the majority of connections to MySQL databases inside PHP pages you will need the following information:

  • Database name – The name of the database you are attempting to access.
  • Database username – The database user who has access to the database you are using.
  • Database username password – The username password owned by the database username.
  • Server address or IP – This is the address of the server where the database is located.

Adding to table to a MySQL database

The following code can be used in html if you there is a need to add tables to a database. To go along with this code you will need the database name, database username, and database user password. You will need to take that information and replace it inside the PHP coding.

//This starts php code

<?

//connection string info
//username can be found in CP or in initial email
$user=”user1_user1″;

//password of user
$password=”password”;

//database name
$database=”user1_testdb”;

//mysql server can be found in Control panel under mysql icon
$server=”mysql9.ixwebhosting.com”;

//actual connect string
mysql_connect($server,$user,$password);

//select to prepare database for execution
@mysql_select_db($database) or die( “Unable to select database”);

//create db string; id string is reference number; the rest is just data
$query=”CREATE TABLE teacher (id int(6) NOT NULL auto_increment,data varchar(15) NOT NULL,data1 varchar(15) NOT NULL,data2 varchar(20) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))”;

//execute create query
mysql_query($query);

//proper close
mysql_close();

//This ends the php section
?>

Inserting information into the MySQL Database

The code below can be used in html if you have a need to insert information into the MySQL database. To insert into this code you will need the database name, database username, and database user password. When you have complied all of that information you will have to replace the sections in the code with your correct information.

//You need some type of input to put in the database table
//form action must be the name of this page; for example it is formfilltimedb.php
<form action=”formfilltimedb.php” method=”post”>
Data: <input type=”text” name=”data”><br>
Data1: <input type=”text” name=”data1″><br>
Data2: <input type=”text” name=”data2″><br>
<input type=”Submit”>
</form>

<?
//connection string info
//username can be found in CP or in inital email
$user=”user1_user1″;
//password of user
$password=”password”;
//database name
$database=”user1_testdb”;
//mysql server can be found in Control panel under mysql icon
$server=”mysql9.ixwebhosting.com”;

//data from the form to input into database
$data=$_POST['data'];
$data1=$_POST['data1'];
$data2=$_POST['data2'];

//actual connect
mysql_connect($server,$user,$password);

//select to prepare database
@mysql_select_db($database) or die( “Unable to select database”);

//insert db string inserts the data from the form into the database
$query = “INSERT INTO teacher VALUES (”,’$data’,'$data1′,’$data2′)”;

//execute insert query
mysql_query($query);

//proper close
mysql_close();

?>

Reading information from a database and printing it to a html page

The following code will allow users to pull information from a MySQL database and display it on a html page. To use this coding correctly you will need to have your database name, database username, and the database user password to add to the coding allowing it to work correctly for you.

<?
//connection string info
//username can be found in CP or in initial email
$user=”user1_user1″;
//password of user
$password=”password”;
//database name
$database=”user1_testdb”;
//mysql server can be found in Control panel under mysql icon
$server=”mysql9.ixwebhosting.com”;

//actual connect
mysql_connect($server,$user,$password);

//select to prepare database
@mysql_select_db($database) or die( “Unable to select database”);

//query statment
$query=”SELECT * FROM teacher”;

//results of inital query which list all classes
$result=mysql_query($query);

//list num of rows for loop
$num=mysql_numrows($result);

mysql_query($query);

//start count for loop through info returned from database
$i=0;

//Loops through data
while ($i < $num){

//associates data with variable
$data=mysql_result($result,$i,”data”);
$data1=mysql_result($result,$i,”data1″);
$data2=mysql_result($result,$i,”data2″);

//Prints the data
echo ‘Data ‘.$data;
echo ‘<br />’;
echo ‘Data1 ‘.$data1;
echo ‘<br />’;
echo ‘Data2 ‘.$data2;
echo ‘<br />’;

//Increments count through data
i++;
}

//proper close
mysql_close();
?>

Please Note: 

It is important to know when performing these tasks that you understand how your database works, table prefixes, table names, keys, and etc to be able to pull and insert data correctly. SQL can be very touchy and very easily messed up. So it is best to make sure backups are made before making any kind of changes.

Was this article helpful?

Yes (8)
No (0)

We're sorry you didn't find this article very helpful. Please help us improve it by leaving your feedback below.

Error