SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    632
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question 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:
    Code:
    <?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.
    Last edited by TechnoBear; Jan 7, 2013 at 13:32. Reason: Username and password removed

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    I suggest you at least move to mysqli instead of mysql (being phased out). Better yet, PDO.

    That said, what you are trying to do is mysql_select_db, and then mysql_query
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    632
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,146
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by etidd View Post
    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.
    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())

  5. #5
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    632
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    632
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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!

  7. #7
    SitePoint Guru etidd's Avatar
    Join Date
    May 2011
    Location
    atlanta, ga
    Posts
    632
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,146
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by etidd View Post
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •