PHP Can't Run Query on Microsoft SQL Server - php

I'm not sure what's wrong with the following code. I'm trying to connect to MSSQL with PHP to run a query. The code can connect to the server and database, but fails on query execution. This code is virtually identical to the way I retrieve data from MySQL. What is the mistake I'm making here?
Thanks.
$link = mssql_connect ($server, $user, $password)
or die ( "<h1>ERROR: Can't connect to MSSQL.</h1>");
$db_selected = mssql_select_db ($db, $link)
or die ("<h1>ERROR: Can't read MSSQL database.</h1>");
$sql = 'SELECT * FROM [uStore].[dbo].[Store]';
$result = mssql_query ($sql)
or die ("<h1>ERROR: Can't execute MSSQL query.</h1>");
echo 'ok';
mssql_free_result ($link);

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

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

oci_execute dies and return blank oci_error, Why is oci_error null?

I am trying to make a connection to a remote oracle database for the first time from PHP. However when I try from php I can make a connection but my php dies when using execute query.
Why does oci_error not return anything?
Code:
$conn = oci_connect('dbname', 'password', '//xx.xxx.xxx.xxx:1521/orcl');
if(!$conn){
echo 'DB CON FAILURE';
exit(0);
} else {
echo 'DB CON SUCCESS';
}
$query = "SELECT * FROM user_tables;";
$stid = oci_parse($conn, $query);
echo $stid;
oci_execute($stid) or die("Could not execute query: '" . oci_error() . "'");
Return:
DB CON SUCCESS
Query: SELECT * FROM user_tables;
Resource id #4
Could not execute query: ''
My php.ini file shows oci8 is installed and I can connect and execute the above query using sqlplus from the webserver.
Get rid of the trailing semicolon in your query.

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.

what is wrong with this mysql code

$db_user="root";
$db_host="localhost";
$db_password="root";
$db_name = "fayer";
$conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn't connect to server");
// perform query
$query = 'SELECT * FROM posts';
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
// use returned data
while($row = mysqli_fetch_assoc($result))
{
echo $row['title'];
}
I get in the browser: "mysql problem".
Help!
UPDATE
I have echoed the query. It shows SELECT * FROM posts and when I query manually it gets the rows.
I think it has something to do with mysqli. I think i should use mysql. Do u think I have incompatibility problems with mysqli?
i have echoed it. it shows SELECT * FROM posts. and when i query manually it gets the rows.
i think it has something to do with mysqli. i think i should use mysql. do u think i have incompatibility problems with mysqli?
You have empty WHERE clause. Remove it or add a search condition.
Change
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
to
$result = mysqli_query($conn, $query) or die ("Couldn't execute query because: " . mysqli_error());
and you will know why the query is failing. Rule of thumb: Whenever you have a failed query, print it out and run it through phpmyadmin or some other raw-query executor and you will discover very quickly what the problem is.

Categories