Oracle OCI statement vs ODBC statement discrepancy. (php) - php

When connecting to a database with PHP I have been using an ODBC connection with the following query:
"SELECT * FROM TAB.LE WHERE TAB.LE.Id = 1";
Where the tablename is TAB.LE.
The code used to make the query isn't required here - but it works fine returning the correct result(s). When I use an OCI connection the same query fails:
$conn = oci_connect("username", "password", "database");
if($conn){
$query = "SELECT * FROM TAB.LE WHERE TAB.LE.Id = 1";
$stid = oci_parse($conn, $query);
$res = oci_execute($query);
if($res){
echo "success";
}
else{
echo "failed";
}
}
I constantly see failed on the screen. I am stumped as to why. The odd thing is: the tablename TAB.LE works for the ODBC connection; however, when viewed in MS Access it shows as TAB_LE. I have attempted to use this different notation in the OCI connection but to no avail.

oci_execute() Executes a statement previously returned from oci_parse().
$stid = oci_parse($conn, $query);
So change
$res = oci_execute($query);
To
$res = oci_execute($stid);

Related

How to select the top row from database using php

<?php
//Step1
$db = mysqli_connect('localhost','root','','form')
or die('Error connecting to MySQL server.');
//Step2
$query = "SELECT * FROM info";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_array($result);
while ($row = mysqli_fetch_array($result)) {
$nam=$row['name'];
$fnam=$row['father'];
$date=$row['date'];
$aadh=$row['aadhaar'];
}
1.In this code it fetches all the values that are stored in the database . But I am in need of the code for fetching the top most row from the database.
2.The query with top attribute is also not working. It gives the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 * FROM info LIMIT 0, 30' at line 1
This is not a PHP issue, it's rather a matter of your MySQL query.
This will return everything because of the * usage.
$query = "SELECT * FROM info";
You can try this instead:
$query = "SELECT *
FROM info
WHERE unique_id = (SELECT MAX(unique_id) FROM info)";

Update Filemaker Pro over ODBC with MYSQL

I'm Updating with mysql over ODBC Filemaker Table.
When a field contains o'reilly or example'two I get this error message:
Warning: odbc_exec(): SQL error: [FileMaker][FileMaker] FQL0001/(1:80):
There is an error in the syntax of the query., SQL state 42000 in SQLExecDirect in C:\fm_1.php on line 49
and using addslashes() does not work.
thank you!
this is my code:
<?php
$conn = odbc_connect("DSN=Server;Database=TEST;UID=odbc;PWD=1234", "odbc", "1234");
if ($conn)
echo "\nConnection established.";
else
die("\nConnection could not be established.");
$result = odbc_exec($conn, "SELECT ID_MH, MH_Name FROM myTable WHERE MH_Name LIKE '%EXAMPLE'");
while ($row = odbc_fetch_array($result)) {
$ID_MH = $row["ID_MH"];
$MH_Name = $row["MH_Name"];
// do something
$MH_Name = addslashes($MH_Name);
$update = "UPDATE myTable SET MH_Name='$MH_Name' WHERE ID_MH=" . $ID_MH;
$data_update = odbc_exec($conn, $update);
}
odbc_close($conn);
?>
Try to escape instead of using addslashes:
"UPDATE myTable SET MH_Name=\"$MH_Name\" WHERE ID_MH=" . $ID_MH;
here is the solution:
$query = 'UPDATE myTable SET MH_Name=? WHERE ID_MH=?';
$stmt = odbc_prepare ($conn, $query);
$success = odbc_execute($stmt, array($MH_Name, $ID_MH));
source:https://www.skeletonkey.com/FileMaker_11_ODBC_Drivers/
thanks!

Trouble querying database with php*fixed*

I am having trouble selecting my data from a database and displaying it. I have looked at tutorials and i still get the same error. Some help would be appreciated. The error i am getting is couldnt fetch result.
$sql = "SELECT * FROM data";
$result = mysql_query($sql) or die("couldnt fetch result");
if($result > 0){
while ($rows = mysql_fetch_array($result)){
$username = $rows['username'];
echo $username;
}
}
Just do that (assuming got it right connecting to DB, first thing to check !)
$sql = "SELECT * FROM `data`"; // data is a reserved keyword, protect it !!!
$result = mysql_query($sql) or die("couldnt fetch result"); // potentially diying here
if($result){
while ($row = mysql_fetch_assoc($result)){
$username = $row['username'];
echo $username;
}
}
If what you're getting is literally 'couldnt fetch result' it means your mysql_query() fails, and die statement takes over. Check your database connection.
I think the very simple problem is that you check if the $result is greater then 0. But you get an resource.
$conn = mysql_connect.......
$sql = "SELECT * FROM data";
$result = mysql_query($sql) or die("couldnt fetch result");
if($result){
while ($rows = mysql_fetch_array($result)){
$username = $rows['username'];
echo $username;
}
}
And if you see your die statement you have an error in your SQL Syntax. Its very short but its possible that your table doesn't exist in that database you're trying to connect. I hope you have a connect before and its not your complete code.
You use the old mysql functions. Its better to use MySQLi or PDO.
And DATA is a reserved keyword its possible that you get problems if you use it in your query. Rename your table in prefix_data for example.
https://dev.mysql.com/doc/refman/5.7/en/keywords.html

PHP prepare and execute

I was using the following code to execute the queries in the database:
$sql = "SELECT * FROM cc_topchoices WHERE location='$location' ORDER BY position asc";
$result = mysqli_query($conn, $sql);
I have read that this way to make the queries is not secure so I want to use the statements prepare() and execute() in php
Now my code looks like this:
$sql = "SELECT * FROM cc_topchoices WHERE location=:location ORDER BY position asc";
$stmt = $conn->prepare($sql);
$stmt->execute(array(":location" => $location));
$result = mysqli_query($conn, $stmt);
But this give me this error:
Fatal error: Call to a member function execute() on boolean
Any idea?
EDIT
Now my code looks like this:
// Create connection
$conn = new PDO("mysql:host=$servername;dbname=$dbname", "$username", "$password");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("set names utf8"); //BECAUSE I NEED TO WORK WITH CHINESE LANGUAGE
$sql = "SELECT * FROM cc_topchoices WHERE location=? ORDER BY position asc";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':location', $location);
$stmt->execute(array($location));
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
if ($result > 0) {
// output data of each row
while($row = $stmt->fetch()) {
echo "<li><div><a href='". $row["rest_url"] ."'><img src='images/top_choices/". $row["image"] ."' alt='". $row["alt_desc"]. "' /></a></div></li>";
}
} else {
echo "0 results";
}
is working :) just need to know if this is a good and secure practice
PDO supports named parameters. MySQLi does not. $stmt is false to show you that the SQL you tried to prepare is syntactically malformed. Use ? instead of :location. Check the MySQLi manual for the correct way to use MySQLi. Or, alternately, switch to PDO.
Use below code to fetch records instead of mysqli_query when using pdo statements if your query returns single row.
$result = $stmt->fetch(PDO::FETCH_ASSOC);
echo $result['db_column'];
And if return multiple rows:
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while ($result = $stmt->fetch()) {
echo $result['db_column'];
}
And one more thing, always put your prepared statement in try{}..catch{} block.
It will work for you.

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