php odbc close connection is not working properly - php

The connection cannot close after i open it. Anyone knows how to solve my problem?
I have search a lot of possible answer but no one solve it.
This is my code
<?php
include 'db.php';
$sql = "SELECT * FROM Table";
$res = odbc_exec($conn, $sql);
while(odbc_fetch_array($res)){
# .....
}
odbc_close($conn);
if (odbc_close($conn)) {
echo "Connection closed";
}else{
echo "Connection was not close";
}
?>
It says The connection was not close.

You can not put odbc_close($conn) in if condition. It never returns anything. its return type is void so if condition will never return true for this.
for more reference please read this.
PHP.net odbc
you can use is_resource($con). it gives false if the connection is closed.

Related

Returning just data from mysqli query

I am trying to receive data back from a mysql db but in my results I receive escape characters. How can I get rid of these and just return raw data (I'm a noob in this realm of coding so please keep that in mind)?
Here is a sample of my result (it's a json signature):
{"signature":"\n\t\t\t[{\"lx\":16,\"ly\":16,\"mx\":16,\"my\":15},{\"lx\":16,\"ly\":17,\"mx\":16,\"my\":16},{\"lx\":16,\"ly\":18,\"mx\":16,\"my\":17},{\"lx\":16,\"ly\":20,\"mx\":16,\"my\":18},{\"lx\":16,\"ly\":21,\"mx\":16,\"my\":20},{\"lx\":16,\"ly\":22,\"mx\":16,\"my\":21},{\"lx\":16,\"ly\":25,\"mx\":16,\"my\":22},{\"lx\":16,\"ly\":26,\"mx\":16,\"my\":25},.....]\n\t\t\t"}
I would like to remove the: {"signature":"\n\t\t\t on the beginning and \n\t\t\t"} on the end automatically.
Any help is greatly appreciated!
<?php
{
// Connect to MySQL
$mysqli = new mysqli( 'localhost', 'root', 'rootpassword', 'crs' );
//Check our connection_aborted
if ($mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_error );
}
//Read the signature
$sql = "SELECT signature FROM rescue_forms WHERE id=53";
$idresult = $mysqli->query($sql);
while($row = mysqli_fetch_assoc($idresults))
$id = $row;
//Just printing this so I can see the results.
print json_encode($id);
//sending this to a statement below.
$json = json_encode($id);
//Close connection
$mysqli->close();
}
?>
You are storing json in a database, which you shouldn't,
and for some reason you are encoding your json again, which is pointless.
In a round about way of down voting my question and basically telling me I'm an idiot, it helped! (which is the point of the forum) In hopes that this might help someone else that might just be starting out coding this kind of thing. I was able to change my array around to get the raw data as I was expecting, here's my example:
<?php
{
// Connect to MySQL
$mysqli = new mysqli( 'localhost', 'root', 'rootpassword', 'crs' );
//Check our connection_aborted
if ($mysqli->connect_error ) {
die( 'Connect Error: ' . $mysqli->connect_error );
}
//Read the signature
$sql = "SELECT signature FROM rescue_forms WHERE id=53";
$idresult = $mysqli->query($sql);
while($sig=mysqli_fetch_assoc($idresult)){
echo "<tr>";
echo "<td>".$sig['signature']."</td>";
echo "</tr>";
}
//Close connection
$mysqli->close();
}
?>
Thank you to all that responded it was greatly appreciated! Also thank you #YourCommonSense for showing me the proper way to format my code in this window with your edit.

Sqlite3 PHP "database disk image is malformed"

I have a sqlite db file which is definitely not corrupt since I can open it with SQLiteStudio.
However, when I try to open it dynamically with PHP with the following code I found in some tutorial:
class MyDB extends SQLite3
{
function __construct()
{
$this->open('../testDB');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
$sql ="SELECT * from testTable";
$ret = $db->query($sql);
while($row = $ret->fetchArray(SQLITE3_ASSOC) )
{
echo "ID = ". $row['id'] . "\n";
echo "NAME = ". $row['name'] ."\n\n";
}
echo "Operation done successfully\n";
$db->close();
I get the following result:
Opened database successfully
Warning: SQLite3::query() [sqlite3.query]: Unable to prepare statement: 11, database disk image is malformed in test.php on line 52
Fatal error: Call to a member function fetchArray() on a non-object in test.php on line 53
I found some threads like that one, but none of them had a definite answer.
Can somebody help me out here? Thanks in advance!
I had this same "database disk image is malformed" error and i solved it using SQLiteStudio.
Since you already have it, open the file, right click on the database and try the Vacuum option. After doing this, try the integrity check. If the result is 'OK' then your problem is solved. At least that's how i solved it. Vacuum rebuilds the entire database.
I've already the same problem with small SQLITE db.
Try to use:
$sql = "VACUUM";
$db->query($sql);

PHP.ini causes Warning: mysqli_error() expects parameter 1 to be mysqli, null given in/sys/index.php on line 19 [duplicate]

This question already has an answer here:
Should we ever check for mysqli_connect() errors manually?
(1 answer)
Closed 2 years ago.
this script works fine on Local Host and Other free Hosting but not on "host Gator "it should be " PHP.ini " Any Help !?!?
function.php
<?php
date_default_timezone_set("America/Chicago");
echo date("Y/m/d H:i:s");
function consql($con){
$con=mysqli_connect("host","BDusername","DBPassWord","DB") or die( mysqli_error($con));}
?>
index.php
line #1 "Very top "
<?php
SESSION_START();
require_once"inc/function.php";
consql($con);
?>
line#12
<?php
if(isset($_SESSION['user']))
{
header("location:users.php");
}
elseif(isset($_POST['submit']))
{
$result=mysqli_query("select user_id,password from user_login where name='$_POST[username]'") or die(mysqli_error($con)); // ** line # 19 ** //
$n=mysqli_fetch_assoc($result);
if($n['password']!=$_POST['userpassword'])
{
echo "user name or password wrong";
}
else
{
echo "you loged in as $_POST[username]";
$_SESSION['user']=$n['user_id'];
header("location:users.php");
}
}
else
I'm new to mysqli and looked up here for same error but didn't get the point on most of them.
You can use
function consql(&$con){
$con=mysqli_connect("host","BDusername","DBPassWord","DB") or die( mysqli_error($con));
}
So this is call by reference and $con can use at any page once you call the function
Or use global $con; before the connection line
function.php
function consql()
{
$con=mysqli_connect("host","BDusername","DBPassWord","DB") or die( mysqli_error($con));
return $con;
}
index.php
require_once"inc/function.php";
$con = consql();
explanation :
why no error appears in your localhost‌? is it a PHP.ini problem ?
maybe errors are hidden on your localhost or mysqli_query runs with no problem there.
but there is a problem on your host, such as connection problem, missing or broken database table or etc...
base on your code, when no error occurs, mysqli_error() wont run and you dont get error

Querying a database with oop

I have connected to a database for the first time with oop and stright away come up with an issue, below is my code which i'm struggling with:
$q = 'SELECT * FROM test';
$sqli->query($q);
if($sqli->query($q)){
echo "worked";
}
if($sqli->error){
echo $sqli->error;
}
I have checked for errors when connecting to the db and that works fine, but when I run this query I get no output, why? I expected an error or "worked", but have got neither.
Whats happening?
I have put some comments in the source code to help:
$q = 'SELECT * FROM test';
//$sqli is the result of a
//new mysqli("localhost", "user", "password", "database");
$resource = $sqli->query($q); // this returns a resource or false
if(!$resource) {
echo $sqli->error;
die; // do not process further
}
// process the results
$rows = $resource->fetch_all();
if ($rows) { // check if there are rows
echo "worked";
}
else {
echo "query is ok, but there are no rows";
}
You could also use $resource->fetch_object() which returns an object for output. Therefore if you wanted to print specific data from the result set, you would do something like
//table test.Name and test.Country
while ($rowobj = $resource->fetch_object()){
printf ("%s (%s)\n", $rowobj->Name, $rowobj->Country);
}
Good luck,
You could use this method, I hope it's what you are looking for. You will need to define the DB first. Then you can connect in OOP and test the connection is true or exit();
Let me know if this works for you. You can also define the DB in an external file and just do an include(); towards the top of your script for any pages needing connection to the DB.
define("SERVER","IP Address");
define("USER","DB USERNAME");
define("PASSWORD","DB PASSWORD");
define("DATABASE","DB NAME");
// This is for connection
$mysqli = new mysqli(SERVER, USER, PASSWORD, DATABASE);
if ($mysqli->connect_errno) {
echo "Connection to MySQL failed: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
exit();
}

Why is my PDO not working?

I am starting to use PDO and I successfully connected to MySQL using PDO. However, when I try to SELECT stuff from my DB, nothing happens. Nothing is echoed. (even though I have records in that table, and the column username exists) No error in my PHP log.
I am using MAMP and all PDO components seem to be working in phpinfo() (since I was able to connect to db in the first place)
Please let me know what could have gone wrong. Thanks a lot
<?php
try
{
$connection = new PDO('mysql:host=localhost;dbname:my_db','my_username',
'xxxxxxx');
$stmt=$connection->prepare("SELECT * FROM users");
$stmt->execute();
while ($row=$stmt->fetch(PDO::FETCH_OBJ)){
echo $row->username;
}
}
catch(Exception $e)
{
echo "There was an error connecting to the database";
}
?>
You need to tell PDO that you want it to throw exceptions:
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Following your comment below, it is apparent that your DSN is incorrect. It should be:
$connection = new PDO('mysql:host=localhost;dbname=my_db','my_username','xxxxxxx');
Note that the syntax is dbname= rather than dbname: (which you had originally).
First, get your query out of your PDO connection segment...
<?php
try
{
$connection = new PDO('mysql:host=localhost;dbname:my_db','my_username',
'xxxxxxx');
}
catch(Exception $e)
{
echo "There was an error connecting to the database";
}
?>
Then, do it.
<?php
$SQL = 'SELECT * FROM users';
foreach($connection->query($SQL) as $row){
print $row['username'] . "\n".'<br />';
}
?>
Why not ask PHP?
catch(Exception $e)
{
die($e);
}
Looks like your either don't have data in that table or have an error:
Try to add this code after $stmt->execute();:
$arr = $sth->errorInfo();
print_r($arr);

Categories