I am currently trying to output the value of an array using a for loop.
I have tried outputting the query that is running in the loop (Resulting in an echo of 24 queries)
1
$number_of_beams = 24;
for ($i = 0; $i < $number_of_beams; $i++)
{
$query = "SELECT fitacf_data.time,
SUM(fitacf_data.num_pts) AS point_total
FROM
fitacf_data
WHERE abbrev = '" . $radar_array[0]['radar_abbrev'] ."'
AND fitacf_data.beam = '" . $i . "'
GROUP BY fitacf_data.time";
$result = pg_query($query) or die('Error: ' . pg_last_error());
while ($row = pg_fetch_array($result)) {
$beam_total_array[] = $row;
}
echo $i
echo $beam_total_array[0]['point_total'] . "<br><br>";
}
If I hard code $i to any value from 0-23 echo $beam_total_array[0]['point_total']; outputs the correct value 24 times.
ie:
2
for ($i = 0; $i < $number_of_beams; $i++)
{
$query = "SELECT fitacf_data.time,
SUM(fitacf_data.num_pts) AS point_total
FROM
fitacf_data
WHERE abbrev = '" . $radar_array[0]['radar_abbrev'] ."'
AND fitacf_data.beam = '5'
GROUP BY fitacf_data.time";
$result = pg_query($query) or die('Error: ' . pg_last_error());
while ($row = pg_fetch_array($result)) {
$beam_total_array[] = $row;
}
echo $i
echo $beam_total_array[0]['point_total'] . "<br><br>";
}
$i is returning 0-23 as expected.
If I run the code as shown in #1 using the $i variable the output is 0 for the 24 times.
What am I missing here?
The problem is that you're declaring $i as a string, which has a value of 0. Remove the double quotes around $i: AND fitacf_data.beam = ' . $i . '
Related
the following sql query works perfectly in phpAdmin.
SELECT response_text FROM statement_responses WHERE response_code = "s1_r1"
it doesn't work without the speech marks around response_code value.
I'm attempting to use the value of response_text in php.
for ($x = 1; $x <= 9; $x++) {
$user_response = ${"statement_" . $x . "_response"};
$sql = "SELECT response_text FROM statement_responses WHERE response_code = \"$user_response\"";
$result = mysqli_query($sql);
$value = mysqli_fetch_object($result);
echo $user_response . "<br>";
echo $sql . "<br>";
echo $result . "<br>";
echo $value . "<br>";
}
The echoes allow me to see what each of the variables contains.
I get the following:
s1_r3 (the value of $user_response)
SELECT response_text FROM statement_responses WHERE response_code = "s1_r3" (the value of $sql - which is identical to the phpAdmin query that works.)
There are no values echoed for $result or $value.
What am I doing wrong, please? Why am I not getting the values from the database into my php code?
The first parameter for the mysqli_query should be the database connection created with mysqli_connect. Similarily, the first parameter for the mysqli_fetch_object, should be the result set identifier returned by mysqli_query.
It is a good practice to check the return values from the functions you call.
firstly, thank you for all of the responses. The following code works - i.e. it returns the value contained in 'response_text' column with the selected row identified by 'response_code' for use as the value of $response_text.
for ($x = 1; $x <= 9; $x++) {
$user_response_pre = ${"statement_" . $x . "_response"};
$user_response = "\"'" . $user_response_pre . "'\"";
$sql = "SELECT response_text FROM statement_responses WHERE response_code = $user_response";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$response_text = $row[0];
${"statement_" . $x . "_complete"} .= $response_text;
}
you can write like this
for ($x = 1; $x <= 9; $x++) {
$user_response = ${"statement_" . $x . "_response"};
$sql = 'SELECT response_text FROM statement_responses WHERE response_code = "'.$user_response.'"';
$result = mysqli_query($sql);
$value = mysqli_fetch_object($result);
echo $user_response . "<br>";
echo $sql . "<br>";
echo $result . "<br>";
echo $value . "<br>";
}
I'm trying to write some code that gets random prices from a database until a specific amount is reached. I want it to add the name and price to an array and then print it at the end with $total_items, but it's only showing the last item. What's wrong?
$total = 0;
while ( $total <= $amount ) {
$query = "SELECT name, price FROM table ORDER BY RAND() LIMIT 1";
$run_query = mysql_query($query);
$total_items = array();
while($row = mysql_fetch_assoc($run_query)) {
$total_items[] = $row;
echo $row['name'] . ' / ' . $row['price'] . "<br>";
$total = $total + $row['price'];
}
}
echo "<br>" . Total Amount: ".$total;
print_r($total_items);
variable $total_items is defined in the while loop, so it is truncated in every while loop, and $total_items will only keep the last result.
try to put it above while ( $total <= $amount )
Please define $total_items outside the while loop. And double quote properly to print the output.
$total = 0;
$total_items = array();
while ( $total <= $amount ){
$query = "SELECT name, price FROM table ORDER BY RAND() LIMIT 1";
$run_query = mysql_query($query);
while($row = mysql_fetch_assoc($run_query)) {
$total_items[] = $row;
echo $row['name'] . ' / ' . $row['price'] . "<br>";
$total = $total + $row['price'];
}
} echo '<pre>';echo "Total Amount: ".$total; print_r($total_items);`
my name's Tony. I am trying to find the way to change this in SQL:
1/ $sql = "select..from..where column_1 = ".$val_1." and column_2 = ".$val_2." and ...and column_n = ".$val_n." ";
------> $sql = "select..from..where [ column,".$val." ] ";
with column run into 1 -> n; $val run into 1 -> n.
2/ Example: If ($val_2 == '') -> $sql = "...where column_1 = ".$val_1." and column_3 = ".$val_3." and ... column_n = ".$val_n." ";
Can i do it ?
Thank you very much.
Make an array and fill it with values which are not empty:
$sql_where_clause = array();
for ($i = 1; $i <= n; $i++)
{
$val_to_test = 'val_' . $i;
if (!empty($$val_to_test))
$sql_where_clause[] = "column_" . $i . " = '" . $$val_to_test . "'";
}
$sql = "select..from..where " . implode(" AND ", $sql_where_clause);
I am trying to solve a problem where I need to execute a query on the database within a do while loop and keep on adding to the resultset. However once its finished running all the queries and I then start processing the result set php gives the error
Warning: mysql_fetch_array() expects parameter 1 to be resource,
string given in
The idea is it executes a query with a limit of 0,2 then executes again with a limit of 2,2 and so on until the numbers of rows returned is empty. I know the best solution would be to do a count and work out from there but this is not an option as the database is too large.
Below is the code that I currently have
$tempResult = "";
$i = 0;
$result = null;
do
{
if (is_resource($tempResult))
{
echo 'Cleared Result <br />';
mysql_free_result($tempResult);
}
echo 'Count: ' . $i . '<br />';
$query = "select * from table_test LIMIT $i, 2";
$tempResult = mysql_query($query) or die ("MySQL Error: " . mysql_error());
echo 'Temp Result: ' . $tempResult .' <br />';
$i = $i + 2;
$result .= $tempResult;
} while (mysql_num_rows($tempResult) > 0);
while ($myrow = mysql_fetch_array($result))
{
echo $myrow['col1'] . ' + ' . $myrow['col2'] .
' + ' . $myrow['col3'] . ' <br />';
}
Thanks for any help you can provide.
Why dont you use a result array? For example something like this:
$tempResult = "";
$i = 0;
$resultArray = array();
$result = null;
do
{
if (is_resource($tempResult))
{
echo 'Cleared Result <br />';
mysql_free_result($tempResult);
}
echo 'Count: ' . $i . '<br />';
$query = "select * from table_test LIMIT $i, 2";
$tempResult = mysql_query($query) or die ("MySQL Error: " . mysql_error());
$resultArray = merge($resultArray, mysql_fetch_array($tempResult));
echo 'Temp Result: ' . $tempResult .' <br />';
$i = $i + 2;
} while (mysql_num_rows($tempResult) > 0);
while ($resultArray as $myRow)
{
echo $myrow['col1'] . ' + ' . $myrow['col2'] .
' + ' . $myrow['col3'] . ' <br />';
}
Well, from such an unusual question we can only guess the right answer.
My bet:
Your goal is to do some calculations on the data stored in the table.
And you are going to do it in the PHP script instead of the proper way of calculating using database facilities.
So, it's better you would do all the calculations in one query.
Anyway,
It is always a good practice to ask a core question, a root problem, instead of a question related to some unusual way you choose to solve it.
Once you ask this one, you will get complete and professional answer.
Why not just print the results in the first loop?
$i = 0;
do
{
$query = "SELECT * FROM table_test LIMIT $i, 2";
$result = mysql_query($query) or die("MySQL Error: " . mysql_error());
while ($myrow = mysql_fetch_array($result))
{
echo $myrow['col1'] . ' + ' . $myrow['col2'] . ' + ' . $myrow['col3'] . ' <br />';
}
$i = $i + mysql_affected_rows();
} while (mysql_affected_rows() > 1);
echo 'Count: ' . $i . '<br />';
But really, this method just seems like a way to add additional overhead to fetching rows from the database.
How could I add a number starting from 1 to show the leader by total games in this script?
<?php
$result = mysql_query("SELECT gamertag, tgames FROM leader ORDER BY tgames DESC");
while ($row = mysql_fetch_assoc($result)) {
echo "number? ".$row['gamertag']." ".$row['tgames']."<br>\n";
}
?>
You could simply add a variable that increases each time.
<?php
$result = mysql_query("SELECT gamertag, tgames FROM leader ORDER BY tgames DESC");
$index = 1;
while ($row = mysql_fetch_assoc($result)) {
echo $index++ . " " . $row['gamertag'] . " " . $row['tgames'] . "<br>\n";
}
?>
(...)
$i = 1;
while($row = mysql_fetch_assoc($result)) {
echo $i . " " .$row['gamertag']." ".$row['tgames']."<br>\n";
$i++;
}