Hi I have a strange problem with below sql query.
If I run the query on sql console on phpmyadmin it returns 3 "dimo" vehicle records
SELECT * FROM bdata WHERE vehicle LIKE 'dimo%' // returns 3 records
but if I make a php file and run it does not return any records but if I change the search to car it shows all the records under car.
Please can someone help I tried many things including changing database structure but did not work. I spent almost a week to fix this issue before seeking help on stackoverflow
<?php
define('HOST','****');
define('USER','****');
define('PASS','*****');
define('DB','*******');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "SELECT * FROM bdata WHERE vehicle LIKE 'dimo%' ORDER BY vehicle";
// database has three dimo vehicles but does not show on the php
// but works fine on phpmyadmin console
$res = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($res))
{
echo $row[15];
}
?>
Please can someone help
Related
At first I'm sorry for posting duplicate question I always try to find answers and never to ask. But nothing solved my problem. I have a MySql DB with table named data. I cannot change the table name. When I execute SELECT * FROM `data` or SELECT * FROM data in phpMyAdmin, the query works correctly but when I execute it in PHP script the query() returns false
<?php
$conn = new mysqli('localhost', 'username', 'pswd', 'dbname');
if ($conn->connect_error) {
die('connection error');
}
$result = $conn->query("SELECT * FROM `data`");
var_dump($result);
echo "-".$conn->error."-";
I have looked at these questions:
Mysql query works in phpmyadmin but not in php (due to date)
Mysql query works in Phpmyadmin but not works in PHP
MySQL query working in phpmyadmin but not in php
and some others...
$result ="SELECT * FROM `data`";
$row=mysqli_query($conn,$result);
while($row_result=$row->fetch_assoc())
print_r($row_result);
With your replies I've got some idea what to try next.I've created copy of the table on my own server and tried changing data types. One of data types in original table is set as JSON, when i changed it to TEXT it started to work.
i would like to ask regarding the php code and oracle sql. I am developing a website for a system as my project task. for the searching page, user can fill in one of the searching field and the system will display all the required details based on what user search for. can anyone teach me on how to do the php code to link with the oracle SQL database. as what i have done so far is:
$sql = "SELECT * FROM sdlrules_tbl_wip_queue
where lotid = trim(upper((:lotid)),
eqpId = trim(upper(:eqpId)),
stepName = trim(:stepName),
sequence = trim(:sequence)";
if($_REQUEST["lotId"]!=""){
$sql .= "SELECT
LOTID,PLAN,STEPSEQ,STEPNAME,LOTTYPE,PRIORITY,DEVICE,EQPID,REMARK
FROM
sdlrules_tbl_wip_queue
WHERE lotid = trim(upper(:lotid))";
}
if($_REQUEST["eqpId"]!=""){
$sql = "SELECT * FROM sdlrules_tbl_wip_queue
WHERE eqpId = ':eqpId'";
}
if($_REQUEST["stepName"]!=""){
$sql = "SELECT * FROM sdlrules_tbl_wip_queue
WHERE stepName = ':stepName'";
}
if($_REQUEST["sequence"]!==""){
$sql = "SELECT * FROM sdlrules_tbl_wip_queue
WHERE sequence = ':sequence'";
}
can anyone teach me on how to do the php code to link with the oracle SQL database.
This is a broad question.
Oracle has two manuals you might want to check out
The Underground PHP and Oracle Manual
Database 2 Day + PHP Developer's Guide
There is some older install content in these books you can skip, but there is much material that is useful.
You simply need to read following resources to connect to Oracle database and perform required operations. Just read functions descriptions. Its pretty straight forward.
http://php.net/manual/en/intro.oci8.php
http://php.net/manual/en/ref.oci8.php
I'm trying to fill various html selects with their respective information. For that I feel the right way is to make a query for each of them. I do the first query with this php tag to fill the first select like this:
<?php
$sql = "call mydb.getPositions();";
$result = $conn->query($sql);
while($row = $result->fetch_row()){
echo "<option>".$row[0]."</option>";
}
?>
That's it. Simple. It works. The function getPositions() is a select. But when I tried to fill the next select/combobox with it's own query, it started throwing this error:
Fatal error: Call to a member function fetch_row() on a non-object in file.php in line 200
I searched for a lot of reasons for a mistake like typos in the query or fetching rows from a delete query.
But then to discard any mistake of those, I decided to copy the exact same php tag one after the other. So, if the first works, why wouldn't the second work? So there I saw that I was receiving the exact same mistake. What am I missing? I tried to close the $result, with no success.
Thanks in advance.
EDIT.
When I said "I decided to copy the exact same php tag one after the other" I really meant that like:
<?php
$sql = "call mydb.getPositions();";
$result = $conn->query($sql);
while($row = $result->fetch_row()){
echo "<option>".$row[0]."</option>";
}
?>
<?php
$sql = "call mydb.getPositions();";
$result = $conn->query($sql);
//It throws the error down here
while($row = $result->fetch_row()){
echo "<option>".$row[0]."</option>";
}
?>
And this throws the same error described before.
EDIT2.
The procedure is this:
CREATE DEFINER=`mainSoccer`#`%` PROCEDURE `getPositions`()
BEGIN
Select namePosition from mydb.Position;
END
It looks like your question is actually: how do I run the same MySQL query twice in a php script? If so, here's the answer:
<?php
$sql = "Select namePosition from mydb.Position;";
$result = $conn->query($sql);
while($row = $result->fetch_row()){
echo "<option>".$row[0]."</option>";
}
?>
<!-- presumably you have other code in between here -->
<?php
$sql = "Select namePosition from mydb.Position;";
$result = $conn->query($sql);
while($row = $result->fetch_row()){
echo "<option>".$row[0]."</option>";
}
?>
And in your comment to my original answer below, you asked "Why shouldn't I be creating a MySQL procedure?" Two answers:
Your initial code is creating the same procedure twice. That is surely throwing an error at the MySQL level. Generally one creates the procedure once for permanent re-use directly in your server, so that many different scripts can them simply invoke (not create) that procedure at any time.
You should use MySQL procedures for complex, large MySQL operations that have performance issues that can gain efficiency as a pre-compiled procedure. Your query here is very simple and does not call for a MySQL procedure.
Hope this helps.
Since you only posted your code that works, but not the code that doesn't work, you can't get specific help.
But I can tell you that the error you quote "Fatal error: Call to a member function fetch_row() on a non-object in file.php in line 200" is typically caused when you write invalid SQL.
First confirm that the SQL you are attempting runs on its own as raw SQL, for example in phpMyAdmin.
let's assume i have a table with five columns like TABLE(a,b,c,d,e)
now if i try to fetch multiple columns like
$sql="select a,b,c from TABLE";
$result = mysqli_query($link,$sql );
if (!$result)
{
echo "something wrong";
exit();
}
and so on................
note there is not any wrong in rest code.because the code is working properly when 1 column is selected
while multiple columns are selected it's causing error ::
[Error 101 (net::ERR_CONNECTION_RESET): The connection was reset.]
it particularly happening when only multiple columns are selected.*
i have been told it's my apache config problem ...if so the can you
tell me how to fix it??
I have this problem.. i am trying to use the lock record for the update. I am using php, and i use oracle as the database. i didnt understand fully but i have managed to copy the previous developer code and make it working.
It is using the odbc_exec(), odbc_fetch_row(). I think that this is suppose to be use for microsoft access data base. But i don't know. And i am also using jquery to fetch this php file(using jquery post) and append to the current page.
But now my senior request me to make sure that if a person is editing one record. then the other cant edit it anymore. They said that something about not closing the connection. can php not close connection to database after 1 page is loaded (when it is using jquery to get the page like mine does)? from what i know, if a page is loaded. the connection to the database is closed.
I want to try this code but i am afraid if i am wrong and the connection would hang.
Now my sql is like this for select all company.
$sqlStr ="select * from company";
$rs = odbc_exec($GblConnOra , $sqlStr);
while (odbc_fetch_row($rs)){
$company_name=odbc_result($rs,"company_name");
$company_id=odbc_result($rs,"company_id");
}
And for select the company to edit
$sqlStr ="select * from company where company_id='34'";
$rs = odbc_exec($GblConnOra , $sqlStr);
while (odbc_fetch_row($rs)){
$company_name=odbc_result($rs,"company_name");
}
The updating
$sqlStr ="update compay set company_name=? where company_id=?";
$stmt = odbc_prepare($GblConnOra , $sqlStr);
$res = odbc_execute($stmt, array($company_name, $company_id));
i think the sql will be like this. to lock the record.
$sqlStr ="select * from company for update skip locked";
$rs = odbc_exec($GblConnOra , $sqlStr);
while (odbc_fetch_row($rs)){
$company_name=odbc_result($rs,"company_name");
$company_id=odbc_result($rs,"company_id");
}
can i do this?
what would my sql be for selecting a row to edit?
what would my sql be for updating the row?
And from my understanding, this lock would be release after an update commit or rollback is call.