vipsa Posted November 4, 2013 Share Posted November 4, 2013 Hi I am new to php I have a small form that collects names and email addresses and inserts it into a database. I have an existing database connection for another form so I want to use that connection so I included the connection file in this script. When I try and insert new content I get errors( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\wamp\www\headfirst\emailscript\addemail.php on line 14 Here is the code <code> <?php $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; echo "Hi $firstname $lastname. <br> Thank you for sunmitting the form<br> we will be sending you an email soon to $email"; include $_SERVER['DOCUMENT_ROOT'] . 'headfirst/includes/db_connection.inc'; $sql = "use elvis" . "insert into email_list (first_name, last_name, email)" . "values ('$firstname', '$lastname', '$email')"; mysqli_query($dbconnection, $sql); mysqli_close($dbconnection); ?> </code> So my question is this: "How do I use an existing connection and how do I initialize it or what do I have to do please? Help is appreciated Quote Link to comment https://forums.phpfreaks.com/topic/283575-initialize-a-database-connection/ Share on other sites More sharing options...
vipsa Posted November 4, 2013 Author Share Posted November 4, 2013 My question is also this: "If I closed a database connection how do I open that connection again" Quote Link to comment https://forums.phpfreaks.com/topic/283575-initialize-a-database-connection/#findComment-1456834 Share on other sites More sharing options...
Ch0cu3r Posted November 4, 2013 Share Posted November 4, 2013 (edited) So my question is this: "How do I use an existing connection and how do I initialize it or what do I have to do please? When connecting to MySQL using mysqli you can use either procedural style or object style (read http://us2.php.net/manual/en/mysqli.construct.php). Example proceducal connection $conn = mysqli_connect('mysql hostname', 'mysql username', 'mysql password', 'msyql database name'); // procedural style connection When using procedural msyqli_*() functions, any function that requires the link identifier you pass it usually as the first argument. For example for running a query you'd do mysqli_query($con, $query). When I try and insert new content I get errors ( ! ) Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\wamp\www\headfirst\emailscript\addemail.php on line 14 You get that error when you are not connected to the database. The database connection in db_connection.inc may have failed. Can you post how you are connecting to the database My question is also this: "If I closed a database connection how do I open that connection again" You reconnect again using mysqli_connect(). But it is recommend to only connect to the database once, rather than connecting/disconnection between queries. Database connections are closed once PHP has finished parsing the code. $sql = "use elvis" . "insert into email_list (first_name, last_name, email)" . "values ('$firstname', '$lastname', '$email')"; That will result in an error. You cannot run multiple queries together using mysqli_query(). Is elvis the name of your database? You state which database to use when connecting to the database not in the query. Edited November 4, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283575-initialize-a-database-connection/#findComment-1456846 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.