is it possible select Database 2 simultaneously in the same conection - php

I have problem with 2 select Databases from the same connection simultaneously, the code is:
#$dbmssSQLGestasa_conn = mssql_connect($servidor, $usuario, $contra);
if(!$dbmssSQLGestasa_conn){
header("Location:nobase.php");
exit();
}
mssql_select_db('GESTASA', $dbmssSQLGestasa_conn);
the code of the other connection:
#$dbmssSQLTasa_conn = mssql_connect($servidor, $usuario, $contra);
if(!$dbmssSQLTasa_conn){
header("Location:nobase.php");
exit();
}
//Apertura de la base de datos
mssql_select_db('TASA', $dbmssSQLTasa_conn);
it dont work and give me error "(severity 16)".
is It possible do 2 or more selections databases en la same connection mssql?

Dont use mysql_select_db();
In Mysqli :
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM test123.posts"; // databse.tablename
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$mack[] = $row;
}
}
$sql1 = "SELECT * FROM portal.abouts"; // databse.tablename
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
$mack1[] = $row1;
}
}
echo "<pre>";
print_r($mack);
print_r($mack1);
echo "</pre>";
$conn->close();

You can easily use 2 databases in same time with Below Codes
I'm Using variables as Capital word But you can use your own variable words.
<?php
define('HOST', "YOURHOSTNAME");
define('USER', "YOURHOSTNAME");
define('PASS', "YOURHOSTNAME");
define('DATABASE1', "NAMEOFDATABASE1");
define('DATABASE2', "NAMEOFDATABASE2");
$DATABASE1 = mysqli_connect(HOST, USER, PASS, DATABASE1);
$DATABASE2 = mysqli_connect(HOST, USER, PASS, DATABASE2);
if(!$DATABASE1){
die("DATABASE1 CONNECTION ERROR: ".mysqli_connect_error());
}
if(!$DATABASE2){
die("DATABASE2 CONNECTION ERROR: ".mysqli_connect_error());
}
$sql = "SELECT * FROM TABLE"; /* You can use your own query */
$DATABASE1_QUERY = mysqli_query($DATABASE1, $sql);
$DATABASE2_QUERY = mysqli_query($DATABASE2, $sql);
$DATABASE1_RESULT = mysqli_fetch_assoc($DATABASE1_QUERY);
$DATABASE2_RESULT = mysqli_fetch_assoc($DATABASE2_QUERY);
/* SHOW YOUR RESULT HERE WHICH DATABASE YOU WANT FROM */
echo $DATABASE1_RESULT['id'];
echo $DATABASE2_RESULT['id'];
/*After complete your all work don't forgot about close database connections*/
mysqli_close($DATABASE1);
mysqli_close($DATABASE2);
?>

Related

Access database remotely to fetch data

I need to access a database remotely using PHP and display the fetched data in html. My code is as below
$servername = "192.168.56.1:3306";
$username = "root";
$password = "";
$dbname = "grh";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
error_log("Failed to connect to database!", 0);
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM ip_patient_logs";
$result = $conn->query($sql);
$rows = array();
if ($result->num_rows > 0) {
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
} else {
echo "0 results";
}
$conn->close();
I expected to be able to connect to the database and fetch the data , but it displays blank page. I am a beginner in PHP.

Why won't this php SELECT FROM query work?

Here is my simple php code:
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "********"; //hiding my password
$dbname = "course";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name FROM tutors";
$result = $conn->query($sql);
if( $result === true ) {
echo "good";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
while($row = $result->fetch_assoc()) {
echo $row["name"];
}
?>
</body>
</html>
In my database called "course", I have a table called "tutors" which has a column called "name". I have two entries in that table with the names "deep thought" and "pyrenees" respectively.
When this code runs however, the only thing that prints out is:
Error: SELECT name FROM tutors
It is supposed to simply print out the two names that I mentioned before.
Does anyone know why this happens? I know for a fact that I have the two entries in my table!
I think the word "name" is a MySQL reserved word. Wrap your query variables in a tilde backticks like this:
$sql = "SELECT `name` FROM `tutors`";
This helps to escape those values from MySQL thinking you're trying to referencing a built in variable.
Why not use mysqli like so:
function getFollowers($link, $userid)
{
$sql = "SELECT users.id, username, profileImg FROM following INNER JOIN users ON users.id = following.userid WHERE followid = " . $userid;
$result = mysqli_query($link,$sql);
$resultsArray = [];
while($row = mysqli_fetch_assoc($result)) {
$resultsArray[] = $row;
}
mysqli_free_result($result);
return $resultsArray;
}
This is just a clean example, I am sure you get the idea.
Here is what $link is
function connection()
{
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'databaseTable');
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
return $link;
}
Or without the methods:
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'databaseTable');
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT name FROM tutors";
$result = mysqli_query($link,$sql);
$resultsArray = [];
while($row = mysqli_fetch_assoc($result)) {
echo $row["name"];
}
mysqli_free_result($result);
To check if the query was successful you can do this:
if (mysqli_num_rows($result) > 0)
{
//has rows, so whatever you want with them.
}
You put the condition after defining $result.

Trying to echo out every name in database table inside hrml list

