PHP ODBC - connection not working properly - php

I wrote this code for the ODBC connection. But, I'm getting like this error. My DB is an MS Access database. Please, can anyone help me?
<html>
<body>
<?php
$conn=odbc_connect('finance','','');
if (!$conn)
{exit("Connection Failed: " . $conn);}
$sql="SELECT * FROM Customers";
$rs=odbc_exec($conn,$sql);
if (!$rs)
{exit("Error in SQL");}
echo "<table><tr>";
echo "<th>Name</th>";
echo "<th>CustomerID</th></tr>";
while (odbc_fetch_row($rs))
{
$Name=odbc_result($rs,"Name");
$CustomerID=odbc_result($rs,"CustomerID");
echo "<tr><td>$Name</td>";
echo "<td>$CustomerID</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>
</body>
</html>
This is my error.
Warning: odbc_exec(): SQL error: [FINANCE ODBC]Base table:Customers not found., SQL state S0002 in SQLExecDirect in C:\xampp\htdocs\odbc\index.php on line 11
Error in SQL

Related

can't connecting to mysqli php What's wrong there

This is my code:
<?php
$connection = mysqli_connect('localhost','root','','cms');
if($connection){
echo "Error : unable to connect to mysqli.".PHP_EOL;
echo "Debugging errno".mysqli_connect_errno().PHP_EOL;
echo "Debugging error".mysqli_connect_error().PHP_EOL;
exit;
}
?>
And I'm getting the following error:
Error : unable to connect to mysqli. Debugging errno0 Debugging error
Brother your connection is successful , you just need to use -
if(!$connection) instead of if($connection)

remote odbc connect - php and msaacess

im trying to connect php and msaccess
Im using the code below in connecting php and ms access and so far it working fine when i add System DSN to fetch database locally
but when the database is in remote location i receive the message connection failed
<html>
<body>
<?php
$conn=odbc_connect('northwind','','');
if (!$conn)
{exit("Connection Failed: " . $conn);}
$sql="SELECT * FROM customers";
$rs=odbc_exec($conn,$sql);
if (!$rs)
{exit("Error in SQL");}
echo "<table><tr>";
echo "<th>Companyname</th>";
echo "<th>Contactname</th></tr>";
while (odbc_fetch_row($rs))
{
$compname=odbc_result($rs,"CompanyName");
$conname=odbc_result($rs,"ContactName");
echo "<tr><td>$compname</td>";
echo "<td>$conname</td></tr>";
}
odbc_close($conn);
echo "</table>";
?>
</body>
</html>
Just for information im working this is old server with winserver 2003 installed

No database connection with no errors

I have Ubuntu 16.10 x86_64 x86_64. I installed LAMP to program in PHP and to create databases. In my php program I want to connect to my local database for creating a table ( in HTML ) with the data of any row of the table.
The problem is that when I open the php file( localhost/file.php ) through firefox ,the browser doesn't charge anything. If thare were been an error during the connection with the database, It would have printed something in the browser.
Here the code:
<!DOCTYPE html>
<html>
<head><title> SQL & PHP </title></head>
<body>
<?php
$db = mysql_connect("localhost", "root", "password")
or die ("Non riesco a creare la connessione");
mysql_select_db("scuola")
or die ("Non trovo il DB");
$sql = "SELECT id_utente, nome_utente, password_utente, conta_pres FROM utenti WHERE conta_pres <> 0";
$ris = mysql_query($sql) or die ("Query fallita!");
echo "<TABLE><TR><TH>ID utente <TH> Nome utente <TH>Password<TH>Contatore visite</TR>";
while ($riga= mysql_fetch_array($ris))
{
echo ("<TR>");
echo "<TD>" . $riga["id_utente"];
echo "<TD>" . $riga["nome_utente"];
echo "<TD>" . $riga["password_utente"];
echo "<TD>" . $riga["conta_pres"];
}
mysql_close();
?>
</body>
</html>
I checked the syntax (using a website) of the code and thare aren't problems,even because I copied this one by a book. I read that mysql_connect has been deprecated, so I replaced it with new mysqli_connect but the error still remains: white page. I tried to put 2 echo, one before the connecting function and one after that. Only the first echo is printed on the screen. I tried to type in the terminal sudo apt-get install php5-mysql but there is an error:
The "php5-mysql" packet has not run to install
Can someone help me, please?
First of all use mysqli instead of mysql.
I think I have found the problem. When you call mysqli_select_db, it expects 2 parameters and you only specified one. Even though you have set the $db database connection, you need to specify which database you want to select the database name from.
So mysqli_select_db($db, "scuola") should do the trick.
And at the bottom close the connection specifying which connection to close. In your case it is: mysqli_close($db);
<!DOCTYPE html>
<html>
<head><title> SQL & PHP </title></head>
<body>
<?php
$db = mysqli_connect("localhost", "root", "password")
or die ("Non riesco a creare la connessione");
mysqli_select_db($db, "scuola") // see this line
or die ("Non trovo il DB");
$sql = "SELECT id_utente, nome_utente, password_utente, conta_pres FROM utenti WHERE conta_pres <> 0";
$ris = mysql_query($sql) or die ("Query fallita!");
echo "<TABLE><TR><TH>ID utente <TH> Nome utente <TH>Password<TH>Contatore visite</TR>";
while ($riga= mysql_fetch_array($ris))
{
echo ("<TR>");
echo "<TD>" . $riga["id_utente"];
echo "<TD>" . $riga["nome_utente"];
echo "<TD>" . $riga["password_utente"];
echo "<TD>" . $riga["conta_pres"];
}
mysqli_close($db); // see this line
?>
</body>
</html>

PHP and HTML: 000webhost error

Im building my website on 000webhost and with Zyro Builder and now i need some php code to get data from mysql database.
Im having this error: "Parse error: syntax error, unexpected '?' in ....." every time i want to use something like this:
echo "<br/>";
I know that is possible, so why I can't use it? :S
Thank you for your attention.
<?php
// Connect to database server
$con=mysqli_connect("mysql4.000webhost.com","a8373599_gus","******","a8373599_lolgus") or die(mysql_error($con));
if(mysqli_connect_errno($con)) {
echo "Fail to connect: " . mysqli_connect_errno();
} else {
echo "sucess!";
}
$result = mysqli_query($con, "SELECT * FROM Player");
$row = mysqli_fetch_array($result);
echo "<br/>";
echo $row['UserName']." ".$row['Playername'];
// Close the database connection
mysqli_close($con);
?>
Try using the numerical instances,
Eg:
echo $row[0]." ".$row[1];

Using PHP to connect to MySQL: ERROR?

I tried running the following code: (http://localhost/read.php)
<html>
<body>
<?php
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
if (mysql_query("CREATE DATABASE testphp",$link))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
?>
</body>
</html>
and got the following error:
Fatal error: Call to undefined function mysql_connect() in
C:\Program Files (x86)\ApacheSoftware Foundation\Apache2.2\htdocs\read.php
on line 5
Look at you phpinfo(). Most likely mysql extensions is not there.
And while you are at it, you could just drop the ancient mysql_* way ow doing thing and learn how to use PDO and prepared statements. It's an abstraction API for database connection and interaction.
your mysql-extension for php is not loaded! check that in your php.ini.

Categories