$sql = "SELECT count(id),soft_name from table_name GROUP BY soft_name";
$d = mysqli_fetch_assoc(mysqli_query($db_name, $sql));
$c = array_shift($d);
The result is always 2, but the database contains more than 3000 items. What could be the problem?
Records are not pulled the way you are pulling for that you have to use similar to following code:
if($result = mysqli_query($db_name, $sql)){
while($d = mysqli_fetch_assoc($result){
echo $d['count'];
}
}
More reference here.
Related
i am trying to insert into multi table after select query return 0 (not found raws) select query working and insert query never done when submite "displayid" and there is no any syntax error
code:
<?php
if ($_POST["displayid"] == TRUE) {
$sqlid = "SELECT * FROM doc1 WHERE idnum ='$pidnum' AND stats='$ok'";
$result = mysqli_query($conn, $sqlid);
if (mysqli_num_rows($result) > 0) {
$sqlup = "UPDATE doc1 SET m_phone='$pm_phone', seen='$dataseen' WHERE idnum ='$pidnum'";
mysqli_query($conn, $sqlup);
$found = 1;
} else {
$found = 0;
$sqlfail = "INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date)
VALUES ('$pfname','$plname','$ptname','$pfuname','$pidnum','$pm_phone','$todaydate')";
$conn->query($sqlfail)
}
}
?>
Use this code:
$sqlfail = "INSERT INTO fail(fname,lname,tname,funame,idnum,m_phone,reg_date)
VALUES ('".$pfname."','".$plname."','".$ptname."','".$pfuname."','".$pidnum."','".$pm_phone."','".$todaydate."')";
make similar changes for update command as well
you actually have one error
$conn->query($sqlfail)
should be
$conn->query($sqlfail);
AND stats='$ok'";
i can't see a variable with this name i think you mean AND stats='ok'";
Well, I'm pretty sure this is just a novice question, so please forgive me for that, but I feel like I'm losing my mind.
I have a simple MySQL rating table and I need to count rows and to sum rates values (int) with PHP PDO
$sql = "SELECT rate FROM rating_table";
$query = $db->query($sql);
$rate_times = count($query->fetchAll()); // it works!
echo '<p>'.$rate_times.'</p>';
$sum_rates = array_sum($query->fetchAll()); // it doesn't work!
echo '<p>'.$sum_rates.'</p>';
Thank you in advance for any suggestion
If I understand you right, all you have to do is to modify your sql request, this will return a single row
sql = "SELECT sum(rate) as rate_sum, count(*) as record_count FROM rating_table";
$query = $db->query($sql);
$row = $query->fetch(PDO::FETCH_ASSOC);
if ($row) {
$sum = $row['rate_sum'];
$count = $row['record_count'];
}
I'm just a beginner and I'm doing a project (a shopping cart). User can add a product to the cart and the id of the product stores in a session. When I use those ids to echo out PRICE from DB it's not working. I'm using PHP & MYSQL. Here is my code
if(count($_SESSION['cart_items'])>0){
// getting the product ids
$nos = "";
foreach($_SESSION['cart_items'] as $no=>$value){
$nos = $nos . $no . ",";
}
// removing the last comma
$nos = rtrim($nos, ',');
//echo $nos; (will display like this INT VALUES 1,2,3,4)
$nos=mysql_real_escape_string($nos);
$site4->DBlogin();
$qry = "SELECT * FROM vendorproducts WHERE product_no IN('.implode(',',$nos).')";
$result = mysql_query($qry);
$row = mysql_fetch_assoc($result);
echo $row['price'];
}
PHP is not recursively embeddable:
$qry = "SELECT * FROM vendorproducts WHERE product_no IN('.implode(',',$nos).')";
^---start of string end of string ---^
Since you're already in a string, .implode(...) is just plain text, NOT executable code.
This means your query is illegal/invalid SQL, and if you had even basic/minimal error checking, would have been told about this:
$result = mysql_query($qry) or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^
I have fixed the issue and thanx MARC for your suggestions.
I was making two mistakes:- IMPLODE input field was not an array. and as Marc said the query was not executable.
Changes made :-
$nos = rtrim($nos, ',');
**$narray = array($nos);**
$fgmembersite4->DBlogin();
$qry = **'SELECT * FROM vendorproducts WHERE product_no IN ('.implode(',',$narray).')'**;
$result = mysql_query($qry) or die(mysql_error());
**while (**$row = mysql_fetch_assoc($result)){
echo $row['price'];}
I want to know how to fetch single row from Oracle in PHP?
Chedck my script-:
I want to fetch single row from ITEM_INFO table & compare that values with variables $sku & $code...Logic I applied which works in Mysql but not working in Oracle...
Each time $sku & $code contains diff. values so I just need to compare them with ITEM_INFO table & if it's matches then update the flag for the same...
$query_fetch = "SELECT ITEM_NAME,SITE_CODE FROM app.ITEM_INFO WHERE ITEM_FLAG = 'N'";
$stmt = oci_parse($conn,$query_fetch);
oci_execute($stmt);
while(($row = oci_fetch_array($stmt, OCI_BOTH)))
{
$ITEM_NAME = ($row["ITEM_NAME"]);
$SITE_CODE = ($row["SITE_CODE"]);
if(($ITEM_NAME === $sku) && ($SITE_CODE === $code))
{
$query_ora_update = "UPDATE app.ITEM_INFO SET ITEM_FLAG= 'Y', LAST_UPDATE_DATE = sysdate WHERE ITEM_NAME = '$sku' AND SITE_CODE = '$code' AND ITEM_FLAG = 'N' ";
$parse_result = oci_parse($conn,$query_ora_update);
$result = oci_execute($parse_result);
oci_commit($conn);
oci_close($conn);
}
}
plz guide me...
Basically, you just have to remove the while loop.
Here's a rewrite of your code applying that change (+ you use too many parenthesis, decreasing your code readability + you should use SQL binding to avoid injection):
$query_ora_update = "UPDATE app.ITEM_INFO SET ITEM_FLAG= 'Y', LAST_UPDATE_DATE = sysdate WHERE ITEM_FLAG = 'N'";
$parse_result = oci_parse($conn, $query_ora_update);
$result = oci_execute($parse_result);
oci_commit($conn);
oci_close($conn);
To fetch a single row in Oracle, add in your where clause the following condition:
ROWNUM = 1
Unfortunately could not understand the rest of your code, did not understand why the "ifs" if you already have the same condition in the where clause of your update.
The Oracle equivalent to mysql_fetch_assoc is oci_fetch_assoc :)
$parsed = ociparse($conn, $sql);
while ($row = oci_fetch_assoc($parsed))
{
// your logic here
}
How would i add the sum of a column in mysql?
Here's my code:
$add = mysql_query("SELECT SUM(rsvp) FROM TABLE_NAME WHERE rsvp > 0;")or die(mysql_error());
when i echo it, it gives me a Resource id #
try this instead
$q = mysql_query("SELECT SUM(rsvp) as sum FROM TABLE_NAME WHERE rsvp > 0") or die(mysql_error());
$row = mysql_fetch_assoc($q);
echo $row['sum'];
I recommend looking up more on how to use PHP and MySQL together possibly from one of these sites:
- http://php.net/manual/en/book.mysql.php
- http://www.youtube.com/user/phpacademy - this one is pretty nooby but it does cover everything from pagination to image uploads and beyond. a good place to start I guess.
mysql_query returns a resource, not a value. You need to use another function, such as mysql_fetch_row to access the value it contained:
$result = mysql_query("SELECT SUM(rsvp) FROM TABLE_NAME WHERE rsvp > 0;") or die(mysql_error());
$row = mysql_fetch_row($result); // get an array containing the value of the first row of the above query
$sum = (int) $row[0]; // get an integer containing the value of the first (and, here, only) item in that row
Yes, and you use mysql_fetch_array to fetch a row from that resource.
$resource = mysql_query( ... );
if ($row = mysql_fetch_array($resource))
{
$add = $row[0];
}
I've found the problem,
Note: Undefined index: sum in D:\xampp\htdocs\demo-shop\cart.php on line 9
$sum_query= "SELECT sum(Prod_Tot) as sum FROM cart WHERE Prod_Tot > 0";
$sum_query_res = mysql_query($sum_query);
$row = mysql_fetch_row($sum_query_res);
echo $row['sum'];
If I put echo $row['0']; instead of echo $row['sum']; then it is ok.
ended up figuring it out.
$add = mysql_query("SELECT SUM(rsvp) FROM TABLE_NAME WHERE rsvp >= 1;")or die(mysql_error());
list ( $rsvp_total ) = mysql_fetch_array($add);
echo $rsvp_total;
This is short and simple try it
$sql = mysql_query("SELECT SUM(rsvp) as sum FROM TABLE_NAME WHERE rsvp > 0") or die(mysql_error());
$record = mysql_fetch_assoc($sql);
echo $record['sum'];