I'm trying to use Auto-complete using Oracle.
I'd done this using MySQL, but with Oracle I got blank spaces.
This my MySQL Code.
global $db;
$term = trim(strip_tags(strtoupper($_GET['term'])));//retrieve the search term that autocomplete sends
$sql = "SELECT partNo as value FROM products WHERE partno LIKE '".$term."%' ORDER BY partNo LIMIT 15";
Basic::EventLog('getPartNosFromDB-> '.$sql);
$res =& $db->query($sql);
while ($row=$res->fetchRow()){
$row['value']=htmlentities(stripslashes($row['value']));
$row_set[] = $row;//build an array
}
echo json_encode($row_set);
And this is Oracle
$term = trim(strip_tags(strtoupper($_GET['term'])));//retrieve the search term that autocomplete sends
$conn = oci_connect('XXXX', 'XXXXX', 'XXXXX');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$sql = "SELECT product_definition as value FROM t_wip_job WHERE product_definition LIKE '".$term."%' AND ROWNUM < 16";
Basic::EventLog('getPartNosFromDB-> '.$sql);
$res = oci_parse($conn, $sql);
oci_execute($res);
while ($rowcc = oci_fetch_array($res, OCI_ASSOC)){
$row_set[] = $rowcc;//build an array
}
echo json_encode($row_set);
If I execute these query the first returns -> [{"value":"A2C00000000"},{"value":"A2C00000001"}] (MySQL)
And the second returns -> [{"VALUE":"A2C87115000"},{"VALUE":"A2C87114900"}] (Oracle)
But inside textbox when it tries to auto-complete the MySql result shows "A2C87115000" and "A2C87114900"
and the Oracle result shows " " and " ".
The problem could be with the case of the hash keys, since the MySql hash keys are lowercase ("value"), while the Oracle ones are uppercase ("VALUE").
When you load the json, since javascript is case sensitive, looking for "value" it finds the content only in the first case.
Try having lowercase keys in both case. This should do the trick!
Related
I'm attempting to grab a number value from a mySQL database using PHP. I'd like to use that number value for some calculations after I've assigned it's value to a variable. The number value is stored as a float in my database. My code attempts to connect to my database (which it does successfully), then pull a number value based on parameters passed on by my query, then apply that number value to a variable that I can use in my code. In my code below, I'm simply trying to print that value to make sure it's pulling properly. The result I get is this notice: Notice: Array to string conversion in /Users/max/Desktop/Sites/webprojects/prg/nba/percentchange.php on line 14 Array
Here's my code:
$mysqli = new mysqli('localhost', 'root', 'root', 'NBA');
if (mysqli_connect_errno()) {
echo '<p>Error: could not connect to database.</p>';
exit;
}
$rank = "SELECT average FROM nbaCompRankings WHERE team = 'Celtics'";
$result = $mysqli->query($rank);
$currentRank = $result->fetch_assoc();
echo $currentRank;
The potential solutions I've found on this site use deprecated libraries, so I've been unsuccessful in a search for a solution. Thanks in advance for any help you can give!
Try This:
$result = $mysqli->query($rank);
while($row = $result->fetch_assoc()) {
$currentRank = $row["average"];
}
You should really be preparing your statements though.
$team = 'Celtics';
$sql= "SELECT average FROM nbaCompRankings WHERE team = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $team);
$stmt->execute();
$stmt->bind_result($average);
while($stmt->fetch()) {
$currentRank = $average;
}
$stmt->close();
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('censored','censored','censored','db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db");
$sql="SELECT message FROM messages WHERE code = '".$q."'";
$result = mysqli_query($con,$sql);
$resultstring = (string)$result;
echo $resultstring;
mysqli_close($con);
?>
I am attempting to echo the result of the query to the user but when this PHP runs through ajax I get this error:
Recoverable fatal error: Object of class mysqli_result could not be
converted to string in D:\xampp\htdocs\getmessage.php on line 12
Now I don't understand this because I am already converting $result into a string.. Thanks!
mysqli_query() can not convert to string.
You must use mysqli_fetch for parse to array.
Example:
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
printf ("%s (%s)\n",$row[0],$row[1]);
}
}
First of all mysqli_query Performs a query on the database
it does not directly return a value
$query = mysqli_query($con,$sql);
so to do that use mysqli_fetch_assoc it will Fetch a result row as an associative array
$result = mysqli_fetch_assoc($query);
Then you can now get your value
$resultstring = $result['message'];
so your code should be like this
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('censored','censored','censored','db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db");
$sql="SELECT message FROM messages WHERE code = '".$q."'";
$query = mysqli_query($con,$sql);
$result = mysqli_fetch_assoc($query);
$resultstring = $result['message'];
echo $resultstring;
mysqli_close($con);
?>
you need to convert the data(RS) into array !
$result = mysqli_fetch_assoc($result);
print_r($result);
As an result of your query on success You will get mysqli_result object. If You want to get received rows use fetch data function.
$result = mysqli_query($con,$sql);
$resultstring = $result->fetch_row()[0];
See docs: mysqli_query &
mysqli_result
I have resolved similar issue with following approach.
// Assign your sql statement
$sqlMasterSiteIds = "SELECT DISTINCT table_name FROM site WHERE gwid = 39 AND master_site_id NOT LIKE '0'";
// Run the query
$masterSiteIdsQuery = $this->db->query($sqlMasterSiteIds);
// Prepare container
$masterSiteIds = [];
// Fetch first row of query results
$first_row = $masterSiteIdsQuery->fetch_assoc();
// Assign first row to your prepared container
$masterSiteIds= $first_row['master_site_id'];
// Change the results into string comma separated so it can be used in next mysqli query or echoed
while($sub_row = $masterSiteIdsQuery->fetch_assoc()) {
$masterSiteIds = implode(',', array($masterSiteIds, $sub_row['master_site_id']));
}
// Now you can use $masterSiteIds which is a STRING of values comma separated
echo $masterSiteIds; // Output: '1024, 1142, 8492'
I hope it will help you or anybody else in need. I am not sure if it is the best approach but it worked well in my case as I had to change my results into STRING so I could use it in my next mysqli query with FIND_IN_SET command.
Happy coding!
I want to print a value that taken from oracle database. but it is showing error.
Below is my code.
<?php
// Connects to the XE service (i.e. database) on the "localhost" machine
$conn = oci_connect('FUNREAD', 'clock24', '10.6.8.51:1525/DRFUNPD1');
if (!$conn)
{
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$atp1 = oci_parse($conn, 'select count(*) as count from ATP1.PENDING_MESSAGE');
oci_execute($atp1);
$atp1 = oci_fetch_array($atp1);
echo $atp11;
?>
But I am not getting right output. Below output I am getting.
Resource id #4
You're trying to echo a resource result, where what you need to do is to either retrieve the first row and output that, or loop over the results and output them:
while ($row = oci_fetch_array($atp1)) {
print_r($row);
}
Note that fundamentally it's probably a good idea not to re-use the $atp1 variable for both the query and the result set, use something more descriptive, e.g.:
$query = oci_parse($conn, 'select count(*) as count from ATP1.PENDING_MESSAGE');
oci_execute($query);
while ($row = oci_fetch_array($query)) {
print_r($row);
}
I'm pretty much copying this straight out of the manual (since I haven't used Oracle functions in PHP before)...
I am trying to use a function to perform an SQL query and return the result. Doing a manual SQL query with the same query as the below works and returns the correct result but doing it within PHP is not working at all (page is blank).
connection.php
<?php
require_once("settings.php");
$conn = oci_connect($db_user, $db_pass, $db_ip.'/'.$db);
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
?>
functions.php
<?php
require_once("connection.php");
function GetInvoice($n)
// returns invoice number for given order number
{
$stid = oci_parse($conn, 'SELECT InvoiceNo FROM Orders WHERE OrderNo = $n');
oci_define_by_name($stid, 'InvoiceNo', $InvoiceNo);
oci_execute($stid);
echo "$InvoiceNo";
}
GetInvoice(999645);
?>
Doing the manual SQL query shows a resulting InvoiceNo, it is just not working in PHP.
Thanks
Steve
You are trying to use a variable outside of function inside the function. That variable is not in function's scope. Due to this, $conn is undefined. Also, you have used $InvoiceNo withoud fetch result, I did some refoctoring on that place. You can use following;
function GetInvoice($n, $conn)
// returns invoice number for given order number
{
$stid = oci_parse($conn, 'SELECT InvoiceNo FROM Orders WHERE OrderNo = $n');
oci_define_by_name($stid, 'InvoiceNo', $InvoiceNo);
oci_execute($stid);
while (oci_fetch($stid)) {
echo "$InvoiceNo";
}
}
GetInvoice(999645, $conn);
The parameters you are passing in functions, seems undefined
as you have created your function needs only one parameter, then from where this will get this variable $InvoiceNo
oci_define_by_name($stid, 'InvoiceNo', $InvoiceNo);
I have a database table made in phpmyadmin. I want the elements of the table to be stored row-wise in an array. I have three columns named edge_id, vertexA and VertexB. I want the elements of these three columns to be stored into an array.
I don't know those commands for PHP. Please help me out.
i have columns in my table named
"vertexa" and "vertexb",i want to
store the colums in two separate
arrays ...how can i do it??
The simplest way would be:
$db = new PDO('mysql:host=localhost;dbname=your_db_name;', $user, $password);
$vertices_a = array();
$vertices_b = array();
foreach($db->query('SELECT * from your_table_name') as $row){
$id = $row['edge_id'];
// add them to the proper array using the edge_id as the index -
// this assumes edge_id is unique
$vertices_a[$id] = $row['vertexa'];
$vertices_b[$id] $row['vertexb'];
}
So with PDO:
$db = new PDO('mysql:host=localhost;dbname=your_db_name;', $user, $password);
foreach($db->query('SELECT * from your_table_name') as $row){
echo $row['edge_id'];
echo $row['vertexA'];
echo $row['vertexB'];
}
Now if you need to use input data you need to escape it. The best way to do this is to use a prepared statement because escaping is handle when the parameters are bound to the query.
Lets say for example you want to use the edge_id supplied from a get request like mysite.com/show-boundry.php?edge=5...
$db = new PDO('mysql:host=localhost;dbname=your_db_name;', $user, $password);
// create a prepared statement
$stmt = $db->prepare('SELECT * from your_table_name WHERE edge_id = ?');
// execute the statement binding the edge_id request parameter to the prepared query
$stmt->execute(array($_GET['edge']));
// loop through the results
while(false !== ($row = $stmt->fetch(PDO::FETCH_ASSOC))){
echo $row['edge_id'];
echo $row['vertexA'];
echo $row['vertexB'];
}
Also you shouldnt use mixed case column/table names like vertexA instead use underscore separators like vertex_a.
You first need to connect to the database:
$link = mysql_connect('localhost','username','password');
if (!$link) {
// Cannot connect
}
$ret = mysql_select_db('database-name', $link);
if (!$ret) {
// Cannot select database
}
Then you need to execute your query:
$ret = mysql_query('SELECT * FROM `table`;', $link);
if (!$ret) {
// Query failed
}
Then you simply load each row:
$data = array();
while ($row = mysql_fetch_assoc($ret)) {
$data[] = $row;
}
Then you should free your request results
mysql_free_result($ret);
And voilĂ .
$res = mysql_query("............");
$row = mysql_fetch_array($res);
Checkout: mysql_fetch_array PHP page