Jump to content

Initialize a database connection


vipsa

Recommended Posts

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

 

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.