server is disconneting database - php

database get connected successfully....but...
here is my code
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'databasename';
mysql_connect($host, $user, $pass) or die ("Database Not Connected");
mysql_select_db($db) or die ("Database Not Fount");
?>
but the database is regularly disconnecting and connecting after 30-40 minutes....please help me, that what's going on.....

Don't forget to close your connection with mysql_close().
Too many connections will cause problems so that might explain your disconnects.

This may be the problem of confusing variables. Your connection and db-selection shouldn't be confused with queries that will run at a later time.
$conn = mysql_connect($host, $user, $pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
$query = "SELECT id, username FROM users";
In this example, $conn will not be used to reference anything other than my resource. My query, ran at a later time, will be known as $query, so as to not confuse myself.
I would also suggest watching the execution times of your queries, and the number of concurrent connections opened. If you need to, be sure to close your connections:
mysql_close($conn); // note the importance of a unique variable here

Related

can not get mysql INFORMATION_SCHEMA result

help me please guys
I've searched and tried all the articles here
but still can not get results from INFORMATION_SCHEMA
I want to get a relationship between tables using KEY_COLUMN_USAGE but still not showing anything, just displaying a blank page
I do not understand what went wrong:
I've used the code I used to use, as below
$server = "localhost";
$user= "root";
$pass= "";
$db= "my_DB";
$conn = mysql_connect($server, $user, $pass) or die ("can't connect server" .mysql_error());
$openconn= mysql_select_db($db) or die ("can't open DB " .mysql_error());
$sql="using_select_command_here";
$query = mysql_query($sql) or die ("error:" .mysql_error());
while ($data = mysql_fetch_array($query)) {
print_r($data);
}
from all sources are here like :
How to find tables all relation in MySQL
How to know relations between tables
the result still showing blank page:
I do not understand if there are other factors?
I am using php version 5.3.1
and mysql version 5.1.41
from xampp for windows ver1.7.3

Error: mysql_fetch_array() expects parameter 1 to be resource

So here is my code:
$sql = "SELECT * FROM `items`";
$result = mysqli_query($conn, $sql);
while($prommes = mysql_fetch_array($result)){
}
When I'm running it. I'm getting this error:
mysql_fetch_array() expects parameter 1 to be resource.
Is it something wrong with my database?
And here is my connection which Irequire in the html document. Can it be something here which makes the query not working?
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// echo "Connected successfully";
You never select a database, as far as I can tell, so $result is not a resultset (because the query never executes properly). It is probably false instead. You can var_dump it to verify that. Try adding the database name in the query. If your database is named db, for example:
SELECT * FROM `db`.`items`;
If you don't want to explicitly specify the database name every time, you'll just want to add the database name to the MySQLi connection string, as shown:
http://php.net/manual/en/mysqli.query.php
In the while loop, mysql_fetch_array is incorrect. It should be
while($prommes = mysqli_fetch_array($result))
Additonally your connection needs to be
$conn = mysqli_connect($servername, $username, $password, $database);
where $database is the name of your database (not the hostname).
As an aside, this can be better avoided using an object orientated approach. So for example, your connection could be;
$conn = new mysqli($servername, $username, $password, $database);
and your query and loop could be;
$result = $conn->query($sql);
while($prommes = $result->fetch_array())
Hope this helps.

Php script update VIEW from other data base

I need to write a script which will take the values from two columns and use them to update the column in a view that I created in another database. In the first database I have sku and qty as well as in the view.
here is my code:
$server = 'localhost';
$user = 'invodata';
$pass = 'Abcd1234!1';
$dbname = 'tboinvodata';
$con = mysql_connect($server, $user, $pass) or die("Can't connect");
mysql_select_db("tboinvodata") or die(mysql_error());
$result = mysql_query("SELECT item, onhand FROM immaster"); <- this is getting the values from the columns in the first data base
$server = 'localhost';<-setting up my second connection to other database
$user = 'tbo';
$pass = 'Abcd1234!1';
$dbname = 'i187358_mage1';
$con = mysql_connect($server, $user, $pass) or die("Can't connect");
mysql_select_db("i187358_mage1") or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {<-this gets the array from other database
UPDATE qtyview SET qty = $row["onhand"] WHERE sku = item;<- this should update the necessary columns "sku" is used in my view and "item" is used in the first data base I use this so the proper rows in the other columns get updated.
}
?>
really not sure what I am doing wrong I am pretty new to this though.
You can make multiple calls to mysql_connect() and use them like this.
First connect to two your MYSQL USER
$con1 = mysql_connect($server, $user, $pass);
$con2 = mysql_connect($server, $user, $pass, true);
Then establish a connect with different DATABASE
mysql_select_db('firstdatabase', $con1);
mysql_select_db('seconddatabase', $con2);
Then query from firstdatabase like this
mysql_query('select * from views', $con1);
And query from seconddatabase
mysql_query('select * from views', $con2);
This code is not tested by me...but i think it will work good for you.. :)

