Hi, Here's what I have. Let's say an egg counter application. I have two columns in a database, current and alert. 'current' will be the current amount the user has. 'alert' will be an amount that if the 'current' amount is less than or equal too it will echo 'something'. I was thinking of using two arrays and comparing them. However I'm not very good with for or while loops. Is there any way of doing this or is there a better way. Thanks
If I have more than one entry that the current number is less than the alert number can I say, And then will I be able to list the egg types that are lower than the alert number?
yes you can get 'egg_type' column where alert is less than or equal to current SELECT 'egg_type' FROM table WHERE alert <= current;
How do I echo the results then. I have; // Making the query for displaying $q2 = "SELECT `item` FROM `main` WHERE `alert` <= `current` ORDER BY `id` ASC"; $r2 = @mysqli_query($dbc, $q2); // Running the query // Counting the MySQL rows $num2 = mysqli_num_rows($r2); $row2 = mysqli_fetch_array($r2, MYSQLI_ASSOC); // Creating the alert_echo funcion function alert_echo () { foreach ($row2 as $value) { echo "$value <br>"; } } PHP: I then call the function. This however doesn't return anything.
$row2 does not exist in the alert_echo function. Look at this page for information on variable scope: http://www.php.net/manual/en/language.variables.scope.php You will need to pass $row2 to the function like: alert_echo($row2) Additionally never use the @ infront of mysqli_query, mysqli_query will return an error only if the query is invalid or connection lost and other cases which should be caught in your statement.
Some advice: 1) STOP making extra variables for nothing; waste of memory 2) STOP fetching the entire result set. waste of memory and performance draining garbage. You need to pass it, pass the mysqli_result 3) STOP using the procedural version, it introduces extra overhead and really should never have been allowed to exist in the first place. 4) STOP using such needlessly cryptic variable names. Try describing things with your vars. 5) on your output it should be just dumping out a bunch of '1' -- as you are fetching associative meaning the result is an array of arrays indexed as 'item'. ... and one question, why are you putting the output in a function? You use the term 'alert' -- that raises some worries about where/when you are trying to use it. $itemAlertResult = $dbc->query(' SELECT item FROM main WHERE alert <= current ORDER BY id ASC '); // no clue why you were putting the output in a function while ($value = $itemAlertResult->fetch_assoc()) { echo $value['item'], '<br />'; } Code (markup):
Then either put the query in the function, or make the result global and access it as such... I'd just go with the former... Well, unless you're showing it more than once per page. (which is usually a bad idea anyways). Or again, pass the result to the function or the access the result as global, instead of using ::fetchAll.