Jump to content

PHP Beginners: Connect to MYSQL

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Epatron

Epatron

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
Hey guys!
I'm intermediate PHP coder, and I'm new in CODECALL.
So I wanted to share my knowledge to beginner coders!

Connect to MYSQL:

Connecting to mysql is very easy to learn and to code, and when you're using
<?php include ("yourfile.php"); ?>, you can get connection to your every files!

Create PHP file named eg. "connect.php", and start writing 1st code into it!
I'm going to use variables in code so it will be simple, so first start with variables like this:
<?php 

$host = "   "

$user = "   "

$pass = "   "

?>
Inside the quotes write your details, details must be correct otherwise connection will fail!

Now lets continue the code and write more code:

$connection = mysql_connect($host,$user,$pass) or die ();

We created variable with name "connection" which stores a mysql_connect(servername,username,password); function.
Attention: servername is our variable $host
username is our variable $user
password is our variable $pass

Now we should have:
<?php

$host = "Your SERVERNAME here"

$user = "Your USERNAME here"

$pass = "Your PASSWORD here"


$connection = mysql_connect($host,$user,$pass) or die ();

echo "Connected to MYSQL";

?>
Added just one echo writing under the mysql_connect function, so remember to add it too!

Now just try to run the code and see if its working!
Any problems? Reply!

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
You should at least take this approach rather than killing the script. What if you include() the file and it kills the script, will you know off hand where it is being killed?
 <?php 
$host = "Your SERVERNAME here" 
$user = "Your USERNAME here" 
$pass = "Your PASSWORD here" 

$connection = mysql_connect($host,$user,$pass); 
if($connection !== FALSE) {
  echo "Connected to MySQL"; 
} else {
  echo "Cannot connect to MySQL"; //mysql_error() can be logged or displayed to show why exactly
  //error handling here
  //die(); //only if you are not allowed to continue the script, it may look bad for the user (no nice error telling them no database connection)
}
?>  

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 554 posts
  • Location:/etc/passwd
Also don't you need a semicolon after each variable declaration, even if you dont its good practice.
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users