Jump to content

Help connecting to Database


kyle_maddisson

Recommended Posts

Can someone tell me why in the following code, in the 'try' section, this code works: (first line after 'try')

 

 $conn = new PDO("mysql:host=$servername;dbname=$dbasename", $username, $password); even though I have not declared a variable named $dbname,

but if I change it to "$databasename", which I have declared, it doesn't work. I don't understand that.

 

<code>

<?php
$databasename = "removed";
$servername = "removed";
$username = "removed";
$password = "removed";
$id=$_POST[id];
$courseID=$_POST[courseID];
$coursename=$_POST[coursename];
$firstname=$_POST[firstname];
$lastname=$_POST[lastname];
$middlename=$_POST[middlename];
$grade=$_POST[grade];
$year=$_POST[year];
$sex=$_POST[sex];
 
echo "<br/>Connecting to Database.....<br/>";
 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbasename", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO $databasename (studentID, lastName, firstName, middleName, courseName, courseNumber, year, grade, gender) 
VALUES ('$id','$lastname','$firstname','$middlename','$coursename','$courseID','$year','$grade', '$sex')";
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }
$conn = null;
?>

</code>

Link to comment
Share on other sites

In the query you need to use the table name, not the database name.

 

$sql = "INSERT INTO table_name (studentID, lastName, firstName, middleName, courseName, courseNumber, year, grade, gender)
VALUES ('$id','$lastname','$firstname','$middlename','$coursename','$courseID','$year','$grade', '$sex')";
    $conn->exec($sql);

Link to comment
Share on other sites

 

 $conn = new PDO("mysql:host=$servername;dbname=$dbasename", $username, $password); even though I have not declared a variable named $dbname,

but if I change it to "$databasename", which I have declared, it doesn't work. I don't understand that.

You can connect to MySQL without declaring a database. If you attempt to query a table without selecting a database MySQL will report a database not selected error.

Link to comment
Share on other sites

In the query you need to use the table name, not the database name.

 

$sql = "INSERT INTO table_name (studentID, lastName, firstName, middleName, courseName, courseNumber, year, grade, gender)

VALUES ('$id','$lastname','$firstname','$middlename','$coursename','$courseID','$year','$grade', '$sex')";

    $conn->exec($sql);

First of all, thank you for your reply.

 

Does that mean I need to make another variable and assign it the table name ($databasetable = database.table)? I assume you cannot connect to the table because I tried hard coding it there and would get access denied error. Once I added '.Students' to the assigned database name, it worked.

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.