I have a series of websites, all of which must share the same information. Any updates to text must be made across all websites. Instead of editing each site individually and uploading the updates files one at a time, I figured it'd be far better to have a central source using MySQL - update the database, and all websites will be changed at once.
I have limited knowledge of PHP and MySQL - everything below is what I've been able to put together for myself so far, using various online sources:
<?php
//DB INFO///////////////////////////////////////////////////////////////////////////////////////
$host="localhost"; // Host name
$username="####"; // Mysql username
$password="####"; // Mysql password
$db_name="####"; // Database name
// Connect to server and select database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get all the data from the "example" table
$pge_logbookabout = "SELECT * FROM pge_logbookabout";
$pge_logbookabout = mysql_query($pge_logbookabout) or die(mysql_error());
$row_pge_logbookabout = mysql_fetch_assoc($pge_logbookabout);
?>
So far, I can use the above to select a table and echo in the HTML using:
<?php echo $row_pge_logbookabout['rep_lbapr'];?>
That's cool, but I'm only able to select one single table using this - I'd like to be able to select ALL tables, and simply enter variables in where I need them.
Will I need to repeat the third section of the above code for each table, or is there a simpler way for me to do this?
To display all records in a table, you need to do:
while($row_pge_logbookabout = mysql_fetch_assoc($pge_logbookabout)){
echo $row_page_logbookabout['COLUMN'];
}
However if you mean that you want to display all records in each table, therefore you need separate queries to do so.
$query = mysql_query("select * from table1");
while($row_table1 = mysql_fetch_assoc($query)){
// code here
}
$query = mysql_query("select * from table2");
while($row_table2 = mysql_fetch_assoc($query)){
// code here
}
Please note this way of connecting to database, quering and fetching data will be deprecated starting PHP 5.5.0. Alternatively you can use PDO prepared statements
If you want to process over a series of tables you can use something like:
$query_tables = mysql_query('SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name LIKE "%table%" ');
while($table = mysql_fetch_assoc($query_tables)){
$query = mysql_query("select * from ".$table['table_name'] );
// code here
}
You will have to make sure you choose the proper table names in the first query so you are processing the proper tables.
Then you can use php to loop over the tables updating a particular column/field. Not sure if that is what you are looking for...
More information at http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
I agree with the above poster... switch to PDO_MYSQL.
Select multiple tables using mysql
Update mutiple tables using mysql
How are you hosting your websites? Are they on the same hosting? If that is the case, you can use localhost. Otherwise you will need to enter the external hosting mysql database credentials.(multisite single database setup).
First you select all tables, then you get the data from each one.
You could do this if you are using mysql_*, but I strongly recommend to use mysqli_* or PDO.
$result = mysql_query("show tables"); // Select all tables
while($table = mysql_fetch_array($result)) {
$queryT = mysql_query("select * from '".$table[0]."'");
while($rtable = mysql_fetch_array($queryT)){
// data of the table
}
}
Related
I have to insert data in two different database's table.
I have created database1 and table1 for database1,
also i have created database2 and table2 for database2.
For inserting data i have written code,
$connect = mysql_connect("localhost","root",""); //database connection
mysql_select_db("database1",$connect); // select database1
mysql_select_db("database2",$connect); // select database2
$sql = mysql_query("INSERT INTO database1.table1 (contact_first, contact_last, contact_email) VALUES('abc','xyz','abc#abc.com')"); //insert record to first table
$sql1 =mysql_query("INSERT INTO database2.table2 (contact_first, contact_last, contact_email) VALUES('abc','xyz','abc#abc.com')"); //insert record to second table
please suggest me corrections for above code to insert data.
Try the following code:
$connect1 = mysql_connect("localhost","root","");
mysql_select_db("database1", $connect1);
$res1 = mysql_query("query",$connect1);
$connect2 = mysql_connect("localhost","root","",true);
mysql_select_db("database2", $connect2);
$res2 = mysql_query("query",$connect2);
Note: So mysql_connect has another optional boolean parameter which
indicates whether a link will be created or not. as we connect to the
$connect2 with this optional parameter set to 'true', so both link will
remain live.
Simply connect to 1 database, insert new row, disconnect, connect to the other database, insert row into that one and disconnect.
Or you can use $connect1 and $connect2 to refer to each of them separately and do the insertion parallely.
EDIT: Btw you can select the database with the 4'th parameter of mysql_connect, no need to use mysql_select_db
And very important, you should write mysqli not mysql. Because mysql functions are not going to be supported for much longer.
first create two database connections
$connect1 = mysql_connect("localhost","root","");
$connect2 = mysql_connect("localhost","root","");
Then select the database for each connection.
mysql_select_db("database1",$connect1); // select database1
mysql_select_db("database2",$connect2); // select database2
Then pass in a second argument for mysql_query which is the respective connection for the query.
mysql_query("SELECT ... ", $connect1);
mysql_query("SELECT ... ", $connect2);
Well, if there's a pattern in db names, tables and queries are exactly the same, you can use a loop:
for ($i = 1; $i <=2; $i++) {
mysql_select_db("database".$i, $connect);
$sql = mysql_query("INSERT INTO table".$i." (contact_first, contact_last, contact_email) VALUES('abc','xyz','abc#abc.com')");
mysql_close;
}
However, using mysql_* is strongly NOT recommended, as it is deprecated from the last stable PHP release and is considered unsafe. Use PDO or MySQLi instead. PHP's official site suggests the article "Choosing an API": http://www.php.net/manual/en/mysqlinfo.api.choosing.php
Well, that's how i do it...
1 - connect --> that you are doing right
2 - check for errors
3 - USE a database (1) you want to put data in (and not 'SELECT')
4 - check for errors
5 - now INSERT items into the database THAT IS BEING USED - that is (1)
6 - check for errors
7 - USE the other database (2)
8 - check for errors
9 - INSERT the data into (2) - because that is the one in use now
10 - check for errors
Yes, be paranoid :P Hope this helps
i am trying to connect to two databases to create a search engine for a couple of my databases. Heres a test code. can someone tell me what i am doing wrong or if it is possible. thanks.
mysql_connect("localhost","user","pass");
mysql_select_db("db1");
mysql_select_db("db2");
$search=mysql_query("SELECT * from db1.repairs, db2.order from db1,db2");
while($row=mysql_fetch_array($search)){
echo $row['first_name']." ".$row['esn']." ".$row['order_type']."<br>";
}
You can query across databases if you specify the database name before the table name like this
SELECT a.col1, b.col2
FROM db1.table1 AS a
INNER JOIN db2.table2 AS b ON a.someIdFromA = b.someIdFromB
As Korcholis mentions the problem is in your select. Also you do not want to use the mysql_* functions if you can avoid it. PDO or MySqli are preferred.
Edit
At least this works using MySQL. I would bet it works for most other RDBMSes as well, but I don't have others handy to test and I can't say if this conforms to SQL standards or not. Comments anyone?
You can use
<?php
$db1 = mysql_connect("localhost","user","pass");
$db2 = mysql_connect("remote","user","pass");
mysql_select_db("db1", $db1);
mysql_select_db("db1", $db2);
$query1 = mysql_query("USE somedatabase", $db1);
$query2 = mysql_query("USE otherdatabase", $db2);
Or try with a class that handles these connections in a different instances
http://www.joni2back.com.ar/programacion/php-class-for-mysql-databases/
mysql_connect returns a $resource. You can connect twice and select a database with each one (in fact, you can select a database from the connect itself), and then use each connection.
However, your problem is that your SELECT is incorrect. You are trying to select fields from tables from databases, which is not correct. In fact, you cannot fetch two different databases in a so fancy way, because they are considered two sets of information independent and unrelated between them. That's why tables exist, to fit that problem.
This other answer, however, may have a solution.
Otherwise, you could connect to each database using two mysql_connect and two resources, fetch the values, and cross them yourself. Not the best option, I know, but an answer that could fit your needs.
PS: If you are beginning the project right now, switch to Mysqli or PDO. Mysql is deprecated.
Try to review this, and maybe you can't query a database with querying FROM:
<?php
$con1 = mysqli_connect("$hostname", "$user1", "$password1", "$db1");
if (mysqli_connect_errno($con1)) {
echo mysqli_connect_error();
}
$con2 = mysqli_connect("$hostname", "$user2", "$password2", "$db2");
if (mysqli_connect_errno($con2)) {
echo mysqli_connect_error();
}
$search1 = mysqli_query($con1, "SELECT * from $db1table");
$search2 = mysqli_query($con2, "SELECT * from $db2table");
/* Other PHP codes here */
mysqli_close($con1);
mysqli_close($con2);
?>
You can even improve this code, nor minimized it!
I added to a database a new table using PHPMyAdmin; when trying to access it from a PHP page I get the dreaded MySQL error "table doesn't exist".
Database connection data are OK, they are used a few lines above on the same page to access another table in the same database. If I do SHOW TABLES in PHPMyAdmin the new table is listed; if I do it from a PHP page the new table does not appear in the list.
Engine for the new table is MyISAM, like all other tables in the database. I can access the db server only via PHPMyAdmin.
Sorry, I forgot the code, here it is:
$db = mysql_connect ($db_host, $db_user, $db_password) or
die("Error message here");
$db_select = mysql_select_db($db_name, $db)or die("Error message here");
$query = ("SELECT * FROM `old_table`");
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
// do stuff - here it works
}
$query = ("SELECT * FROM `new_table`");
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
// do stuff - here it does not work
echo mysql_error();
}
On Unix, table names are case sensitive. On Windows, they are not. Fun, isn't it? Kinda like their respective file systems. Do you think it's a coincidence?
It probably depends on table type; MyISAM in your case.
Field names are case-insensitive regardless.
For database and table names, it depends on the underlying operating system. Identifier Case Sensitivity
The answer is simple: some typo or another silly mistake like this: you're connecting wrong server, editing wrong file or something of the kind. Just double check everything.
There is no particular error to cause this
The answer is: the table name or at least one of the field names is a reserved word.
To solve it, you can enclose the fields and table name with grave accents (`), e.g:
SELECT `value` FROM `pivot`;
Are these 2 equivalent, if not how can I make them. I'm using php/Mysql (I'll use mysqli later)
mysql_select_db("db_App", $link);
$result = mysql_query("SELECT * FROM AppOne");
OR
$result = mysql_query("SELECT * FROM db_App.AppOne"); // how can I get this to work like above?
You'll always have to select a database. From then on, specifying the database in the query is useless. It's better not to specify it there anyway, as that'd make you run into troubles if your database changes at some point.
if you skip the $link in mysql_select_db("db_App", $link);
they should do the same.
I am looking forward to connect two different database from different servers. Also, i would want to run a query that fetches data from both the database into a single result. Am doing this in PHP script with mysql. here is how am looking forward to do it [ without success :) ]
$dbh1 = mysql_connect('server1', 'uname', 'pwd')or die("Unable to connect to MySQL1");
$dbh2 = mysql_connect('server2', 'uname', 'pwd') or die("Unable to connect to MySQL2");
mysql_select_db('db1', $dbh1);
mysql_select_db('db2', $dbh2); //both will have same table name though
$qry = mysql_query("select * from db1.table1 where db1.table1.id='100' and db1.table1.id=db2.table1.id",$dbh1) or die(mysql_error());
$row= mysql_fetch_array($qry);
echo $row[2];
Am not getting any result or either error. Any help is appreciated, tnx.
According to the PHP docs:
http://il2.php.net/manual/en/function.mysql-query.php
"If the link identifier is not specified, the last link opened by mysql_connect() is assumed."
So in this case you're only retrieving data from $dbh2.
I don't think it's possible to do what you are trying to do with one query. You should merge the results after you get them.
It doesn't work that way. You can use multiple databases in a single SQL query, but it always operates on one connection handle. If you need to connect to two different servers, you have to use two queries and merge data in PHP.