Connecting to the MySQL Database

Hello all,

So, I’ve successfully set up the columns and added the first few rows of test data into my database using phpMyAdmin on my web host. Now, the next step is to have the PHP properly set up to connect to the database. I’ve used the mysql_connect() and mysql_close() at the beginning and the end with the login credentials needed for my database entry- now, which method do I use to specify the table name? I see a pg_connect() for Postgre SQL (whatever that is). Then I need to know what PHP function do I use to write an SQL query inside of.

Again, this is for my page at World Review Group.

Code PHP:


<?php
	  /*This is the scripting section for the e-mail collection box.*/
	$email = " ";
	$connection = mysql_connect("worldreviewgroupcom.fatcowmysql.com" , "username" , "password");
	$dbconn = pg_connect("dbname=emailcollection");
	if (!$connection){
		die('Could not connect: ' . mysql_error());
   }
	
	function showWindowAndValidate()
	{
		echo'<div id="activeemailbox"><input type="checkbox" value="Yes, I would like to join the mailing list."><p>Enter the characters you see below.</p></div>' . generateRandom() . ;	
	}
	
	if (isset($_POST["go"]){
		$email = $_POST["go"];
		if (isItAValidEmail($email)){
			showWindowAndValidate();
			//call a function to write the user to database
		} else {
			invalidEntry();
	} else {
		invalidEntry();
		}
	
	/*test the e-mail address submission for valid entry */
	function Boolean isItAValidEmail($email){
		if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
		  return false;
		} else {
		  return true;
		}
	}
	
	function invalidEntry()
	{
		return echo'<div id="activeemailbox"><p id="error">You didn\\'t enter a valid email, silly!</p><input type="submit" value="Go Back" 		  onclick="goBack()"></div>';
	}
	
	function generateRandom()
	{
	
	}
	
	function goBack()
	{
		header();
	}
	
	mysql_close($connection);
	
	//end e-mail box scripting section
?>

Thank you very much for your guidance. It keeps me going.

I suggest you at least move to [FPHP]mysqli[/FPHP] instead of mysql (being phased out). Better yet, [FPHP]PDO[/FPHP].

That said, what you are trying to do is [FPHP]mysql_select_db[/FPHP], and then [FPHP]mysql_query[/FPHP]

Thanks for the response, StarLion.

Now, how would I move to PDO when my web host supports mySQL through phpMyAdmin? I am going to ask them now if it is possible, but I don’t see anything on any part of the web host’s site about either of these database query tools.


I see it stands for PHP Data Objects, and you can use it to create and manipulate databases completely in PHP. Now I’m out of a source of knowledge that teaches how to use it instead of MySQL.

PDO can still utilize MySQL, you just tell it to use the MySQL Driver, the reason why everyone suggests PDO, is it has security baked in, whereas the mysql functions do not.

For more information on how to convert your mysql functions to PDO, see this article (and I strongly recommend the Prepared Statements approach instead of using query())

Great resource. As I started reading, I came about this line:

On Windows, all of the PDO drivers are included in the ext folder that was created when you installed PHP from the pre-compiled binary’s archive. You only need to update your php.ini by adding or uncommenting the line:
1 extension=php_pdo_mysql.dll

The problem is that I never installed PHP, just a local testing environment, since most web hosts already have it installed.

I’m unclear as to how to get this set up.

Well, I did just find this page on Stack Overflow in a web search.

Install PDO in PHP

…and I have installed XAMPP already; so, I bet I can find php.ini somewhere in its installation directory.

Will I need to edit a php.ini file on my web host for when this gets published? I would think so.

XAMPP php.ini is in your \xampp\php\ folder - make sure you restart XAMPP after changing your php.ini

As for your host, it depends what version of PHP they are running. Chances are, it is already enabled.

Cpradio- thank you very much!

XAMPP has the PDO module installed already, and so does my web host (just spoke to them).

I’m all set!