Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know php at all so sorry for a simple question - I've got the project started by another person so now I'm trying to finish it.
The problem is - I'm making an android app for which I can't make a change without changing a php and this language I don't know.
here's this part :
function getPlaces(){
$result = array();
$sql = "SELECT id, name, short_description, photo_list, selected, recommended, isTOP FROM place
WHERE id IN(SELECT id_place FROM rubric_place WHERE id_rubric IN(SELECT id FROM rubric WHERE name = '".$_REQUEST["rubric"]."')) ORDER BY isTOP DESC";
getConnect();
$query = mysql_query($sql);
if(!$query){
error100();
}else {
$result['code'] = 200;
for ($i = 0; $i < mysql_num_rows($query); $i++) {
$row = mysql_fetch_assoc($query);
$result["places"][$i] = $row;
$sql = "SELECT * FROM rubric WHERE id IN(SELECT id_rubric FROM rubric_place WHERE id_place = ".$row[id].")";
$queryModule = mysql_query($sql);
if($queryModule){
for ($k = 0; $k < mysql_num_rows($queryModule); $k++) {
$rowModule = mysql_fetch_assoc($queryModule);
$result["places"][$i]["rubrics"][$k] = $rowModule;
}
}
$sql = "SELECT SUM(rating)/COUNT(rating) AS rating FROM comment WHERE id_place = ".$row[id];
$queryModule = mysql_query($sql);
if($queryModule){
$rowModule = mysql_fetch_assoc($queryModule);
$result["places"][$i]["rating"] = $rowModule[rating];
}
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
exit();
}
}
what I need is to make another function that returns in $result a value of "length" of places. I know I can find out length on the other end - in Android's app result, but this particular function will be changed so it will return by 20 results only so I need another function that returns length so plz help
To count the number of characters in a json you can do:
$string = json_encode($result, JSON_UNESCAPED_UNICODE);
$length = strlen($string);
You have to first put the json into a variable to count it, before outputting it.
To count the items in an array before you turn it into a json, you can do:
$length = count($array);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I currently have a database table setup like so:
| id | thing_id | value
The "thing_id" relates to a unique ID within the same table.
I'm trying to create a function (in PHP) that will run a query against the MySQL database, dump that information into an array, and if "thing_id" is NOT NULL it will loop back and run the query again, and add the information on to the existing array. This will continue until the "thing_id" is NULL.
How can I go about doing this? Btw, I'm designing this on top of WordPress.
The following below works, but I feel like there is some better way to do this. How can I simplify this and make it not hog resources? Here is what I have:
$related_thingsSql = "
SELECT $thingsDB.value, $thingsDB.thing_id, $thingsDB.id
FROM $thingsDB
WHERE $thingsDB.id = '$related_thing_id'
";
$related_things = $wpdb->get_results( "$related_thingsSql" );
foreach ($related_things as $related_thing) {
$related_thing_name[] = $related_thing->value;
$related_thing_id2[] = $related_thing->id;
$related_thing_id = $related_thing->thing_id;
}
while ($related_thing_id != NULL) {
$related_thingsSql2 = "
SELECT $thingsDB.value, $thingsDB.thing_id, $thingsDB.id
FROM $thingsDB
WHERE $thingsDB.id = '$related_thing_id'
";
$related_things = $wpdb->get_results( "$related_thingsSql2" );
foreach ($related_things as $related_thing) {
array_unshift($related_thing_name, $related_thing->value);
array_unshift($related_thing_id2, $related_thing->id);
$related_thing_id = $related_thing->thing_id;
if (empty($related_thing->thing_id)) {
$related_thing_id = NULL;
}
}
}
$related_things_length = count($related_thing_name);
for ($x = 0; $x < $related_things_length; $x++) {
echo ' > ' . $related_thing_name[$x] . '';
}
I figured out a way that works. I feel like this way is really sloppy, but it works. If anyone has any suggestions on how to improve this, feel free.
$related_thingsSql = "
SELECT $thingsDB.value, $thingsDB.thing_id, $thingsDB.id
FROM $thingsDB
WHERE $thingsDB.id = '$related_thing_id'
";
$related_things = $wpdb->get_results( "$related_thingsSql" );
foreach ($related_things as $related_thing) {
$related_thing_name[] = $related_thing->value;
$related_thing_id2[] = $related_thing->id;
$related_thing_id = $related_thing->thing_id;
}
while ($related_thing_id != NULL) {
$related_thingsSql2 = "
SELECT $thingsDB.value, $thingsDB.thing_id, $thingsDB.id
FROM $thingsDB
WHERE $thingsDB.id = '$related_thing_id'
";
$related_things = $wpdb->get_results( "$related_thingsSql2" );
foreach ($related_things as $related_thing) {
array_unshift($related_thing_name, $related_thing->value);
array_unshift($related_thing_id2, $related_thing->id);
$related_thing_id = $related_thing->thing_id;
if (empty($related_thing->thing_id)) {
$related_thing_id = NULL;
}
}
}
$related_things_length = count($related_thing_name);
for ($x = 0; $x < $related_things_length; $x++) {
echo ' > ' . $related_thing_name[$x] . '';
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a database of 100,000 contacts.
I have a multiple select box which I would like to use to select contacts to add to a specific mailing list.
My problem is that I am currently storing the data for the mailing list in a serialized array format (PHP) in MySQL.
When I select over a certain number of contacts, something seems to break (I assume not enough memory) and does not update the array.
Is there a best way to store a large array in MySQL and is there a best way to keep memory usage in a PHP array() low?
Code Example
if(isset($_POST['add'])) {
$name = $core->EscapeString($_POST['name']);
$desc = $core->EscapeString($_POST['desc']);
foreach($_POST['addSelect'] as $null => $id) {
if(!in_array($id, $recipientArray)) {
$recipientArray[] = $id;
}
}
$contacts->updateML($lid, $name, $desc, serialize($recipientArray));
}
else if(isset($_POST['rm'])) {
$name = $core->EscapeString($_POST['name']);
$desc = $core->EscapeString($_POST['desc']);
foreach($recipientArray as $null => $id) {
foreach($_POST['rmSelect'] as $null1 => $id1) {
if($id == $id1) {
unset($recipientArray[$null]);
}
}
}
$contacts->updateML($lid, $name, $desc, serialize($recipientArray));
}
UpdateML Function
//Class 3, Function 16
function updateML($lid = '', $name = '', $desc = '', $recip = '') {
global $MySQLi;
$query = "UPDATE `mailing_lists` SET `name` = '".$name."', `desc` = '".$desc."', `recipients` = '".$recip."' WHERE `list_id` = '".$lid."' LIMIT 1";
$commit = $MySQLi->query($query);
if($commit == false) {
die("Issues with the database were detected. Please email peter#domain.com quoting error code: <strong>CLASS3/16.1</strong>.");
}
else
{
return true;
}
}
You need 2 tables. One for Mailing List Definition, one for recipients. In recipients table you'd need to have a foreign key relating the record to appropriate mailing list.
so mailing_lists table looks like this:
ml_id
name
desc
and recipients looks like this:
r_id
email
ml_id
First add the mailing list to the database:
INSERT INTO mailing_lists SET name = 'my first list', desc = 'mailsareus'
When you add new recipients just add a new row to recipients table:
INSERT INTO recipients SET email = 'xxx#mail.com', ml_id = 1
If you need to get all the recipients and definition from a mailing list just use a join
SELECT * FROM mailing_lists JOIN recipients ON recipients.ml_id = mailing_lists.ml_id
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Im trying to find the best killer (most kills) out of a kills database.
Put the killerId of $important into an array, and compare it with the rest of the killers. Find the best killer with the weapon $important.
How do I do that?
$array = array();
$index = 0;
while($mData = $q->fetch_assoc())
{
$index++;
$arr = explode('with ', $mData['killText']);
$unimportant = array(" (in paintball)", " (in event)");
$important = str_replace($unimportant, "", $arr[1]);
if(empty($important)) { $important = "Suicide"; }
$array[$important]['Kills']++;
$array[$important]['Gun'] = $important;
$query2 = $mysql->query("SELECT * FROM `kills` WHERE `killText` LIKE '%$important%' AND `killerID` = '". $mData['killerID'] ."'") or die($mysql->error);
while($kData = $query2->fetch_assoc())
{
// put the killerId of $important into an array, and compare it with the rest of the killers. Find the best killer with the weapon $important
}
}
A GROUP BY will do the trick:
"SELECT killerID, COUNT(*) FROM kills WHERE killText LIKE '%$important%' GROUP BY killerID;"
You ca just fetch "killerID" and got the killer with most kills of weapon $important:
$kData = $query2->fetch_assoc();
$kData['killerID'];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a PHP function that runs a SELECT Query in SQL:
if(!function_exists("SelectQuery")) {
function SelectQuery ($sql) {
global $conn;
$SelectQuery = mysql_query($sql,$conn);
return $NumRows=mysql_num_rows($SelectQuery);
$SelectQuery_Results=array();
while($SelectQuery_Row = mysql_fetch_array($SelectQuery)) {
$SelectQuery_Results[] = $SelectQuery_Row;
}
return $SelectQuery_Results;
}
}
then i am calling it here:
$sql="SELECT * from tickets where ticketnumber = '".$_GET["seq"]."' ";
$ticket = SelectQuery($sql);
foreach($ticket as $ticket2) {
}
rather than using a foreach loop what else could i use as my query will only be returning one row and i dont want to put my whole page within a loop
i tried just removing the foreach loop but that didnt work
Just access your value as $ticket[0].
A function cannot return two values for a single call. So, remove
return $NumRows=mysql_num_rows($SelectQuery);
You can check the array size for the number of rows in result.
If you want to echo out the result the following code will be ok!
if( sizeof($ticket) > 1 ){
foreach($ticket as $ticket2){
for($i=0; $i<sizeof($ticket2)/2; $i++)
echo "[" . $ticket2[$i] . "]";
echo "<br />";
}
}
$ticket is an integer see :
return $NumRows=mysql_num_rows($SelectQuery);
So you juste have to use $ticket.
If you want the row, remove this useless return in the SelectQuery function and use $ticket[0] for the first and only row.
Change the function with this :
function SelectQuery ($sql) {
global $conn;
$SelectQuery = mysql_query($sql,$conn);
$NumRows=mysql_num_rows($SelectQuery);
$SelectQuery_Results=array();
if ($numRows>1) {
while($SelectQuery_Row = mysql_fetch_array($SelectQuery)) {
$SelectQuery_Results[] = $SelectQuery_Row;
}
}
else $SelectQuery_Results = mysql_fetch_array($SelectQuery);
return $SelectQuery_Results;
}
Then use :
$sql="SELECT * from tickets where ticketnumber = '".$_GET["seq"]."' ";
$ticket = SelectQuery($sql);
if (is_array($ticket)) { foreach loop; }
else { use directly ticket['attribute'] }
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've been trying to figure out the best way to return an order from a database. I've come up with this, and it's working the way I want (I think) it gives me the results I'm looking for but I wanted to know if its correct or if there is a better way.
<?php
$conn = mysql_connect('', '', '');
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbs, $conn);
$Order_ID = $_POST['Order_ID'];
//$Order_ID = '1001';
$queryOrderHead = "SELECT * FROM Orders WHERE Order_ID = '$Order_ID' ";
$queryOrderLines = "SELECT *
FROM Order_LineDetails
WHERE Order_LineDetails.Order_ID = '$Order_ID'
";
if ($queryRunHead = mysql_query($queryOrderHead)){
while ($info_HEAD = mysql_fetch_array($queryRunHead))
{
$OrderID_HEAD = $info_HEAD['Order_ID'];
$User_ID_HEAD = $info_HEAD['User_ID'];
$Customer_ID_HEAD = $info_HEAD['Customer_ID'];
echo $OrderID_HEAD.' '.$User_ID_HEAD.' '.$Customer_ID_HEAD.'<br>';
}
$queryRunLines = mysql_query($queryOrderLines);
while ($info = mysql_fetch_array($queryRunLines))
{
$OrderID = $info['Order_ID'];
$OrderLineID = $info['OrderLineItem_ID'];
echo $OrderID.' '.$OrderLineID.'<br>';
}
} else {
echo mysql_error();
}
mysql_close($conn);
?>
So what it does, is it uses the Order_ID val from the $_POST and runs the first query then on success it uses the same Order_ID and loops the second query and gets all the Order_LineDetails from a different table.
Other than the mysql_real_escape() tags....
Any pointers or ideas???
Any pointers or ideas???
There's nothing wrong with selecting the order first, then its items. However, you'd benefit from organizing the data into an array structure as well as following better naming conventions (both for variables and database schema):
$orderId = $_POST['order_id'];
// order_id should be an INT, so no quotes.
// Also look into parameterized queries with PDO as the mysql_* functions are archaic!
$sqlOrder = "SELECT *
FROM orders
WHERE order_id = ".mysql_real_escape_string($orderId);
$order = array();
if ($resOrder = mysql_query($sqlOrder)) {
if ($rowOrder = mysql_fetch_array($resOrder)) {
$order = $rowOrder;
// echo $rowOrder['order_id'].' '.$rowOrder['user_id'].' '.$rowOrder['customer_id']."<br />\n";
$sqlOrderLines = "SELECT *
FROM order_lines
WHERE order_lines.order_id = ".mysql_real_escape_string($orderId);
if ($resOrderLines = mysql_query($sqlOrderLines)) {
$order['order_lines'] = array();
while ($rowOrderLines = mysql_fetch_array($resOrderLines)) {
$order['order_lines'][] = $rowOrderLines;
// echo $rowOrderLines['order_id'].' '.$rowOrderLines['order_line_id']."<br />\n";
}
}
} else {
echo 'Order not found'.
}
} else {
echo mysql_error();
}
// debug
print_r($order);