I'm running into a situation where a mysql_query() result being fed into a mysql_fetch_array() function is being interpreted as a boolean instead of the result.
The below code uses Using an SQL result in a foreach loop as a coding example for doing a foreach loop. There may be multiple problems with the code still as my current problem occurs before the foreach loop.
$results=mysql_query("SELECT * FROM order_details WHERE orderid = $orderid");
print "SELECT * FROM order_details WHERE orderid = $orderid";
$productid;
$quantity;
$price;
$resultset = array();
while ($row = mysql_fetch_arraY($results)) {
$resultset[] = $row;
}
foreach ($resultset as $result)
{
$productid = $result['productid'];
$quantity = $result['quantity'];
$price = $result['price'];
print "<br />$productid, $quantity, $price";
};
Change $orderid to '$orderid' provided that everything is fine.
One big note, try going over mysqli and PDO instead of mysql.
for($i=0;$i<$max;$i++) {
$pid=$_SESSION['cart'][$i]['productid'];
$q=$_SESSION['cart'][$i]['qty'];
$price=get_price($pid);
$pname;
$row = mysql_fetch_assoc(mysql_query("SELECT name\n"
. "FROM `products` \n"
. "WHERE SERIAL =$pid\n"
. "LIMIT 1"));
$pname = $row['name'];
print "<br />Product Name: $pname, Quantity: $q, Price: $price";
}
Related
$con=mysqli_connect("localhost","root","","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
error_reporting(E_ALL ^ E_NOTICE);
$query = "SELECT * FROM TT_posts WHERE post_status='publish' AND
ping_status='open'";
$result = $con->query($query);
while($row1 = $result->fetch_assoc())
foreach ($result as $row1){
$image = "SELECT * FROM TT_posts WHERE post_title='$row1[post_name]'";
$result1 = $con->query($image);
while($row2 = $result1->fetch_assoc())
foreach ($result1 as $row2){
echo "<img src='".$row2[guid]."'>";
echo "<p>".$row1[post_title]."</p>";
}}
?>
actually, below query returns 8 results.
$query = "SELECT * FROM TT_posts WHERE post_status='publish' AND
ping_status='open'";
When it executed the loop, it stops at the first result. I don't know what exactly stops the code.
I think you've got your variable names mixed up. Your first while assigns a row to $row1, then your foreach refers to $result, not $row1. The variable $row1 is actually being overwritten every time it loops around the foreach.
You don't actually need either foreach:
while ($row1 = $result->fetch_assoc()) {
$image = "SELECT * FROM TT_posts WHERE post_title='$row1[post_name]'";
$result1 = $con->query($image);
while ($row2 = $result1->fetch_assoc()) {
echo "<img src='".$row2[guid]."'>";
echo "<p>".$row1[post_title]."</p>";
}
}
It is a tracking system like DHL. Tracking shipment number from MySQL database using php form.
but I need it Search multiple row separate by comma from mysql using php.
<?php
$ship=$_POST['Consignment'];
$cons = explode(',',$ship);
?>
<?php
$sql = "SELECT * FROM tbl_courier WHERE cons_no = '$cons[]'";
$result = dbQuery($sql);
$no = dbNumRows($result);
if($no == 1){
while($data = dbFetchAssoc($result)) {
extract($data);
?>
Shipment Name: <?php echo $ship_name; ?>
Shipment Phone: <?php echo $phone; ?>
<?php }//while
}//if
else {
echo 'In else....';
?>
Consignment Number not found.Search Again.
<?php
}//else
?>
So I need my search will work with separating by comma(,).
Thanks for helping me.
You can use IN operator in that case.
<?php
$ship=$_POST['Consignment'];
?>
<?php
$sql = "SELECT * FROM tbl_courier WHERE cons_no IN(".$ship.")";
$result = dbQuery($sql);
$no = dbNumRows($result);
if($no == 1){
while($data = dbFetchAssoc($result)) {
extract($data);
?>
Hope it will help to you.
change your sql query you have written '$cons[]' in select query which is wrong . after explode you will get data as 1,2,3 so you just need to write variable in query not array and user IN Operator like this.
`$sql = "SELECT * FROM tbl_courier WHERE cons_no IN(".$ship.")";`
You should always prepare/sanitize the POST data before using it in MySql query (in terms of security):
<?php
if (isset[$_POST['Consignment']] && !empty($_POST['Consignment'])) {
$ship = $_POST['Consignment'];
$cons = explode(',', $ship);
$cons = array_filter($cons, function($v){
return trim(strip_tags($v));
});
$cons = '"' . implode('","', $cons) . '"';
$sql = "SELECT * FROM tbl_courier WHERE cons_no IN ($cons)";
$result = dbQuery($sql);
$no = dbNumRows($result);
if ($no == 1) {
while ($data = dbFetchAssoc($result)) {
extract($data);
....
}
....
}
?>
Please Use Find IN SET
SELECT * FROM tbl_courier WHERE FIND_IN_SET(cons_no,'1,2,3,4,5')
Updated
SELECT * FROM tbl_courier WHERE FIND_IN_SET(cons_no,'$ship')
Note :- $ship Comma Separated Value not an array
I found the Answer:
if(isset($_POST['Consignment'])){
$ship=$_POST['Consignment'];
$shipment= explode(',',$_POST['Consignment']);
$ship = implode("', '",$shipment) ;
$query = "SELECT * FROM `tbl_courier` WHERE `cons_no` IN('$ship')";
$results = $mysqli->query($query);
if($results){
print '<table border="1">';
while($row = $results->fetch_assoc()) {
print '<tr>';
print '<td>'.$row["cons_no"].'</td>';
print '<td>'.$row["customerName"].'</td>';
print '<td>'.$row["customerPhone"].'</td>';
print '</tr>';
}
print '</table>';
// Frees the memory associated with a result
$results->free();
}
else {
echo "Query Not Match";
}
$mysqli->close();
}
Thanks to Answer.
$query = "SELECT * FROM main";
if ($result = $db->query($query)) {
while ($row = $result->fetch_assoc()) {
$name= $row["name"];
$image = $row["image"];
}
}
then somewhere in my code I print it out using like echo $name; but I got only one result, which is the last row. I know I have to do foreach but what variable should I put?
foreach($result as $results) ? like this?
On every iteration, you reassign the values to $name and $image, causing it to show only the last value when it leaves the loop.
You can either echo them right away in the loop, or populate an array or an object with them, so they will be available later.
Example:
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = array('name'=>$row["name"], 'image'=>$row["image"]); // push into the array
}
var_dump($data); // it's all here now
And to echo the data later, one of the ways is foreach:
foreach($data as $row) {
echo "Name: ", $row['name'], "; Image: ", $row['image];
}
$query = "SELECT * FROM main";
$result = $db->query($query);
$row = $result->fetch_assoc();
foreach ($row as $rows) {
echo $rows['name'] ." ". $rows['image'];
}
I used following code to get the total price, but it outputs value that is higher than the expected value. What would be the reason for this?
$query = " SELECT *
FROM `mytable`
WHERE `sale_id` = $id";
$result = mysql_query($query);
$item = mysql_fetch_array($result);
foreach ($item as $row) {
$total_price += $row['price'];
}
echo $total_price;
Try this,
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$total_price += $row['price'];
}
echo $total_price;
please why is the loop statement breaking, after the multiplication it only update the last row, tried using a foreach loop for the $totalprice in the update statement it says invalid argument.
if(array_key_exists('item', $_POST)){
// $items = $_POST['item'];
//foreach($_POST['item'] as $item){
//echo $item['Pquantity'] . ", ";
//echo $item['Pidno'] . ", ";
// }
//Loop through $_POST items, updating the database for each item
foreach ($_POST['item'] as $item) {
$Pquantity = intval($item['Pquantity']);
$Pidno = ($item['Pidno']);
//echo $Pquantity . ", ";
//echo $Pidno . ", ";
//$totalprice = intval($item['Pquantity'])
$queryreg = mysql_query("
UPDATE repplac
SET Pquantity = {$Pquantity}
WHERE
Pidno = '{$Pidno}'
AND
Uname = '{$_SESSION['username']}'
") or die(mysql_error());
}
}
$pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'") or die(mysql_error());
while ($row = mysql_fetch_assoc($pplresult))
{
$totalprice = $row['Price'] * $row['Pquantity'];
//echo "$totalprice";
//die();
$queryreg = mysql_query("
UPDATE repplac
SET Tprice = {$totalprice}
WHERE
Pidno = '{$Pidno}'
AND
Uname = '{$_SESSION['username']}'
") or die(mysql_error());
}
Neither arguments in the second condition gets changed inside the loop:
WHERE
Pidno = '{$Pidno}'
AND
Uname = '{$_SESSION['username']}
Perhaps you forgot to update $Pidno?
Also, I don't think the following is safe:
"SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'"
You might want to sanitize $_SESSION['username'] before passing it to the query.
Inside your foreach you update $Pidno with the following code
$Pidno = ($item['Pidno']);
Thus you are updating different records in the database. But inside your while loop $Pidno is staying the same the entire time. Thus you are updating the same record over and over. To fix this add the following code at the beginning of the while loop
while ($row = mysql_fetch_assoc($pplresult))
{
//get the correct Pidno for this row
$Pidno = ($row['Pidno']);
$totalprice = $row['Price'] * $row['Pquantity'];
// ... the rest of your while loop