I am using sqlite and trying to connect to it through my PHP script.
The select query is working but the update query is not working
Please find below the code snippet
$dir = 'sqlite:/var/www/html/BMKAOAData.db';
$dbh = new PDO($dir) or die("cannot open the database");
$query = 'update KAOAData set comments = :Comments where aptNo = :aptNo';
$stmt= $dbh->prepare($query);
if(!$stmt){
echo "\nPDO::errorInfo():\n";
print_r($dbh->errorInfo());
}
$stmt->bindParam(':Comments',$myComments,PDO::PARAM_STR);
$stmt->bindParam(':aptNo',$myaptNo,PDO::PARAM_STR);
$stmt->execute();
$count = $stmt->rowCount();
print "Updated $count rows\n";
I searched a lot for its solution and tried various methods but couldn't resolve it. Note that I have the database in the same directory of the script and I have given right permission to it and all its parent directory.
I am executing the same query in sqlite prompt and it is working fine.
Please help to resolve this.
Maybe is the database autocommit set to false? In that case
$dbh->commit();
could solve your issue
Related
I'm scratching my head over this and not sure why this isn't working
I am trying to post info from a form to save the entry in the database, for troubleshooting I am doing a field at a time
Here is my code and the link is /example.php?device=test
I have even tried $_GET['device'] = "test101"; and still no luck.
The database name and login is correct as I have another form that works, knowing this I have copied and pasted the code to this file and changed the details for the table to try help solve my problem.
I'm guessing its something simple that I have over looked.
<?php
// include database connection
include 'inc/connect.php';
try{
// insert query
$query = "INSERT INTO repairs SET device=:device";
// prepare query for execution
$stmt = $con->prepare($query);
// bind the parameters
$stmt->bindParam(':device', $_GET['device']);
// Execute the query
if($stmt->execute()){
echo "<div>Record was saved.</div>";
}else{
echo mysql_error();
die('Unable to save record.');
}
}
// show error
catch(PDOException $exception){
die('ERROR: ' . $exception->getMessage());
}
?>
change INSERT INTO to update
if you want to update:-
$query = "update repairs SET device=:device";
or if you want to insert
$query = "INSERT INTO repairs (device) VALUES (:device)";
I am wondering if its possible to run an insert query if the answer is correct, I have tried multiple types of queries from creating a new stmt and array and inserting them into the database if the answer if correct, However with my basic understanding of how it all works, I may be missing something? I am not getting any errors returned, just getting the echo statement returned.
Note : I know I am trying to insert QuestionID multiple times its just to put data into the database. It will be updated with the correct fields.
Can anyone enlighten me on whats the issue :
<?php
// establishing the MySQLi connection
require_once('connection-test.php');
$mysqli = new mysqli($host,$username,$password,$database);
if($mysqli -> connect_error)die($mysqli->connect_error);
$questionID = $_POST['id'];
$userAnswer = $_POST['answer'];
$query = "SELECT answers FROM answers WHERE questionID=?";
$statement = $mysqli->prepare($query);
$statement ->bind_param('i', $questionID);
$statement->execute();
$statement->bind_result($answer);
while($statement->fetch()){
if($answer != $userAnswer){
echo "Wrong";
//return to previous Page
}else{
echo "Correct";
mysqli_query($mysqli,"INSERT INTO submissions (submissionsID,teamID,questionID,answer)
VALUES ($questionID,$questionID,$questionID,$userAnswer)");
}
}
$statement->close();
?>
Turns out I had to free_result()before I could run another mysqli statement
$query = "SELECT `ip` FROM `banned` WHERE `ip` = '$ip'";
$retval = mysqli_query($conn, $query);
if(!$retval){
die("Could not Execute Query: " . mysqli_error($conn));
} else {
if(mysqli_num_rows($retval) == 0){
echo "test";
} else {
header('Location: http://www.teutonic-development.net/index.php?p=banned');
}
}
when I'm running this code all that's printed out is: "Could not Execute Query:"
I really have absolutely no idea why it's doing this. I'm connecting fine in my init.php file. Which is where this file is.
My other script which just adds a log entry works fine. And if I run my $query in phpmyadmin's sql interpreter it runs perfectly fine (when I replace the $ip part with an actual ip of course)
Any suggestions?
Normally one would say that hey your query failed to execute story finish. But this case is interesting.
Your code is
die("Could not Execute Query: " . mysqli_error($conn));
and your error message is
Could not Execute Query:
Notice even though you have mysqli_error($conn) but there is no mysql error being shown. That confirms 100% that $conn is not properly established (contrary to what you think)
So take a look at your code again and see if $conn is really a mysqli resource and is available to your file in proper variable scope.
Hy everyone, I can't wrap my head around this. I'm trying to get some data from a table using PDO. this is my code:
//in db.php I have the connection:
$host = 'localhost';
$db = 'APL';
$dbuser = '';
$pass = ' ';
try{
$conn = new PDO("mysql:host=$host;dbname=$db", $dbuser, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
//in my file I have this:
$id = $_GET['id'];
$sel_sql = "SELECT * FROM users WHERE id =:id";
$stmt = $conn ->prepare($sel_sql);
$stmt -> bindParam(':id', $id);
$stmt -> execute();
$result = $stmt -> fetchAll(PDO::FETCH_ASSOC);
The problem is that print_r($result) returns '1' (just the value 1, therefore I can't access any data stored in the table) as long as $_SESSION['user'] is set.
The whole data-retrieving worked just fine if the $_SESSION['user'] is not set.
Can someone please explain why this is happening? (I'm fairly new to all this and I'm really trying to understand why some issues occur).
Thank you!
The fetchAll function should be returning either an array, or a boolean FALSE.
You report that print_r($result) is displaying an integer value of 1.
I don't see how that's possible, unless you are assigning a different value to $result. Try relocating print_r($result) to immediately follow the assignment from fetchAll.
(My suspicion is that $result is being assigned a value of 1 elsewhere in your code, before you do the print_r. If there were "Issues with php connection to MySQL database", we'd be expecting to see a PDO error of some sort.)
NOTE: I don't think PDO::FETCH_ASSOC is a defined fetch style for the fetchAll function. (fetchAll has different fetch styles than fetch.)
Just in case someone else stumbles upon this, between the $result variable and the print_r($result) I had an include_once(); statement (which was wrongly put there in the first place).
Thank you everyone for your answers.
This is pretty similar to this question, but I need to capture output. It seems to be basically an issue with getting the correct syntax, but using the syntax suggested in the answer didn't work for me.
I have a php application that calls a stored procedure on a SQL Server database which I developed on my Windows laptop running xampp.
My stored procedure looks like this:
CREATE Procedure [dbo].[check_for_barcode] #Sample_Id nvarchar(20), #Barcode_In_table int OUTPUT
AS
SELECT #Barcode_In_table = count(*) FROM dbo.sample_info
WHERE sample_name = #Sample_Id
RETURN #Barcode_In_table
GO
The php code that works on Windows xampp looks like this:
// use a sample Id known to be present in database
$sample_yes = '11335577';
// use a sample Id known NOT to be in database
$sample_no = '0011223344';
$host = 'hostIP';
$db = 'db';
$user = 'user';
$password = 'pw';
try {
$conn = new PDO("sqlsrv:Server=$host;Database=$db", $user, $password);
$sql = "{CALL dbo.check_for_barcode(#Sample_Id=:barcode, #Barcode_In_table=:isInDatabase)}";
$stmt = $conn->prepare($sql);
$stmt-> bindParam('barcode', $sample_yes, PDO::PARAM_STR);
$stmt->bindParam('isInDatabase', $isInDatabase, PDO::PARAM_INT|PDO::PARAM_INPUT_OUTPUT, 4);
$stmt->execute();
echo 'for sample id 11335577 $isInDatabase = '.$isInDatabase."\n";
$stmt-> bindParam(':barcode', $sample_no);
$stmt->execute();
echo 'for sample id 0011223344 $isInDatabase = '.$isInDatabase;
$stmt->closeCursor();
}
catch (Exception $e) {
echo $e->getMessage();
}
The output is:
for sample id 11335577 $isInDatabase = 1
for sample id 0011223344 $isInDatabase = 0
Since moving it to the Linux (Ubuntu 4.24) server where it will be deployed, it no longer works.
I installed the dblib drivers as described here.
I adapted the code to as required to use the dblib driver so that it would run without errors. This only involved changing the first two lines inside the try statement.
I have changed the first line to:
...
$conn = new PDO("dblib:host=$host;dbname=$db", $user, $password);
...
I have tried all of the following for the second line:
$sql = "{CALL dbo.check_for_barcode(#Sample_Id=:barcode, #Barcode_In_table=:isInDatabase)}";
$sql = "{CALL dbo.check_for_barcode #Sample_Id=:barcode, #Barcode_In_table=:isInDatabase}";
$sql = "CALL dbo.check_for_barcode(#Sample_Id=:barcode, #Barcode_In_table=:isInDatabase)";
$sql = "CALL dbo.check_for_barcode :barcode, :isInDatabase";
As well as all of the above using EXEC instead of CALL
The output in all cases is:
for sample id 11335577 $isInDatabase =
for sample id 0011223344 $isInDatabase =
If I add ...
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
var_dump($result);
}
... this generates no output.
There are no error message at all, so I can't see where the problem is. I would welcome any suggestions on what to try next.
It's been a while since I had to do this, but I used ODBC to connect to SQL Server from PHP running on a linux box.
Drivers - http://msdn.microsoft.com/en-gb/data/ff657782.aspx
ODBC docs - http://www.php.net/manual/en/ref.pdo-odbc.connection.php
EDIT: The ODBC drivers above are for Windows, the Linux installation instructions and drivers can be found at www.php.net/pdo_odbc