Forum Moderators: coopster

Message Too Old, No Replies

Connecting to database

         

Amy Ra Ra Ra

12:06 am on Apr 20, 2012 (gmt 0)

10+ Year Member



Hello all, here's some code that I'm using. My goal is to have visitors fill out my form and they get a short confirmation that their registration was submitted. When I fill out the form the info gets inserted into my database but I get the following error:

HERE'S THE ERROR
Your e-mail has been added to our list. We will send you updates on our newest products. Thank you. Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in /home/content/98/7731198/html/mygiftpackage/addemail.php on line 27

//HERES MY CODE
$dbc = mysqli_connect('mygiftpackage.db.7731198.hostedresource.com', 'username', password!', 'databasename')
or die('Error connecting to MySQL server.');

$last_name = $_POST ['lastname'];
$first_name = $_POST ['firstname'];
$email = $_POST ['email'];

$query = "INSERT INTO email_list (last_name, first_name, email)" .
"VALUES ('$last_name', '$first_name', '$email')";

$result = mysqli_query($dbc, $query)
or die('Error querying database.');

if ($result)
{
echo "Your e-mail has been added to our list. We will send you updates on our newest products. Thank you.";
}

mysql_close($dbc);
?>

cffrost2

2:11 am on Apr 20, 2012 (gmt 0)

10+ Year Member



Hi. I'm more familiar with plain old mysql. But research shows mysqli stands for MySQL Improved. It's mainly object oriented. Meaning your code should be written using the class scripting.

Example

//opens connection
$mysqli = new mysqli('host', 'username', 'password', 'db_name');
//select statement
$qry = "SELECT * FROM table";
//query
$result = $mysqli->query($qry);
//store results in an array
$row = $result->fetch_array($result);
//print the result
echo $row['col_name'];
//free the result set
$result->close();
//close connection
$mysqli->close();

Hope this helps lead the way. As I said, I still use the old style much like you have written but it only uses mysql_connect and mysql_close. But I think I may start using the improved mysql.

Amy Ra Ra Ra

2:51 am on Apr 20, 2012 (gmt 0)

10+ Year Member



Oh yippy, I looked at the manual and discovered that I only had to delete mysql_close () statement altogether. I'm so excited that I'm learning and some day I'll be able to make a bigger impact. I'm glad you answered my plea for help otherwise I might have been stuck another day or too :)

cffrost2

2:56 am on Apr 20, 2012 (gmt 0)

10+ Year Member



Glad you figured it out :) Later than php 3.4, the need to close a mysql_connect instance ceased. However, it's still good practice to close or "free" the instance because it returns php and mysql resources. It may one day help speed things up when you make a "bigger impact" ;)

rocknbil

4:17 pm on Apr 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Amy but see my comments here [webmasterworld.com] about using raw input in your applications. It's very dangerous and you need to address it while you're still learning. If there's one bad habit PHP coders form early, it's this. :-)

It's mainly object oriented.


Just a FYI, this is false - you can easily use procedural programming on either mysql or mysqli. See Amy's other thread for a procedural example, it's not that much different than using mysql.

cffrost2

8:20 pm on Apr 20, 2012 (gmt 0)

10+ Year Member



I looked at the thread. Most of the info I found on it elsewhere made it seem object oriented. But I played with it a bit an you're right, you can use procedural programming like mysql. I wonder what the difference is then? "MySQL Improved". What's been improved? Hmmmm, something to look into. Thanks for the info.

rocknbil

5:06 pm on Apr 23, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Object-oriented interface
Support for Prepared Statements
Support for Multiple Statements
Support for Transactions
Enhanced debugging capabilities
Embedded server support


overview [php.net]

:-)

The authors of PHP would **prefer** you use OOP, but it does support procedural programming as well.