How do I connect to a database I created in HostGator? - php

Edit: I have googled and searched, read what I could find of their documentation. Even chatted with them. The chatter could help me. This is why I reach to you.
I am a complete beginner and I am having some troubles getting started with databases on hostgator. I guess my question also is valid using other hosts.
I created a db through the cpanel in hostgator and added a user to it.
I copied this script into a test.php in my /public_html/ folder and ran it on my site.
In the script I used the name, user and password from the database and user I previously created in cpanel. This database I can see using phpMyAdmin.
<?php
try
{
//open the database
$db = new PDO('sqlite:localhost;dbname=user_db', 'user_username', 'password');
//create the database
$db->exec("CREATE TABLE Dogs (Id INTEGER PRIMARY KEY, Breed TEXT, Name TEXT, Age INTEGER)");
//insert some data...
$db->exec("INSERT INTO Dogs (Breed, Name, Age) VALUES ('Labrador', 'Tank', 2);".
"INSERT INTO Dogs (Breed, Name, Age) VALUES ('Husky', 'Glacier', 7); " .
"INSERT INTO Dogs (Breed, Name, Age) VALUES ('Golden-Doodle', 'Ellie', 4);");
//now output the data to a simple html table...
print "<table border=1>";
print "<tr><td>Id</td><td>Breed</td><td>Name</td><td>Age</td></tr>";
$result = $db->query('SELECT * FROM Dogs');
foreach($result as $row)
{
print "<tr><td>".$row['Id']."</td>";
print "<td>".$row['Breed']."</td>";
print "<td>".$row['Name']."</td>";
print "<td>".$row['Age']."</td></tr>";
}
print "</table>";
// close the database connection
$db = NULL;
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>
This worked, which is nice, but it created a file in my /public_html/ folder called 'localhost;dbname=user_db'
My issue is that I thought I was connecting to the database I created using cpanel, but when I open phpMyAdmin, that database is empty.
How do I change that script to talk to the database I created using cpanel so that I can reach it using phpMyAdmin?
Edit 2:
So I learned that I need to use mysql, not sqlite because phpMyAdmin is based on MySQL.
Also, using the script from http://www.w3schools.com/php/php_mysql_create_table.asp I was able to connect! So, success! Thank you #mituw16 and #Fred -ii- for helping me! :D

I am assuming you created a MySQL database if you used PHPMyAdmin even thought the tags say SQL Lite.
It looks like you're trying to create an SQL Lite database instead of opening a connection to your MySQL database.
Change this line to the following
//open the database
$db = new PDO('mysql:localhost;dbname=user_db', 'user_username', 'password');

Related

Unable to access table data from php script

I have a script that is accessing a remote MySQL database. This database has multiple tables with more than 4 billion (yes, I mean billion) records.
Today I created a new table with around 6.3 million records. When I access the database and more specifically the new table via Sequel Pro, I'm able to do queries and get data from any table in the database without any issues, this includes accessing the new table I just created.
THE PROBLEM IS: When I access the same database using the same user credentials via PHP, I am unable to access the new table I just created. I am able to access data in every other table in the database but just not the new table I created.
Here's what I've tried:
I first tried making the connection and queries via PDO. When that didn't work I tried making the connection and queries with mysqli. Neither of these methods worked. In order to make sure I wasn't missing something in my script, I created the script below that gets a list of every table in the database and then accesses each table, grabs 2 records, and outputs them to the screen:
try {
$db = new PDO('mysql:host=IP_ADDRESS;port=PORT_NUMBER;dbname=DB_NAME', 'DB_USER', 'DB_PASSWORD', [
PDO::ATTR_PERSISTENT => true
]);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
$query = $db->query('SHOW TABLES');
$results = $query->fetchAll(PDO::FETCH_COLUMN);
foreach ($results as $r) {
echo '<br><br>' . $r . '<br>';
$res = $db->query("SELECT * FROM `" . $r . "` LIMIT 2");
foreach ($res as $row) {
echo '<br>';
print_r($row);
}
}
This script is able to retrieve records from every table but the new table.
More Information:
-This is a MySQL database on a Windows 10 machine that is hosted remotely.
-The user has access full access to all tables in the DB.
-I created the table with the default DB options: encoding: utf8, collation: utf8_general_ci, table type: InnoDB
-The table was created from Sequel Pro using a INSERT INTO TABLE_NAME
SELECT ..... statement.
-I'm accessing the DB with a Macbook Pro using MAMP Pro and I haven't really changed any of the default PHP settings.
So, I'd be surprised if this helps anyone else. BUT the thing that fixed my problem was adding another field to the table. I did this via Sequel Pro's "Structure" interface. Since my table had 6.4 million records it took a little while to add the field, but once it completed I was able to access the table via PHP without any issues.

Send database table from one server to another database table in different server PHP MySQL

I am trying to send database table from one server to another database table in different server. Here is the PHP code I have tried so far, but to no avail:
<?php
$dblink1=mysql_connect('server', 'user', 'pass'); // connect server 1
mysql_select_db('db1_name',$dblink1); // select database 1
$dblink1=mysql_connect('server', 'user', 'pass'); // connect server 2
mysql_select_db('db2_name',$dblink2); // select database 2
$table='output_table';#not sure if this is correct
$tableinfo = mysql_fetch_array(mysql_query("SHOW CREATE TABLE first_table ",$dblink1)); // get structure from table on server 1
echo $tableinfo;
mysql_query(" $tableinfo[1] ",$dblink2); // use found structure to make table on server 2
$result = mysql_query("SELECT * FROM first_table ",$dblink1); // select all content
while ($row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
mysql_query("INSERT INTO output_table (".implode(", ",array_keys($row)).") VALUES ('".implode("', '",array_values($row))."')",$dblink2); // insert one row into new table
}
mysql_close($dblink1);
mysql_close($dblink2);
echo "complete";
?>
I have tried the output_table with and without the same column names/amount as first_table, but it does not seem to be working.
Again, any help would be appreciated.
The problem might be that $dblink2 is undefined.
$dblink2=mysql_connect('server', 'user', 'pass'); // connect server 2
mysql_select_db('db2_name',$dblink2); // select database 2
Also consider using pdo methods instead because mysql- methods are now less favored to the PDO way of working with database objects.
This is most likely a firewall issue. Most 3rd party hosts don't allow you to access MySQL from 'the outside'.
You can do this if you manage the MySQL server yourself or if you have a host that doesn't deny you to access the database from another machine, but in regular web hosting, this is not common.
o to your phpMyAdmin installation and look for the file config.inc.php. Open that file, and check which hostname / ip is being used

Connecting to a database using php script

I have created a form in HTML and the action is set to a php script. I'm pretty new to php and was wondering if someone could help me out with it? I need to write a script to add the info from the form to a database. I need to create the database and the table as well. I did a lot of reading on the net and I'm still unable to do it. This is the script I have. Please tell me what mistakes I have made. Thank you for all the help.
<?php
$con=mysql_connect("example.com","peter","abc123","my_db");
$sql="CREATE DATABASE user";
if (mysql_query($con,$sql)) {
echo "Database user created successfully";
}
$sql="CREATE TABLE Persons(PID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(PID),firstName CHAR(30),lastName CHAR(30),age INT, dateofbirth DATE, email CHAR(30)";
if (mysql_query($con,$sql)) {
echo "connected to database";
}
$sql="INSERT INTO Persons (firstName, lastName, age, dateofbirth, email) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]','$_POST[dateofbirth]','$_POST[email]')";
if (mysql_query($con,$sql)) {
echo "added to database";
}
mysql_close($con);
?>
I tried all the suggested answers and still not able to do it. Can someone please provide the code to do that? I need to obtain data from a form and insert it into a database using php!
Hi Try This Code,
$con=mysql_connect("example.com","peter","abc123");
$sql="CREATE DATABASE user";
if (mysql_query($sql))
{
echo "Database user created successfully";
}
1.- Don't use mysql_ functions because are deprecated, use mysqli_ functions or PDO instead.
2.- You have several error i guess, first of all you select a database my_db on the connection script, but you are created another database in the next line... it's very strange this behaviour. If this script executes every time then you should change your code (you can't create a database and a table every time.
In the insert string you have an error with the post code, try this:
$sql="INSERT INTO Persons (firstName, lastName, age, dateofbirth, email) VALUES ('{$_POST['firstname']}','{$_POST['lastname']}','{$_POST['age']}','{$_POST['dateofbirth']}','{$_POST['email']}')";
Your CREATE TABLE query will fail because of syntax error. You have to check queries results especially when next query depends on previous (and you're doing operations like creating databases/tables).
Next thing to change is mysql_*. This functions are deprecated and instead you should use PDO or mysqli_* (they are not hard to learn, just try).
And one more important change have to be done in your script. You're getting user input and adding it to query. Don't do that! You have to always assume that user is trying to hack you, so all inputed data have to be checked and filtered. Also it's good to use prepared statements with such data.
if (mysql_query($con,$sql)){
echo "Database user created successfully";
} else {
echo 'Error creating database - ' . mysql_error();
}
Same thing for all your sql statements to see where you went wrong
Change your code (mysql_query($sql)) instead of (mysql_query($con,$sql))

Pass variable data from PHP to shell_exec command

I have a script that clones tables on to uniquely named MySQL databases from a master sql dump file . Each account has their own database, but the table structure is the same for all accounts. My solution was to dump the master database table and then through PHP shell_exec, run the MySQL controller cmd (mysql) to populate the newly created database with default tables.
At Issue: The process works but only when I hard code the accounts unique database name in the master sql dump file.
However, "USE acct_dbID" line inside the master sql file needs to be dynamically set at runtime.
Here is the code:
include('.dbase_credentials'); //constants for connection object
//using PHP built in connection class mysqli
$mysqli = new mysqli(DB_HOST,DB_UNAME,DB_UPWORD,DB_NAME);
if ($mysqli->connect_errno){
echo "Failed to open database connection: ".$mysqli->connect_error;
exit();
}
$dbID=$varNum; //variable, number ID generated earlier in the account setup process
//create database, doesnt return a resultset, no need for object var here
if ($mysqli->query("CREATE DATABASE acct_".$dbID) === TRUE){
//if dbase was created, clone the tables
$res = shell_exec('mysql -u dbaseUser --password=`cat /path/to/pass` -e "source /path/to/master_tables.sql" acct_'.$dbID);
//provide some UI feedback, shell_exec returns null on failure
if ($res!=null){
echo "The tables were cloned!";
}else{
echo "The cloning process failed!";
}
}else{
echo "no database created.";
}
So again, master_tables.sql needs variable data passed it at runtime so the "USE acct_dbID" can be specific to each new account.
Any thoughts are appreciated.
rwhite35
knittl had the right direction for what I need to do. See the comment conversation above. Thanks knittl.
Here is the new code block using multi_query and file_get_contents.
if ($mysqli->query("CREATE DATABASE acct_".$dbID) === TRUE){
$acct="acct_".$dbID;
$query = "USE ".$acct.";";
$query .= file_get_contents('/path/to/master_table.sql');
$result=$mysqli->multi_query($query);
}
Now account can be variable and multi_query will run each query statement in the string. I probably lose some efficiency but gain the ability to clone tables for each new account. The only additional edit made was to the master_table.sql. I removed all the comments that the dump process adds.
Thanks,
rwhite35

Can't access the SQLite database with MAMP and PHP

I have been learning how to program websites lately and the time has come for me to add a database. I have in fact already successfully created a MySQL database and interacted with it with PHP.
My problem is I can't seem to access a SQLite database file with it. I am using MAMP to host locally for now. Here is a snippet of the code I am using to access the db and find and print out a value stored on it.
<?php
$dbhandle = sqlite_open('/Applications/MAMP/db/sqlite/Users');
if ($dbhandle == false) die ('Unable to open database');
$dbquery = "SELECT * FROM usernames WHERE username=trevor";
$dbresult = sqlite_query($dbhandle, $dbquery);
echo sqlite_fetch_single($dbresult);
sqlite_close($dbhandle);
?>
As you have access to the database (your code doesn't die), I'd say there's got to be an error later ;-)
Looking at your SQL query, I see this :
SELECT * FROM usernames WHERE username=trevor
Are you sure you don't need to put quotes arround that string ?
Like this :
SELECT * FROM usernames WHERE username='trevor'
Also, notice that sqlite_fetch_single will only fetch the first row of your data -- which means you might need to use sqlite_fetch_array or sqlite_fetch_object if you want to access all the fields of your resulset.

Categories