I have 2 db connections, db1 is the main db and db2 is the copy from.
my script is copying from db2 (has this was the development) to make db1 the same.
However i cannot get the create table if not exist to work with using more then 1 database and using "LIKE".
This is the connection:
<?php
$host = 'localhost';
$db1 = "***";
$username_db1 = '***';
$password_db1 = '***';
$db_c1 = mysql_connect($host, $username_db1, $password_db1) or die('Error connecting to Database!<br>'.mysql_error());
$db2 = "***";
$username_db2 = '***';
$password_db2 = '***';
$db_c2 = mysql_connect($host, $username_db2, $password_db2, true) or die('Error connecting to Database!<br>'.mysql_error());
mysql_select_db($db1, $db_c1);
mysql_select_db($db2, $db_c2);
?>
This is the script well part of it.
$tables1 = array();
$tables2 = array();
$res = mysql_list_tables($db1, $db_c1);
while (list($tmp) = mysql_fetch_row($res))
{
$tables1[] = $tmp;
}
$res = mysql_list_tables($db2, $db_c2);
while (list($tmp) = mysql_fetch_row($res))
{
$tables2[] = $tmp;
}
// Tables creates if not exists
foreach($tables2 as $k=>$v)
{
mysql_query("CREATE TABLE IF NOT EXISTS ".$db1.".".$v." LIKE ".$db2.".".$v, $db_c1);
}
I think the problem is with the LIKE i dont think it is reading it correctly, i have var dumped $tables2 and it does come up with the results.
Thanks
try it in mysql first. drop the table 2 then try your query before doing it dynamically. then one by one change your query to variables. i cant read much from the code but that would be my approach
Related
i am new to working in php, i am work on how to alter table dynamically in database through coding in php
database like this,
database name : data_switch
table name - data
did dataname host dbuser dbpwd dbname
1 abc local root root abc_db // here dataname create new database, when register new dataname
2 pqr ubuntu root passwd pqr_db
php code below:
<?php
$dsn = "localhost";
$username = "root";
$password = "passwd";
$db = new PDO("mysql:host=$dsn;dbname=data_switch", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
foreach ($db->query("select * from data") as $row)
{
$dataname = $row['dataname'];
$host = $row['host'];
$dbuser = $row['dbuser'];
$dbpwd = $row['dbpwd'];
$dbname = $row['dbname'];
$connection_array['data1'][] = array(
'dataname' => $dataname,
'host' => $host,
'dbuser' => $dbuser,
'dbpwd' => $dbpwd,
'dbname' => $dbname
);
}
echo "<pre>";
print_r($connection_array);
$dbh = new PDO("mysql:host=$host;dbname=$dbname", $dbuser, $dbpwd);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// here all dataname find but i dont know how to generate dataname's own connection. dynamically
?>
i am getting all dataname in loop but i am not able to dynamically diffrent connection,i am register new dataname in form as like abc then it create a new database dynamically, but when i am alter the table i am not getting how to connection dynamically on each dataname's host,dbname,dbpwd through each own dataname.
Any body having any idea please help to sort it out. Thanks
I know it's an old post but it came up in a search as a possible relation to a question I had.
I have a script doing very similar to what is being asked. You need to place your connections in a dynamic variable.
eg.
I have a list of branches and their respective database connection details in an array called $branchDcom. I can make calls to all my branches without closing connections and opening all the time.
I have a connection function that connects to a database with the sent values in the function conn_dcom_branch('your_server', 'your_db', 'your_username', 'your_password')
The following creates connections to all databases.
$connection = array();
foreach ($branchDcom as $key => $value) {
$branch = $value['Name'];
if (!is_resource($connections[$branch]['conn'])) {
${'connection_'.$branch} = conn_dcom_branch($value['server'], $value['db'], $value['username'], $value['password']);
${'connection_'.$branch.'_db'} = $value['db'];
}
}
This gives me an array of all branch connections and their respective database names.
To use just enter in the branch name and it will use that connection:
$branch = 'select_name';
$query = "
SELECT
your_field
FROM
[".${'connection_'.$branch.'_db'}."].['table_name']
";
$rs = ${'connection_'.$branch}->execute($query);
This may help someone
I try to make a simple IOS app that can connect to mysql database and read one table. But my php code does't work and really have no idea why, it's seems correct to me. The database is in a raspberry phpmyadmin server and the server works great.
I will put my code here and please tell me what's wrong.
<?php
$host = "192.168.2.193";
$db = "produtos";
$user = "root";
$pass = "1234";
$connection = mysql_connect($host, $user, $pass);
if(!$connection)
{
die("Database server connection failed.");
}
else
{
//attempt to select the database
$dbconnect = mysql_select_db($db, $connection);
//check to see if we could select the database
if(!dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$query = "SELECT * FROM produtos";
$resultset = mysql_query($query, $connection);
$records = array();
//loop throught all our records and add them to our array
while ($r = mysql_fetch_assoc($resultset))
{
$records[] = $r;
}
echo json_ecode($records);
echo $resultset;
}
}
?>
Based on the question:
use mysqli_connect rather than mysql_connect because mysql_connect is deprecated and will not work someday. Also what is the the error you are getting? change your die() statement to something more helpful die(mysqli_error($connection));
Based on your comment:
That error would suggest that you either A) don't have the right IP address or B) there is a network issue between your host server and the SQL server, is this code running on the same server that is hosting the SQL database? if so then you can probably just use localhost for your $host
I am working on a small social web application as a final project for my iOS class. I have a profile view controller where all the info about the user from the database is supposed to be displayed on the labels. The problem is that I don't really know the best way to do this. Here is my php script:
<?
// Database credentials
$host = 'localhost';
$db = 'blabla';
$uid = 'blabla';
$pwd = 'blabla';
// Connect to the database server
$link = mysql_connect($host, $uid, $pwd) or die("Could not connect");
//select the json database
mysql_select_db($db) or die("Could not select database");
// Create an array to hold our results
$arr = array();
//Execute the query
$rs = mysql_query("SELECT IdUser, username, fullname, phonenumber, facebook, instagram FROM login");
// Add the rows to the array
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
//return the json result.
echo '{"users":'.json_encode($arr).'}';
?>
So here I get the info about all the users in the database. I am sure this is not the right way to go, so I guess I need to change the SQL query to retrieve the data for the current user only. But how can I do this? Should I put the username which I enter on the login page into an extra variable and then pass it with JSON to this php script and add the 'WHERE username = 'blabla' statement to the SQL query then? If so, how can I pass the variable to this script with JSON?
Can you please give me some sample code? Or is there a different way to do this?
Thank you so much!
<?php
// Database credentials
$host = 'localhost';
$db = 'blabla';
$uid = 'blabla';
$pwd = 'blabla';
// Connect to the database server
$link = mysql_connect($host, $uid, $pwd) or die("Could not connect");
//select the json database
mysql_select_db($db) or die("Could not select database");
//Execute the query
$rs = mysql_query("SELECT IdUser, username, fullname, phonenumber, facebook, instagram FROM login");
// Add the rows to the array
$data = mysql_fetch_array($rs);
foreach($data as $rec){
echo "user: $rec<br>";
}
?>
I need to get some data from a Microsoft SQL Server database at work. When I have the data I need, I need to make an Excel spreadsheet that can be saved locally on my computer.
I found PHPExcel which seems to do the job on the Excel part, but what about getting the data from the Database?
I can't seem to find anything that's recent. Only old tutorials.
Use this way to Fetch the Records :
<?php
$hostname = "192.168.3.50";
$username = "sa";
$password = "123456";
$dbName = "yourdb";
MSSQL_CONNECT($hostname,$username,$password) or DIE("DATABASE FAILED TO RESPOND.");
mssql_select_db($dbName) or DIE("Database unavailable");
$query = "SELECT * FROM dbo.table";
$result = mssql_query( $query );
for ($i = 0; $i < mssql_num_rows( $result ); ++$i)
{
$line = mssql_fetch_row($result);
print( "$line[0] - $line[1]\n");
}
?>
This will fetch each rows from the Data Retrieve and Print on the Page. Use your Required format into that. I mean, Use html Table to show the data in well format.
Use this code to get an data from Database.
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = '192.168.3.50';
// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'sa');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
else{
echo "connected ";
mssql_select_db('Matrix') or die("Wrong DATAbase");
//mssql_query("SELECT Seq_no from dbo.Trans_R WHERE Seq_no = 000001",$link) or die("cannot execute the query");
$query = mssql_query("SELECT Tr_Date,Tr_Time,Tr_Data from Matrix.dbo.Trans_R");
$f = mssql_fetch_array($query);
echo $f['Tr_Date'];
}
?>
Can i know why Negative Vote??
He asked me to :
" but what about getting the data from the Database?"
Hey, I already have a view counter coded, but i need help preventing people from just refreshing and refreshing to add more views. Normally, i Would mark this storing the ip, page name and date viewed in a SQL table where in php, i would run a search to see if someone with that ip has viewed the page within 24 hours, but I run a website that is mostly operated in schools and i want each computer in a computer lab to count as a view when they see the page. Again, i could use cookies but my server isn't sending out cookies right. It works fine on my testserver but not on the dedicated web hosting server. Is there any other way to prevent spam?
Heres my code
function connect() {
$domain = $_SERVER['HTTP_HOST'];
$dbhost = 'censored';
$dbname = 'censored';
$dbuser = 'censored';
$dbpass ='censored';
if ($domain == 'localhost'){
$dbhost = 'localhost';
$dbname = 'db1';
$dbuser = 'root';
$dbpass ='';
}
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$con){
trigger_error("Problem Connecting to the MySQL Server.");
}
$db = mysql_select_db($dbname, $con);
if(!$db){
trigger_error("Problem finding the Database!");
}
return $con;
}
function fetchdata($qry){
connect();
$result = mysql_query($qry);
return $fetch = mysql_fetch_assoc($result);
}
function addcounter($id) {
connect();
$counter = fetchdata("SELECT * FROM counter WHERE `path` = '$id';");
$counter = $counter['counter'];
if(isset($_COOKIE["counter_".$id.""])){
}else{
if ($counter === NULL) {
mysql_query("INSERT INTO counter VALUES (0, '" .$id. "');");
}
echo "<!-- submitting query -->";
mysql_query("UPDATE counter SET counter = `counter`+ 1 WHERE path = ".$id."") or die ('failupdate');
setcookie("counter_$id", "Playcookie_".$id."");
}
}
If you cannot use the user's IP address (presumably because they are behind NAT?) and you cannot use cookies, there's really not much you can do.
You could try to use the IP together with the user agent string (which might be different among different computers in the lab), but this would be both slower and of course far from guaranteed to work.
Other than that I think you 're out of options.