//get the current member count
$sql = ("SELECT count(member_id) as total_members from exp_members");
$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows != 0) {
while($row = mysql_fetch_array($result)) {
$total_members = $row['total_members'];
}
}
//get list of products
$sql = ("SELECT m_field_id, m_field_label from exp_member_fields where m_field_name like 'cf_member_ap_%' order by m_field_id asc");
$result = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows != 0) {
while($row = mysql_fetch_array($result)) {
$m_field_id = $row['m_field_id'];
$m_field_label = $row['m_field_label'];
$sql2 = ("SELECT count(m_field_id_".$m_field_id.") as count from exp_member_data where m_field_id_".$m_field_id." = 'y'");
$result2 = mysql_query($sql2) or die(mysql_error());
$num_rows2 = mysql_num_rows($result2);
if ($num_rows2 != 0) {
while($row2 = mysql_fetch_array($result2)) {
$p = ($row2['count']/$total_members)*100;
$n = $row2['count'];
$out .= '<tr><td>'.$m_field_label.'</td><td>'.number_format($p,1).'%</td><td>'.$n.'</td></tr>';
}
}
}
}
It's easier to help if you can describe in non-code terms what you're trying to accomplish. But one indicator of a problem is seeing a php loop on rows from one query with another query executing for each row.
There are ways to query for subtotals. But it would be easier to explain if you can explain the goal a bit.
count query would always return 1 row, so you don't need the loop
$sql = ("SELECT count(member_id) as total_members from exp_members");
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$total_members = $row['total_members'];
Other than that i am not sure how you can make it better. You can do the same for both of your count queries.
As these are straight forward queries, any bottleneck i guess now would be on the MySQL end
The first COUNT query ("get the current member count") should execute almost instantaneously.
The second query ("get list of products") may be slow depending on your indexes. You are querying on m_field_name and then ordering on m_field_id so you may need a combined index of the two.
The third query, which is executed repeatedly (once for each product), is querying on m_field_id_* (i.e. any of a number of possible fields), so you should probably make sure they are indexed.
In summary, you need to a) figure out which query is running slow, b) index things that need to be indexed, and c) combine queries if possible.
Related
The following script was initially intended to select a row at random, which it could do fine, however, I an unable to figure out how to amend it to select data from a second table using a column ID called "nid" which is the same in both tables:
$conn = mysqli_connect($host,$username,$password, $database) or die(mysql_error ());
$total_rows = 5;
$selected_row = mt_rand(0, $total_rows);
$query="SELECT * FROM `node` LIMIT $selected_row, 1;";
$result=$conn->query($query);
while($row = $result->fetch_assoc()) {
$node=$row["nid"];
echo $row["title"];
$query2="SELECT * FROM `field_data_body` LIMIT $node, 1;";
$result2=$conn->query($query2);
while($row = $result->fetch_assoc()) {
echo $row["body_value"];
}
Admittedly, the above is not really close to working, but I was hardpressed to find an example of what I was after.
These tables are in the database for a Drupal site, so the title field and the body_value fields are in two different tables; ultimately, I would like to echo a result that is a matching set of title and body_value for a randomly selected node.
Speaking of this script specifically, I want to use the nid to find the corresponding row for the second table.
Is this possible?
The bit that works, selecting the data I want from a single table is in the following format:
$total_rows = 5;
$selected_row = mt_rand(0, $total_rows);
$query="SELECT * FROM `field_data_body` LIMIT $selected_row, 1;";
$result=$conn->query($query);
while($row = $result->fetch_assoc()) {
echo $row["body_value"];
}
?>
UPDATE:
At the suggestion of a commenter, I used a join, and ended up with:
$conn = mysqli_connect($host,$username,$password, $database) or die(mysql_error ());
$total_rows = 5;
$selected_row = mt_rand(0, $total_rows);
//Use the result in your limit.
$query="SELECT a.nid, a.title, b.entity_id, b.body_value
FROM node a, field_data_body b
WHERE a.nid = b.entity_id LIMIT $selected_row, 1;";
$result=$conn->query($query);
while($row = $result->fetch_assoc()) {
echo $row["title"];
echo " | ";
echo $row["body_value"];
}
which worked perfectly.
for future reference since you have solved your problem, you need to close the previous connection before starting a new one, or free the results
$conn = mysqli_connect($host,$username,$password, $database) or die(mysql_error ());
$total_rows = 5;
$selected_row = mt_rand(0, $total_rows);
$query="SELECT * FROM `node` LIMIT $selected_row, 1;";
$result=$conn->query($query);
while($row = $result->fetch_assoc()) {
$node=$row["nid"];
echo $row["title"];
$result->close(); // close $result
$query2="SELECT * FROM `field_data_body` LIMIT $node, 1;";
$result2=$conn->query($query2);
while($row = $result->fetch_assoc()) {
echo $row["body_value"];
}
$result2->close(); // close $result2
i also notice you haven't closed the connection on the UPDATE you posted either which will lead you to run into the same problem again later on.
If this is just about to select a single record, you do not have to define the LIMIT, instead you can use the randomly generated value to select the row by using it in WHERE!
You can just join the two tables. Beside that, your code has a potential problem. It relies on the existence of at least 5 records. You can avoid that by putting the randomization into the query. The code will then be
$conn = mysqli_connect($host,$username,$password, $database) or die(mysql_error());
$query = "SELECT n.*, b.* FROM `node` AS n LEFT JOIN `field_data_body` AS b ON n.nid=b.nid ORDER BY REVERSE(RAND()) LIMIT 0, 1;";
$result = $conn->query($query);
$row = $result->fetch_assoc();
$result->close();
echo $row["title"];
echo $row["body_value"];
The REVERSE() function makes the result from RAND() more random. You may omit it, if you want.
function countBrand($brand_id, $brand_name) {
$sql = "SELECT brand FROM coupons WHERE expire >= CURRENT_DATE AND brand='".$brand_id."'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo '<li>'.$brand_name.'</li>';
}
function brandCount() {
$sql = "SELECT DISTINCT brand,brand_id,brand_name FROM coupons,coupons_brand WHERE brand=brand_id AND expire >= CURRENT_DATE ORDER BY brand_name";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$num = mysql_num_rows($result);
echo '<h3>'.$num.' Brands</h3>';
echo '<ul>';
$i = 0;
while ($i < $num) {
$brand_id = mysql_result($result, $i, "brand_id");
$brand_name = mysql_result($result, $i, "brand_name");
countBrand($brand_id, $brand_name);
$i++;
}
echo '</ul>';
}
It works perfectly and gives me the results I am looking for. I am not as strong with sql statements as I would like to be. Is there a way I could do this that would be more efficient, it seems very slow.
Basically, it counts how many brands have coupons, then coupons how many coupons each brand has..
I also, on the same page, do this for categories. There are a few thousand categories and maybe 20,000 coupons.
there are a few php optimisations you could do, but they probably wont save you much time, compared to adding an index to mysql on the correct columns
i have commented some php optimisations below, maybe of interest to you anyway
given the simple nature of the functions, it is not necessary to have 2 functions, and that would save the timecost of calling countBrand(), (although it is a pretty minimal time saving)
function countBrand($brand_id, $brand_name) {
$sql = "SELECT brand FROM coupons WHERE expire >= CURRENT_DATE AND brand='".$brand_id."'";
$result = mysql_query($sql) || die (mysql_error()); // always check for errors
list($brand) = mysql_fetch_row($result);
// fetch row, returns a more concise array then mysql_fetch_array
// $row = mysql_fetch_array($result);
// use commas rather then . when concatenating echo statements
// dots force concatenation before output
echo '<li>',$brand,'</li>';
}
function brandCount() {
$sql = "SELECT DISTINCT brand,brand_id,brand_name FROM coupons,coupons_brand WHERE brand=brand_id AND expire >= CURRENT_DATE ORDER BY brand_name";
$result = mysql_query($sql) || die(mysql_error()); // always check for errors
// $row = mysql_fetch_array($result); // not sure why this is needed
$num = mysql_num_rows($result);
// use commas rather then . when concatenating echo statements
// dots force concatenation before output
echo '<h3>',$num,' Brands</h3>';
echo '<ul>';
// fetch all cols at once, rather then using lots of separate calls to mysql_result
// use mysql_fetch_row as it returns just the ordered values (vs mysql_fetch_assoc, and mysql_fetch_array)
//
while(list($brand, $brand_id, $brand_name) == mysql_fetch_row($result)) {
countBrand($brand_id, $brand_name);
}
// replaced with while loop above
// $i = 0;
// while ($i < $num) {
// $brand_id = mysql_result($result, $i, "brand_id");
// $brand_name = mysql_result($result, $i, "brand_name");
// countBrand($brand_id, $brand_name);
// $i++;
}
echo '</ul>';
}
those enhancements will only give you a minor speed increase
the biggest speed increase you will get is if you call the database less.
currently you select each brand, and then go back and count each brand individually
without knowing the structure of you tables this sql is difficult for me to write, so it is a guess, but it should point you in the right direction
SELECT brand, brand_id, brand_name, COUNT(*) as brandcount
FROM coupons
JOIN coupons_brand ON brand=brand_id
WHERE expire >= CURRENT_DATE
GROUP BY brand, brand_id, brand_name
ORDER BY brand_name
mysql_fetch_array($result,MYSQL_ASSOC);
SELECT SQL_CACHE brand FROM
Profiling query
I'm working on a type of spellcheck, autosuggest. I have two dictionaries, one with phrases, and one that should kick in for basic spelling suggestions. Right now I have this on the server side:
$q = strtolower($_GET["q"]);
$q= mysql_real_escape_string($q);
if (!$q) return;
$sql = ("SELECT headings FROM dictionary WHERE headings LIKE '$q%' LIMIT 3");
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
$auto = $rs['headings'];
echo "$auto\n";
}
if (mysql_num_rows(mysql_query("SELECT headings FROM dictionary WHERE headings LIKE '$q%' LIMIT 3")) == 0){
$res = mysql_query("SELECT spelling FROM spellcheck WHERE spelling LIKE '$s%' LIMIT 3");
while($result = mysql_fetch_array($res)) {
$spell = $result['spelling'];
echo "$spell\n";
}
}
basically, $s should equal where the first query left off. So if the query was : hello wor. $s would be wor and it would suggest "world" from the mysql_query: $res. I would have to deal with multiple spaces between words too.
Use mysql_affected_rows() to get affected rows of last executed query...
refer http://php.net/manual/en/function.mysql-affected-rows.php
I don't know if i am doing it right, this is what I got:
while($row = mysqli_fetch_assoc($result)) {
$items = mysqli_num_rows($row);
}
It always sets $items = to 1 for some reason.
Here is my mysqli_query...
$top10_query = "SELECT * FROM users WHERE userid='$userid'";
$result = mysqli_query($cxn, $top10_query) or die("Couldn't execute query.");
$row = mysqli_fetch_assoc($result);
Well, $row only contains one row so....
$items = mysqli_num_rows($result)
should give you the correct number of items
Anyway, why are you doing that in a loop? The number of rows is constant...
I use this
$query = "SELECT * FROM info ORDER BY id DESC limit 10";
$result = #mysql_query( $query );
$row = mysql_fetch_array($result);
print_r($row);
but it gets just the last row
mysql_fetch_array does not fetch an array of rows.
It fetches an array of columns from a single row.
To get all rows, you have to run it in a loop:
$query = "SELECT * FROM info ORDER BY id DESC limit 10";
$result = #mysql_query( $query );
while ($row = mysql_fetch_array($result))
print_r($row);
The query is correct. If you seem to be only getting one row, it's a factor external to the query causing it: either you only have one row in the table, or your application logic is hosed so that it looks like you only have one row.
Edit: Yeah, now that you've posted your code, we can see that it's that your application logic is hosed. Try this:
$result = mysql_query($query);
$rows = array();
while($row = mysql_fetch_array($result))
$rows[] = $row;