The row in my database is not changing even though it is able to call from that row just not write to it
<?php
$formId = $_POST["formId"];
$newSuper = $_POST["newSuper"];
$return = $_POST;
require_once('mcl_Oci.php');
$xdm = new mcl_Oci("xdm");
$sql = "SELECT x.*,x.ROWID FROM INTOXDM.ASTP_FORM x WHERE x.FORMID = '".$formId."'";
while($row = $xdm->fetch($sql)){
$row['SUPERVISOR_EID'] = $newSuper;
}
$return["json"] = json_encode($return);
echo json_encode($return);
?>
Related
I would like to see where my code is incorrect. I want to store values from my database as a php array. Then I'd like to store the individual parts of the array as separate variables. Here is my code:
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = mysqli_fetch_row($result);
$comment0 = $comments[0];
$comment1 = $comments[1];
$comment2 = $comments[2];
$comment3 = $comments[3];
$comment4 = $comments[4];
$comment5 = $comments[5];
$comment6 = $comments[6];
$comment7 = $comments[7];
$comment8 = $comments[8];
$comment9 = $comments[9];
?>
This will run your mysql query and add each comment to an array of comments, then print the array.
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = array();
while($comment = mysqli_fetch_row($result)){
$comments[] = $comment;
}
print_r($comments);
?>
not sure if you are in console or through web server.
<?php
$result = mysqli_query($db, "SELECT column FROM table");
if (!$result) {
echo 'Could not run query';
exit;
}
$comments = mysqli_fetch_row($result);
foreach($comments as $comment){
echo print_r($comment,1).'--------------\r\n<br>\r\n';
}
?>
this is called a loop. loop is your friend.
I am building a site, and essentially what this PHP algorithm will do is look at a product (row in MySQL database) one at a time, and do a process accordingly.
I put a lot of research into this but couldn't find anything, any help would be greatly appreciated!
My Code (currently returning nothing for echo variables):
<?php
include_once 'dbconnect.php';
$query = "SELECT * FROM track";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$pro_code = mysql_result(mysql_fetch_array(mysql_query('SELECT product_code FROM track')));
$currency = mysql_fetch_array(mysql_query('SELECT currency FROM track'));
$cc = mysql_fetch_array(mysql_query('SELECT cctld FROM track'));
$initial_price = mysql_fetch_array(mysql_query('SELECT initial_price FROM track'));
$url = 'test';
}
echo $pro_code;
echo $currency;
echo $initial_price;
?>
First of all, try the advice about PDO and stuff from Jay Blanchard some day.
Secondly I've tried to answer your question anyway and I've tried to interpret your complete intention. I put comments in the code:
<?php
include_once 'dbconnect.php';
$query = "SELECT * FROM track";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
//You need to read the row variable as an array
$pro_code = $row['product_code'];
$currency = $row['currency'];
$cc = $row['cctld'];
$initial_price = $row['initial_price'];
//$url is not used.. I asume a test to get the external source ;-)
$url = 'test';
if ($url == $cc) {
//if you want to print every row, you must echo inside the while loop
echo $pro_code;
echo $currency;
echo $initial_price;
} elseif ($url == 'test') {
//do something else here
} else {
//do something for all the other cases here
}//end if
}//end while
?>
Why do you query the same table multiple times, your code should be written like this:
include_once 'dbconnect.php';
$query = "SELECT product_code, currency, cctld, initial_price FROM track";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo $row['product_code'];
echo $row['currency'];
echo $row['cctld'];
echo $row['initial_price'];
}
and please upgrade to mysqli or PDO
I'm trying to fetch couple of single data in my server database but this is throwing some errors. The incoming data is correct. The search function just don't get completed.
Here's the code:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
define('HOST','xxxxxxxxxxx');
define('USER','xxxxxxxxxxxx');
define('PASS','xxxxxxxxx');
define('DB','xxxxxxxxxx');
$con = mysqli_connect(HOST,USER,PASS,DB);
$post_id = $_POST['id'];
$buyer_mobile = $_POST['mobile'];
$buyer_name = $_POST['name'];
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysqli_query($con,$sql);
$owner_mobile = $row['mobile'];
$sql = "select name from user where mobile='$owner_mobile'";
$r = mysqli_query($con,$sql);
$owner_name = $row['name'];
$sql = "INSERT INTO flat_booking (post_id,owner_mobile,owner_name,buyer_mobile,buyer_name) VALUES ('$post_id','$owner_mobile','$owner_name','$buyer_mobile','$buyer_name')";
if(mysqli_query($con,$sql)){
echo "Success";
}
else{
echo "error";
}
mysqli_close($con);
}else{
echo 'error1';
}
What am I doing wrong here? Maybe this:
$owner_mobile = $row['mobile'];
Thanks in advance!
create table flatower and add mobile column
$post_id = 1;
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysql_query($con,$sql);
$row = mysql_fetch_array($res);
$owner_mobile = $row[0]['mobile'];
Your problem is this line:
$owner_mobile = $row['mobile'];
You have not created the $row variable. For this you would need to do something such as:
Do this first:
<?php
$row = array();
while ($result = mysqli_fetch_assoc($res))
{
$row[] = $result;
}
?>
This allows you to do this:
<?php
foreach ($row as $r)
{
var_dump($r); print "<br />"; // One row from the DB per var dump
}
?>
I'm a newbie to PHP and I'm just trying the very basics of MVC. Everything is going good but I have a problem while fetching data from MySQL and populating a HTML table with it.
The problem is that my code is just returning one row of the table (there are three rows in that table).
I have tried many things and right now I'm using arrays for storing the data and passing to controller and then to the view.
Query class file having a function for getting data and name queryDB:
public function getdata(){
$connectObj=new dbConnection();
//its a connection class where mysql connection has been made
if(!$connectObj->connectDB()){
echo "Error in mysql: ".mysql_error();
return false;
}
else{
$query = "select * from tbl_cartypes";
$result = mysql_query($query) or die("Error: ".mysql_error());
$data = array();
while($row = mysql_fetch_assoc($result)){
$data[0] = $row['car_id'];
$data[1] = $row['car_name'];
$data[2] = $row['car_model'];
$data[3] = $row['car_type'];
$data[4] = $row['car_price'];
}
return $data;
}
$connectObj->closeDB();
}
The controller class where the controller of this query is name carController.php:
public function getAllData(){
$runQuery = new queryDB();
$array = array();
$array = $runQuery->getTickets($userid);
return $array;
}
And the final view where I'm just echoing my data:
include "$path/controllers/carController.php";
$ticket = new carController();
$array = array();
$array = $ticket->getdata();
for($i=0;$i<count($array);$i++){
echo $array[$i]."<br />";
}
Output of this code is without error, but the problem is that it's just fetching one row of the table whereas there are three rows.
So any one can help me with this?
It's fetching all rows, but you're saving all the data to the same place ($data[0] through $data[5]), so all but the last row is getting overwritten.
This might work better:
$data = array();
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
Using PDO and what other people have posted try using this
public function getdata(){
$connectObj=new dbConnection();
//its a connection class where mysql connection has been made
if(!$connectObj->connectDB()){
echo "Error in mysql: ".mysql_error();
return false;
}
else{
$query = 'select * from tbl_cartypes';
$result = $connectObj->query($query);
$data = array()
foreach ($result as $row){
array_push($data, $row)
}
return $data;
}
$connectObj->closeDB();
}
Your problem is that you're overwriting the values of the previous table:
while($row = mysql_fetch_assoc($result)){
$data[0] = $row['car_id'];
$data[1] = $row['car_name'];
$data[2] = $row['car_model'];
$data[3] = $row['car_type'];
$data[4] = $row['car_price'];
}
This will just re-write the last row of data over the key's in that table.
Try:
$data = array()
while($row = mysql_fetch_assoc($result)){
array_push($data, $row)
}
Your while loop is assigning just one row to array $data. in while loop instead try this
while($row = mysql_fetch_assoc($result)){
$data["car_id"][] = $row['car_id'];
$data["car_name"][] = $row['car_name'];
$data["car_model"][] = $row['car_model'];
$data["car_type"][] = $row['car_type'];
$data["car_price"][] = $row['car_price'];
}
return $data;
Now you can iterate through the array.
The problem is that only some of the XML data is being Inserted into the my mysql database. 10 results are supposed to be entered into the database but it varies between 2 and 8 results. I have no idea why it is doing this and I have tried adding a sleep function to slow the script down, but the data that is inserted into the data base is never as much as when I echo it out on screen. Any help would be much appreciated..
function post_to_db($xml,$cat_id){
if ($xml->Items->Request->IsValid == 'True'){
$xml = $xml->Items->Item;
foreach($xml as $item){
$asin = (string)$item->ASIN;
$title = (string)$item->ItemAttributes->Title;
$content = (string)
$item->EditorialReviews->EditorialReview->Content;
$sku = (string)$item->ItemAttributes->SKU;
$brand = (string)$item->ItemAttributes->Brand;
$feature = (string)$item->ItemAttributes->Feature;
$model_no = (string)$item->ItemAttributes->Model;
$review = (string)$item->ItemLinks->ItemLink[5]->URL;
$check = "SELECT * FROM `products` WHERE `asin` = '$asin'";
$checked = mysql_query($check);
$numrows = mysql_num_rows($checked);
if ($numrows == 0){
$query = "INSERT INTO `products`".
"(`cat_id`,`asin`,`sku`,`brand`,".
"`model_no`,`title`,`content`,`feature`) ".
"VALUES".
"('$cat_id','$asin','$sku','$brand',".
"'$model_no','$title',".
"'$content','$feature')";
$result = mysql_query($query);
$post_id = mysql_insert_id();
$review_page[] = array($post_id=>$review);
}
}
}
return $review_page;
}
My guess would be some of your variables from XML are creating an invalid query (do they contain quotes?)
Instead of this for each variable:
$asin = (string)$item->ASIN;
Do this instead:
$asin = mysql_real_escape_string((string)$item->ASIN);
If the problem still persists, change your mysql_query line to this for debugging:
$result = mysql_query($query) or die(mysql_error());