I have been trying to echo out database data through a while loop now for awhile but it doesn't work, I can't find where the issue is. I have tried echoing out data manually and that works just fine.
<?php $results = mysqli_query($con,"SELECT * FROM guestbook ORDER BY id DESC"); ?>
<?php while ($row = mysqli_fetch_assoc($results)) : ?>
<li>
<?php echo $row['message']; ?>
</li>
<?php endwhile ?>
First of all make sure you are connected to the database. I don't know if you omitted that on purpose or not. Also, check if the connection is established.
<?php
//Insert your server info here
$servername = "servername";
$username = "root";
$password = "root";
$dbname = "test_database";
// Create and check your connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM guestbook ORDER BY id DESC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<li>' . row["message"] . '</li>';
}
} else {
// Your query produced 0 results
echo "Empty result!";
}
// Remember to close the connection
mysqli_close($conn);
?>

PHP and mysql connection not working

I have written following code to connect mysql in php but I am not getting output.
<?php
$servername = "localhost";
$username = "root";
$password = "pravin";
$mysql_conn = new mysqli($servername, $username, $password);
if ($mysql_conn->connect_error) {
die("Connection failed: ". $mysql_conn->connect_error);
}
echo "Connected successfully";
$name = $_POST["microorganism"];
echo $name;
$db_selected = mysql_select_db('yieldofvanillin', $mysql_conn);
if (!$db_selected){
die ('Can\'t use : ' . mysql_error());
}
$query = "SELECT * FROM vanillin WHERE Microorganism = '$name' ";
$result = $mysql_query($query);
while ($line = myql_fetch_array($result, MYSQL_ASSOC)) {
echo $line["Substrate"];
echo $line["products"];
echo $line["Microorganism"];
echo $line["yield"];
echo $line["Reference"];
}
mysql_close($mysql_conn);
?>
The database name is "yieldofvanillin" and it has five column. I an getting output Connected successfully. After that no output. Please let me know the bug in code.
i have remove errors. which i mention in comments. Code Reference PHP Manual. you should read this manual (strongly recommended)
<?php
$mysqli = new mysqli("localhost", "root", "pravin", "yieldofvanillin");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT * FROM vanillin WHERE Microorganism = '$name' ";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
echo $row["Substrate"];
echo $row["products"];
echo $row["Microorganism"];
echo $row["yield"];
echo $row["Reference"];
}
/* free result set */
$result->free();
}
you're mixing mysqli and mysql libraries.
the code should be:
<?php
$servername = "localhost";
$username = "root";
$password = "pravin";
$mysql_conn = new mysqli($servername, $username, $password);
if (mysqli_connect_errno()) {
die("Connection failed: ". mysqli_connect_error());
}
echo "Connected successfully";
$name = $_POST["microorganism"];
echo $name;
$db_selected = mysqli_select_db($mysql_conn,'yieldofvanillin');
if (!$db_selected){
die ('Can\'t use : ' . mysqli_error($mysql_conn));
}
$query = "SELECT * FROM vanillin WHERE Microorganism = '$name' ";
$result = mysqli_query( $mysql_conn,$query);
while ($line = mysqli_fetch_assoc($result)) {
echo $line["Substrate"];
echo $line["products"];
echo $line["Microorganism"];
echo $line["yield"];
echo $line["Reference"];
}
mysqli_close($mysql_conn);
?>
Remove error in your code.Read carefully php manual.
<?php
$servername = "localhost";
$username = "root";
$password = "pravin";
$db = "yieldofvanillin";
// Create connection
$mysqli = new mysqli($servername, $username, $password, $db);
/* connection string*/
if ($mysqli->connect_errno) {
die("Connection failed: " . $mysqli->connect_error);
exit();
}
$query = "SELECT * FROM vanillin WHERE Microorganism = '$name' ";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
echo $row["Substrate"];
}
$result->free();
}
$mysqli->close();
?>
Your output not showing because mysql_fetch_array is not correct.Because you are mixing mysql_ and mysqli_ functions and you called myql_fetch_array that doesn't exist in mysqli. MySQL and MySQLi are two different PHP extensions and they cannot be mixed. Because the former is deprecated in mysqli

Connecting MYSQL using PHP

I can't run my codes.
It says:
syntax error, unexpected '$query' (T_VARIABLE).
Code
<?php
$hostname="localhost";
$username="";
$password="";
$dbname="thesis";
$usertable="product";
$yourfield="product_id";
msql_connect($hostname,$username,$password) or die ("<html><script>
language='Javascript'>alert('Unable to connect to database!.'),history.go(-1)</script></html>")
$query = "SELECT * FROM $usertable";
$result = mysql_query($query);
if($result)
{
while ($row = mysql_fetch_array($result))
{
$name = $row["$yourfield"];
echo "Name: ".$name."</br>";
}
}
?>
msql_connect($hostname,$username,$password) or die ("<html><script>
language='Javascript'>alert('Unable to connect to database!.'),history.go(-1)</script></html>")
should have a semicolon.
Replace it with this:
msql_connect($hostname,$username,$password) or die ("<html><script>
language='Javascript'>alert('Unable to connect to database!.'),history.go(-1)</script></html>");
<?php
$hostname = "localhost";
$username = "";
$password = "";
$dbname = "thesis";
$usertable = "product";
$yourfield = "product_id";
$mysqli = new mysqli($hostname, $username, $password, $dbname);
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT * FROM $usertable")) {
while ($row = mysql_fetch_array($result))
{
$name = $row["$yourfield"];
echo "Name: ".$name."</br>";
}
/* free result set */
$result->close();
}
$mysqli->close();
http://php.net/manual/en/mysqli.query.php

Categories