Connecting several MySQL databases with 1 shared user

I am trying to integrate a wiki, forum, chat and user database all on 1 site. To connect to the database we use:
<?php
$host="localhost.sitename.com";
$user="user_name";
$password="password";
$database="site_database";
$connection = mysql_connect($host,$user,$password)
or Die ("Could not connect to Server.");
$db=mysql_select_db($database,$connection)
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("site_database");
?>
I need it to also connect to the 3 other databases and am wondering if I need to list each one with a separate $database and again mysql_select_db line or all in one with a , in between?
$database="site_database";
$database="chat_database";
$database="wiki_database";
$database="forum_database";
and
mysql_select_db ("site_database");
mysql_select_db ("chat_database");
mysql_select_db ("wiki_database");
mysql_select_db ("forum_database");
OR
$database="site_database","chat_database","wiki_database","forum_database";
and
mysql_select_db ("site_database","chat_database","wiki_database","forum_database");
???
You can only have one database selected per connection. If you want to have 4 databases open and selected at the same time, even if your using the same login credentials you will need to have 4 open connections. Otherwise you will need to select the appropriate database before each MySQL query if the last MySQL query on that connection had a different database selected.
First: You should use mysqli
Second: Try like this:
$Con1 = new mysqli ("host","user","password","database");
$Con2 = new mysqli ("host","user","password","database");
$Con3 = new mysqli ("host","user","password","database");
Then :
$PreparedStatement1 = $Con1->prepare();
$PreparedStatement1->bindparam('',);
$PreparedStatement1->execute();
$PreparedStatemet1->close();
$PreparedStatement2 = $Con2->prepare();
$PreparedStatement2->bindparam('',);
$PreparedStatement2->execute();
$PreparedStatemet2->close();
What you can do is create a function which connects you to the database
function connect($host, $username, $password, $database) {
$db = mysql_connect($host, $username, $password);
mysql_select_db($database, $db);
return $db;
}
This is theorically what you want to do, but here are some tips:
Avoid using mysql extension (you SHOULD use mysqli or PDO)
Try not to switch between databases with a connection for further database compatibility (although MySQL supports database switching, not every DBMS will behave the same)
Also, if you had to perform a couple of operation you could do:
mysql_select_db('first', $db);
// Perform some queries
mysql_select_db('second', $db);
// Perform some other queries
mysql_select_db('first', $db);
// And then switch back to the first database

connect oracle database with php and execute sql?

Hey guys im trying to connect to an oracle database with php. I tried it like i do it with mysql.
How to do it like this:
$host="localhost";
$user="username";
$pass="password";
$db="database";
$link = mysql_connect($host, $user, $pass) or die ("Keine Verbindung zu der Datenbank moeglich.");
mysql_select_db($db, $link);
$sql = "SQL query goes here";
$result = mysql_query($sql);
How can i do exact this with an oracle database. I have the following connection details
sid, ip, port, username, password.
Simple script:
$DB = '//1.2.3.4:1521/XE';
$DB_USER = 'user';
$DB_PASS = 'pass';
$DB_CHAR = 'AL32UTF8';
$conn = oci_connect($DB_USER, $DB_PASS, $DB, $DB_CHAR);
$statement = oci_parse($conn, 'select 1 from dual');
oci_execute($statement);
$row = oci_fetch_array($statement, OCI_ASSOC+OCI_RETURN_NULLS);
To connect with an Oracle database, you don't use the mysql extension (since that is for MySQL). You should use PDO, with the OCI/Oracle adapter.
You'll want to use PDO to connect to Oracle here is the PHP manual page on creating a connection using PDO, the example given is for MySQL but it will work fine with Oracle. You will need to ensure the PDO:Oracle extension is installed and running on your PHP configuration.

Categories