array_fill with word results - php

$sql23 = "SELECT * FROM map1pokemon ORDER BY RAND() LIMIT 1;";
$result23 = mysql_query($sql23) or die(mysql_error());
$battle_get23 = mysql_fetch_array($result23);
$sql2 = "SELECT * FROM pokemon WHERE name='".$battle_get23['pokemon']."'";
$result2 = mysql_query($sql2) or die(mysql_error());
$battle_get2 = mysql_fetch_array($result2);
$filler = "SELECT * FROM pokemon WHERE name='Charmander'";
$filler2 = mysql_query($filler) or die(mysql_error());
$filler3 = mysql_fetch_array($filler2);
$pokemon1 = array_fill(1,10,"".$filler3."");
$pokemon2 = array_fill(1,2,"".$battle_get2."");
$pokemon3 = array_merge($pokemon1, $pokemon2);
shuffle($pokemon3);
$pokemon4 = array_shift($pokemon3);
$pic2= mysql_real_escape_string($pokemon4['pic']);
$pic = strip_tags($pic2);
echo "<center>";
echo '<img src="pokemon/'.$pic.'" border=0><p></p>' ;
The queries at the top grab the information about each pokemon, I then want to use the array_fill and array_merge to make it harder for one of the results to appear the, but right now it doesn't show the result of the pokemon after the mixing of the pokemon, I believe that I'm doing the mixing of the pokemon wrong, but have no idea why?
Does anyone see what I'm doing wrong here?

this line seems to be useless :
$pic2= mysql_real_escape_string($pokemon4['pic']);
Then, it is logical you do not display what you want as you do not store what you want. Let's review your code :
$filler = "SELECT * FROM pokemon WHERE name='Charmander'";
$filler2 = mysql_query($filler) or die(mysql_error());
$filler3 = mysql_fetch_array($filler2);
$filter3 is clearly an array which contains all of your table columns.
$pokemon1 = array_fill(1,10,"".$filler3."");
What? you concatenate an array with a string? Don't use this concate trick it will not bring you good results. $pokemon1 = array_fill(1,10,$filler3); is ok. A little greedy but ok.
One thing you can do is using random generator, it will be easier to use, less greedy and quicker.
$rand = rand(12);
if($rand <=10){
$result = $filler3;
}else{
$result = $battle;
}
echo $result['name'];

Related

MYSQL query using a set of values (values stored from a session array) not working

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'];}

While returning no results

I've got the following query in an existing script - but it's not always returning a value even though it should based off what's in the database. There are plenty of things in the database it SHOULD be grabbing - they are there.
Don't see anything wrong with it - but I barely do this anymore :) See anything?
$query = "SELECT id FROM xtags WHERE tag_id = '$tagid' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query = "SELECT * FROM xtable WHERE id = '$row[id]'";
$result = mysql_query($query) or die(mysql_error());
$row2 = mysql_fetch_assoc($result);
echo $row2[title];
}
$result is being used inside the loop and outside, try making a new variable inside and not reusing the outside one.
You're reusing the $result variable inside the loop which overwrites the value for use in the while condition. Use a different name for $query and $result inside the loop.
I don't know if is ok, but you are using $result twice, one before the "while" and another inside the "while".
I would personally split the string and the variable $row.
Why not use var_dump() to see $row and the other variables ???
I don't understand what you are trying to do actually, but try this:
$query = "SELECT id FROM xtags WHERE tag_id = '".$tagid."' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query2 = "SELECT * FROM xtable WHERE id = '".intval($row[id])."'";
$result2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($result2);
echo $row2[title];
}
Problem solved - did a join statement.

autocomplete query to execute another mysql query if rows = 0 on first one using last keyword entered

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

Add the SUM of a Column in mySQL & PHP

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'];

How Can I make this MySQL Count query more efficient?

//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.

Categories