I have a simple code for selecting data using MySQl and PHP.
Here it is:
<?php
require_once 'login.php' ;
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die ("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database)
or die("Unable to choose DB: " . mysql_error());
$query = "SELECT * FROM bank";
$result = mysql_query($query);
if (!$result) die ("Failed with connection to DB: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j=0; $j < $rows; ++$j) {
$row = mysql_fetch_row($result);
echo 'MFO: ' . $row[0] . '<br/>';
echo 'NAME: ' . $row[1] . '<br/>';
echo 'STATE: ' . $row[2] . '<br/><br/>';
}
?>
But i have to use ODBC driver instead of standard integrated driver.
I've read a lot of information but haven't find anything , so maybe someone help what should i add or rewrite in this code for using ODBC ?
There are a lot of odbc functions in PHP.
http://php.net/manual/en/function.odbc-connect.php
You can connect with this functions to mysql and other databases. In the commments on php.net you can find some examples how to connect to MySQL.
http://www.mysql.de/products/connector/
Here you can find an ODBC driver for MySQL.
Related
I am connecting PHP to Pervasive SQL and the connection keep resetting
This connection is on Windows Server 2012 using PHP7 on Apache 2.4. I have already created a DNS connection and the test can connect to the database successfully.
<?php
$conn=odbc_connect("brps","","");
if(!$conn) die("Could not connect");
?>
The following code works for me in an x64 environment using PHP 7.31 with the ODBC extension enabled in the PHP.INI. I'm also using the v11.30 x64 Client connecting to a remote PSQL v11 server.
<?php
$conn=odbc_connect("brpp","","");
if(!$conn) die("Could not connect");
$result = odbc_tables($conn);
echo '<div id="top">..</div><table border="1" cellpadding="5"><tr>';
$tblRow = 1;
while (odbc_fetch_row($result)){
if(odbc_result($result,"TABLE_TYPE")=="TABLE"){
$tableName = odbc_result($result,"TABLE_NAME");
echo '<tr><td>' . $tblRow . '</td><td>' . $tableName . '</td></tr>';
$tblRow++;
}
}
echo '</table><hr>';
$result = odbc_tables($conn);
while (odbc_fetch_row($result)){
if(odbc_result($result,"TABLE_TYPE")=="TABLE"){
$tableName = odbc_result($result,"TABLE_NAME");
echo '<div id="' . $tableName . '"> *** ' . $tableName . ' *** top</div>';
$cols = odbc_exec($conn, "SELECT * FROM $tableName WHERE 1=2");
$ncols = odbc_num_fields($cols);
for ($n=1; $n<=$ncols; $n++) {
$field_name = odbc_field_name($cols, $n);
echo $field_name . "<br>";
}
echo '<hr>';
}
}
?>
If the DSN doesn't exist, I get an error:
Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in C:\Wnmp\html\phpodbc.php on line 2
Could not connect
I was getting a System error 998 after installing the PSQL x64 client. A reboot of the machine fixed that error.
I'm trying to connect my PHP webpage with my MS SQL database. I've got this code from the internet, but have tried others. All seems to come back with a problem with "mssql_connect".
I have tried (I think) everything and can not find out why it won't work.
My code is:
<?php
$myServer = 'SQL5008.Smarterasp.net,1433';
$myUser = '*****';
$myPass = '*****';
$myDB = '*****';
//connection to the database
$dbhandle = mssql_connect($myServer, $myuser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT id ";
$query .= "FROM tblEmployees ";
$query .= "WHERE CompanyID=3";
//execute the SQL query and return records
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
//display the results
while($row = mssql_fetch_array($result))
{
echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
Why dont you try using PDO. I use it with SQL SERVER daily. You will need the php sqlsrv extenstion though.
// instantiate the pdo object
try {
$Server = "localhost";
$User = "username";
$Pass = "password";
$Database = "mydb";
$this->conn = new PDO("sqlsrv:Server=$Server;Database=$Database", $User, $Pass);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);//allow for SQL query errors
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
Check using phpinfo() if mssql extension is loaded. According to PHP Manual:
This extension is not available anymore on Windows with PHP 5.3 or later.
If you find out mssql is unloaded, try to connect using sqlsrv extension. Here you can find a few examples http://php.net/manual/ru/function.sqlsrv-connect.php
Anyway it's a good idea to post here an error message you get from PHP.
I am having trouble connecting to my localhost database with php. It feels like I have followed every tutorial there is.
current php code:
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="webutvshop";
$username="dbconnect";
$password="password";
//DO NOT EDIT BELOW THIS LINE
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysql_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysql_select_db($database, $link);
if (!$db_selected) {
die ('Can\'t select database: ' . mysql_error());
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysql_close($link);
?>
The issue stands, when I acess the file locally on my computer I do not get any answer at all from it. I've tried with many other, yet I do not get any answers from them!
I need help in order to keep working on my schoolproject, thanks.
Stop using mysql_*, because they are deprecated officially. Use mysqli_* or PDO for this purpose. An example:-
<?php
//ENTER YOUR DATABASE CONNECTION INFO BELOW:
$hostname="localhost";
$database="stackquestion";
$username="root";
$password=""; // check once with empty password and once with some password that you tried already
//DO NOT EDIT BELOW THIS LINE
$link = mysqli_connect($hostname, $username, $password);
if (!$link) {
die('Connection failed: ' . mysqli_connect_error());
}
else{
echo "Connection to MySQL server " .$hostname . " successful!
" . PHP_EOL;
}
$db_selected = mysqli_select_db($link,$database);
if (!$db_selected) {
die ('Can\'t select database: ' . mysqli_error($link));
}
else {
echo 'Database ' . $database . ' successfully selected!';
}
mysqli_close($link);
?>
Output:- http://prntscr.com/7cbr5j
Can you also verify you can connect to the database from a command line:
mysql -u dbconnect -ppassword -h localhost webutvshop
I did an express install for MS SQL Server 2008 and created a database with a test table. I am running Windows Server 2008 and have IIS, PHP and MS SQL installed. I chose to go with SQL server authentication, where you would need credentials to modify or view the database. I try to print out a simple test table on a web page, but it keeps failing and only returns a white screen..
<?php
$server = "WS1\SQLEXPRESS";
$username = "sa";
$password = "password";
$db = "testdb1";
$dbhandle = mssql_connect($server, $username, $password)
or die ("Cannot connect to SQL Server on $server");
$selected = mssql_select_db($db, $dbhandle)
or die ("Could not open database $db")
echo "You are connected to the " . $db . "database on the " . $server . ".";
$query = "SELECT * FROM table1";
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";
echo "<li>" . $row[""] . $row[""] . "</li>";
while ($row = mssql_fetch_array($result)) {
print_r($row);
}
mssql_close($dbhandle);
?>
Ah, the horrific white screen. Sometimes very hard to troubleshoot. Often times it's a simple missing semicolon ...
like the one here:
$selected = mssql_select_db($db, $dbhandle)
or die ("Could not open database $db")
Put a semicolon on the end of that statement and you are back in business.
Hey i am using Mamp on imac and my problem is that when i hit the submit button (on a post form) to enter the data then nothing shows up and the database remains empty.
Here is my code :
<?php
define('DB_NAME', 'demob');
define('DB_USER','brom');
define('DB_PASSWORD','****');
Define('DB_HOST','localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect : ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected){
die('cant use' . DB.NAME . ' : ' .mysql_error());
}
$value = $_POST['input1'];
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_close();
?>
You are not executing a query.
$sql = "INSER INTO memberss ('input1') VALUES ('$value')";
mysql_query($sql);
You should know that, the method you are using to connect to mysql is deprecated now. please read up about PDO or mysqli