Unable to access table data from php script - php

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.

Related

PHP code not retrieving data from database?

I use the VirtueMart component on my site.
I have this piece of code running on Joomla 3.6:
<?php
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all records from the user profile table where key begins with
//"custom.".
// Order it by the ordering field.
$query->select($db->quoteName(array('customfield_value',
'virtuemart_product_id')));
$query->from($db->quoteName('jos_virtuemart_product_customfields'));
$query->where($db->quoteName('virtuemart_custom_id') . ' LIKE '. $db-
>quote('3'). ' AND '. $db->quoteName('virtuemart_product_id') . 'LIKE ' .
$db->quote($product->virtuemart_product_id));
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more
options on retrieving data).
$results = $db->loadResult();
echo ($results);
?>
However, for some reason, it's not loading the data. I just get a blank page. On my localhost it's working fine, yet on my live site it's not loading at all.
Any reason why this could be? It's using the same database (phpmyadmin).
Hosting issue possibly?
I don't know. I've tested everything offline for a week and it all went swimmingly and then this happens. I don't see ANY errors in the console, so I don't know what could be wrong.
Thanks for reading.
This problem arises when you transfer your database to the server. because some of the servers have a different configuration for the database server like it may be running a database server on localhost:8080. so first check your DB config file for configuration.if you still found the same problem then I will suggest enabling PHP error reporting to 1 so that you can see what actually causing this problem.

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

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');

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

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