I am having a small problem with my PHP MySQL Select. The function is inside of a PHP class. Here is the error I get:
Warning: mysql_fetch_array() expects parameter 1 to be resource,
integer given in C:\xampp\htdocs\include\database.php on line 59
Warning: extract() expects parameter 1 to be array, null given in
C:\xampp\htdocs\include\database.php on line 59
The function just simply updates the database to show what browser and OS they visited the site with. The function is called from another file that is called by an AJAX call that uses POST to send the data about the OS and browser that was gathered from a Javascript file. It only fails if there is an entry of the IP address already in the database. If there is no IP Address entry in the database it succeeds in creating one.
Here is my code:
function addStat($browser, $os){
$IE = 0; $Firefox = 0; $Safari = 0; $Opera = 0; $Chrome = 0; $otherb = 0;
$Windows = 0; $Linux = 0; $Mac = 0; $Android = 0; $iOS = 0; $otheros = 0;
$ql = 0; $totalVisits = 0;
$ip = ip2long($_SERVER['REMOTE_ADDR']);
$q1 = mysql_query("SELECT * FROM " . DB_STATS . " WHERE ip='$ip'", $this->connection);
if (mysql_num_rows($q1)==0){
$browser = mysql_real_escape_string($browser);
$os = mysql_real_escape_string($os);
switch($browser){
case "Internet Explorer":
$IE += 1;
break;
case "Firefox":
$Firefox += 1;
break;
case "Safari":
$Safari += 1;
break;
case "Opera":
$Opera += 1;
break;
case "Chrome":
$Chrome += 1;
break;
default:
$otherb += 1;
break;
}
switch($os){
case "Windows":
$Windows += 1;
break;
case "Mac OS X":
$Mac += 1;
break;
case "Linux":
$Linux += 1;
break;
case "Android":
$Android += 1;
break;
case "iOS":
$iOS += 1;
break;
default:
$otheros += 1;
break;
}
$q = $this->query("INSERT INTO " . DB_STATS . " VALUES (null, '$ip', '$Chrome', '$IE', '$Firefox', '$Opera', '$Safari', '$otherb', '$Windows', '$Mac', '$Linux', '$Android' , '$iOS' , '$otheros', 1)");
if ($q == true){
return(1);
}
else{
return(0);
}
}
else if (mysql_num_rows($q1)==1){
extract(mysql_fetch_array($ql));
switch($browser){
case "Internet Explorer":
$IE += 1;
break;
case "Firefox":
$Firefox += 1;
break;
case "Safari":
$Safari += 1;
break;
case "Opera":
$Opera += 1;
break;
case "Chrome":
$Chrome += 1;
break;
default:
$otherb += 1;
break;
}
switch($os){
case "Windows":
$Windows += 1;
break;
case "Mac OS X":
$Mac += 1;
break;
case "Linux":
$Linux += 1;
break;
case "Android":
$Android += 1;
break;
case "iOS":
$iOS += 1;
break;
default:
$otheros += 1;
break;
}
$totalVisits += 1;
$q = $this->query("UPDATE " . DB_STATS . " set Chrome='$Chrome', IE='$IE', Firefox='$Firefox', Opera='$Opera', Safari='$Safari', otherb='$otherb', Windows='$Windows', Mac='$Mac', Linux='$Linux', Android='$Android' , iOS='$iOS' , otheros='$otheros', totalVisits='$totalVisits'");
if ($q == true){
return(1);
}
else{
return(0);
}
}
else{
return(-1);
}
}
I hope everything made sense and that someone will help.
I see it now -- you used $ql (lower case L) when you intend to use $q1. Let this be a lesson against using very short variable names or very similar names.
// $ql was initialized to 0
$ql = 0; $totalVisits = 0;
// $q1 holds the result resource
extract(mysql_fetch_array($q1));
It is not advisable to call extract() on the output of mysql_fetch_array() unless you also specify the second parameter MYSQL_ASSOC as the fetch type. By default it returns both numeric and associative indices for each column.
extract(mysql_fetch_array($q1, MYSQL_ASSOC));
// Or better
extract(mysql_fetch_assoc($q1));
In general, I would probably advise against using extract() in most any situation, since it results in numerous variables dumped into the global namespace, in particular when you have done SELECT * without being specific about which columns are selected. Better to access them via their array:
$row = mysql_fetch_assoc($q1);
echo $row['browser'];
Related
Hy,
I got switch case inside function when but when i call it, i got error Undefined Variable and i don't know why (i use PHP 8)
private function getIDFromBrand() {
switch ($this->brand) {
case "Niky":
$id = 1;
break;
case "Pumo":
$id = 4;
break;
case "Coke":
if ($this->typecoke== 0) {
$id = 2;
} else {
if ($this->typecoke== 1) {
$id = 3;
}
}
break;
case "Tomato":
$id = 5;
break;
case "Riles":
$id = 6;
break;
case "TEST":
$id = 7;
break;
}
return $id; // Error Undefined variable $id
}
When i declare $id at the top of my function, like
$id = null
or
$id = 0
The switch doesn't update it, so it will return null or 0, it will return the declared value.
Your switch statement has no default branch, so if $this->brand is, say, "Stack Overflow", it will not run any of the statements, and $id will never be set.
See the PHP manual for the switch statement:
A special case is the default case. This case matches anything that wasn't matched by the other cases.
Similarly, if $this->brand is "Coke", but $this->typecoke is, say, 42, it will not match either of the conditions in that branch.
switch ($this->brand) {
case "Niky":
$id = 1;
break;
case "Pumo":
$id = 4;
break;
case "Coke":
if ($this->typecoke== 0) {
$id = 2;
} elseif ($this->typecoke== 1) {
$id = 3;
} else {
$id = -1; // WAS PREVIOUSLY NOT SET
}
break;
case "Tomato":
$id = 5;
break;
case "Riles":
$id = 6;
break;
case "TEST":
$id = 7;
break;
default:
$id = -1; // WAS PREVIOUSLY NOT SET
break;
}
I'm working on a homework assignment while learning PHP for the first time. Everything is working, except for my switch statement. My professor is asking-
"Modify index.php
Get the selected value from the "calculate" radio button you created
Add a switch statement to set values for:
only the average calculation if the user selected the "average" radio button
only the total calculation if the user selected the "total" radio button
both the average and total if the user selected the "both" button."
I'd like to provide my entire index.php file in case it's important to see everything-
<?php
//set default values to be used when page first loads
$scores = array();
$scores[0] = 70;
$scores[1] = 80;
$scores[2] = 90;
$scores_string = '';
$score_total = 0;
$score_average = 0;
$max_rolls = 0;
$average_rolls = 0;
$score_total_f = '';
$score_average_f = '';
//take action based on variable in POST array
$action = filter_input(INPUT_POST, 'action');
switch ($action) {
case 'process_scores':
$scores = $_POST['scores'];
// validate the scores
$is_valid = true;
for ($i = 0; $i < count($scores); $i++) {
if (empty($scores[$i]) || !is_numeric($scores[$i])) {
$scores_string = 'You must enter three valid numbers for scores.';
$is_valid = false;
break;
}
}
if (!$is_valid) {
break;
}
// process the scores
$score_total = 0;
foreach ($scores as $s) {
$scores_string .= $s . '|';
$score_total += $s;
}
$scores_string = substr($scores_string, 0, strlen($scores_string)-1);
// calculate the average
$score_average = $score_total / count($scores);
// format the total and average
$score_total_f = number_format($score_total, 2);
$score_average_f = number_format($score_average, 2);
$calculate = filter_input(INPUT_POST, 'calculate');
switch($calculate) {
case "average":
$message_average = $score_average_f;
break;
case "total":
$message_total = $score_total_f;
break;
case "both":
$message_average = $score_average_f;
$message_total = $score_total_f;
break;
default: die("Invalid type");
}
break;
case 'process_rolls':
$number_to_roll = filter_input(INPUT_POST, 'number_to_roll',
FILTER_VALIDATE_INT);
$total = 0;
$max_rolls = -INF;
for ($count = 0; $count < 10000; $count++) {
$rolls = 1;
while (mt_rand(1, 6) != $number_to_roll) {
$rolls++;
}
$total += $rolls;
$max_rolls = max($rolls, $max_rolls);
}
$average_rolls = $total / $count;
break;
}
include 'loop_tester.php';
?>
Also, here is part of the other file where I had to create radio buttons-
<h3>What do you want to do?</h3>
<input type="radio" name="calculate" value="average" checked> Average<br>
<input type="radio" name="calculate" value="total"> Total<br>
<input type="radio" name="calculate" value="both"> Both<br>
<label>Scores:</label>
<span><?php echo htmlspecialchars($scores_string); ?></span><br>
<label>Score Total:</label>
<span><?php echo $message_total; ?></span><br>
<label>Average Score:</label>
<span><?php echo $message_average; ?></span><br>
</form>
Thank you!
Again, everything is working fine when I test in XAMPP, just not the switch statement. I get no output of any kind.
EDIT: Disregard my original answer, I tested out your original syntax and it seems to work fine. Sorry, that was a mistake on my part, though I'd still say the new code is more elegant.
There seems to be an issue with a misplaced break; - Here is a full working code for to 'process_scores' case:
case 'process_scores':
$scores = $_POST['scores'];
// validate the scores
$is_valid = true;
for ($i = 0; $i < count($scores); $i++) {
if (empty($scores[$i]) || !is_numeric($scores[$i])) {
$scores_string = 'You must enter three valid numbers for scores.';
$is_valid = false;
break;
}
}
if (!$is_valid) {
break;
}
// process the scores
$score_total = 0;
foreach ($scores as $s) {
$scores_string .= $s . '|';
$score_total += $s;
}
$scores_string = substr($scores_string, 0, strlen($scores_string)-1);
// calculate the average
$score_average = $score_total / count($scores);
// format the total and average
$score_total_f = number_format($score_total, 2);
$score_average_f = number_format($score_average, 2);
$calculate = filter_input(INPUT_POST, 'calculate');
$score_average_f = number_format($score_average, 2);
$score_total_f = number_format($score_total, 2);
switch($calculate) {
case "average":
echo "Average: " . $score_average_f;
break;
case "total":
echo "Total: " . $score_total_f;
break;
case "both":
echo "Average: " . $score_average_f . "<br />";
echo "Total: " . $score_total_f;
break;
default: die("Invalid type");
}
break;
I'm not sure about other the other part of your code, but I tested this and got the intended results. If you still see nothing, check what's in your $_POST variables. Also as a general advice for debugging: in a situation like this, just go through your code and echo stuff out inside and outside of every loop or function you believe your code should reach, to see where it gets derailed. It may not sound too professional, but it sure gets the job done.
I'm doing the same exercise. You need to define your variables before all the code runs.
$scores_string = '';
$score_total = 0;
$score_average = 0;
$max_rolls = 0;
$average_rolls = 0;
$message_average = 0;
$message_total = 0;
After I defined variables, $message_average and $message_total, everything worked fine.
if (isset($_POST["submit"])){
$oride='';
$count = "25";
$origin = $_POST["origin"];
$destinataion = $_POST["destination"];
$oride = ($destination = $_POST["destination"] - $origin= $_POST["origin"]);
switch (true) {
case ($count<="0"):
echo "invalid";
break;
case ($count==="15"):
echo $count;
break;
case ($count==="16"):
$total = $count + "1";
echo $total;
break;
default:
echo "hello";
} }
The code will compute 1st then execute switch depending on what is the result of the computation. I tried if else but it will be too long because the case will go up to 130.
You must use the var $count in switch statement and the constant in case this way
switch ($count) {
case "0" :
echo "invalid";
break;
case "15":
echo $count;
break;
case "16":
$total = $count + "1";
echo $total;
break;
default:
echo "hello";
break;
}
You have to provide an expression to the switch statement, while the case statements are just "versions" of the result of that expression. The only thing you can NOT do directly is the "<= 0" expression, but you can work around it:
if (isset($_POST["submit"])){
$oride='';
$count = "25";
$origin = $_POST["origin"];
$destinataion = $_POST["destination"];
$oride = ($destination = $_POST["destination"] - $origin= $_POST["origin"]);
// --- normalize $count:
$count = $count <= 0 ? 0 : $count;
// use $count as expression:
switch ($count) {
case 0:
echo "invalid";
break;
case "15":
echo $count;
break;
case "16":
$total = $count + "1";
echo $total;
break;
default:
echo "hello";
} }
So, i was challenged by a coworker to build a random poker hand generator. At the time, all I had was my phone that I could run php with.
So, after some tinkering and lots of searching, i came up with this:
<?php
$cards;
$card = array();
function GetCard($getSuit, $getNumber){
for($s=0; $s < 4; $s++){
for($n=0; $n < 13; $n++){
if($s == 0){
//Clubs
$suits[$s] = "-C";
} else if($s == 1){
//Hearts
$suits[$s] = "-H";
} else if($s == 2){
//Spades
$suits[$s] = "-S";
} else if($s == 3){
//Diamonds
$suits[$s] = "-D";
}
if($n == 0){
$num[$n] = "A";
} else if($n == 10){
$num[$n] = "J";
} else if($n == 11){
$num[$n] = "Q";
} else if($n == 12){
$num[$n] = "K";
} else {
$num[$n] = $n+1;
}
$cards[$s][$n] = $num[$n].$suits[$s];
}
}
return $cards[$getSuit][$getNumber];
}
function GetRandomPokerHand(){
$i = 0;
while($i < 5){
mt_srand();
$rs = mt_rand(0,3);
$rn = mt_rand(0,12);
$randomCard = GetCard($rs,$rn);
if(!in_array($randomCard,$card)){
$card[i] = GetCard($rs,$rn);
echo $card[i];
echo " ";
$i++;
}else{
echo " found ";
}
}
}
GetRandomPokerHand();
?>
I get the hand, however... every so often in_array fails and I get the same card twice. i added the "else" statement with "found" to see if it was finding the duplicate at all. It did and echos "found", and occasionally it STILL displays a duplicate.
So I decided to try the same code (well roughly the same) when i got home with c++: (Using "switch" instead of "if" was because of preference and switch statements are too much trouble on a phone)
#include <iostream>
#include <string>
#include <vector>
#include <time.h>
using namespace std;
string suits[4];
string num[13];
string cards[4][13];
string GetCard(int getSuit, int getNumber){
for (int s = 0; s < 4; s++){
for (int n = 0; n < 13; n++){
switch (s){
case 0:
suits[s] = "-C";
break;
case 1:
suits[s] = "-H";
break;
case 2:
suits[s] = "-S";
break;
case 3:
suits[s] = "-D";
break;
}
switch (n){
case 0:
num[n] = "A";
break;
case 10:
num[n] = "J";
break;
case 11:
num[n] = "Q";
break;
case 12:
num[n] = "K";
break;
default:
num[n] = to_string(n + 1);
break;
}
cards[s][n] = num[n] + suits[s];
}
}
string card = { cards[getSuit][getNumber] };
return card;
}
bool in_array(const string &value, const vector<string> &array){
return find(array.begin(), array.end(), value) != array.end();
}
void GetRanddomPokerHand(){
int hand = 0;
srand(time(NULL));
while (hand < 5){
int suit = rand() % 4;
int value = rand() % 13;
string randomCard = GetCard(suit, value);
vector<string> card = { "", "", "", "", "" };
if (!in_array(randomCard, card)){
card[hand] = randomCard;
cout << card[hand] << endl;
hand++;
}else{
cout << "found";
}
}
}
int main(){
GetRanddomPokerHand();
char stop;
cin >> stop; // yes, i know this isn't necessary in VE2013
return 0;
}
Same problem. I can't seem to figure out why the duplicates are being printed in either case.
Any ideas?
In the c++ one you have this line inside your loop
vector<string> card = { "", "", "", "", "" };
That is creating a fresh blank hand each time, so it never will have duplicates.
$card[i] need to be $card[$i]
your $card array always has only 1 element which is last card cause of that mistake ;)
Ok, so I'm trying to write something which will analyze the US Presidential candidates.
I'm trying to add all of the values up on a given day - I'm part of the way through that.
Anyway, $romlinearray - I want to have two values inside, the date, and the value for a given day. I want to insert them inside the if ($romneydiff == 0) { block.
What I am having a problem with is generating an array with a key name (i.e. I want to be able to do $romlinearray["value"]) and have it pull a value, but the value isn't going to be inserted until later.
How can I create an array with a key, but without a value as of yet?
$resultromney = mysql_query("SELECT category, value, timestamp from results where name='".$chartname2."'");
if (!$resultromney) {
die('Invalid query, please contact administrator');
}
$posromney = 0;
$negromney = 0;
$dateromney = "1/1/2012";
$romray = array();
while ($twitromney = mysql_fetch_assoc($resultromney))
{
$romneytime = intval($twitromney["timestamp"]);
$romneydate = date('m/d/Y', $romneytime);
$romlinearray = array(); // THIS IS WHERE THE PROBLEM IS
$romneydiff = date_diff($dateromney, $romneytime);
$PLromney = 0;
$NLromney = 0;
$totalromney = 0;
if ($romneydiff == 0) {
switch ($twitromney["category"]) {
case "composed":
$PLromney = $PLromney + $twitromney["value"];
break;
case "elated":
$PLromney = $PLromney + $twitromney["value"];
break;
case "clearheaded":
$PLromney = $PLromney + $twitromney["value"];
break;
case "agreeable":
$PLromney = $PLromney + $twitromney["value"];
break;
case "energetic":
$PLromney = $PLromney + $twitromney["value"];
break;
case "unsure":
$NLromney = $NLromney + $twitromney["value"];
break;
case "tired":
$NLromney = $NLromney + $twitromney["value"];
break;
case "depressed":
$NLromney = $NLromney + $twitromney["value"];
break;
case "guilty":
$NLromney = $NLromney + $twitromney["value"];
break;
case "confused":
$NLromney = $NLromney + $twitromney["value"];
break;
case "anxious":
$NLromney = $NLromney + $twitromney["value"];
break;
case "hostile":
$NLromney = $NLromney + $twitromney["value"];
break;
case "confident":
$PLromney = $PLromney + $twitromney["value"];
break;
}
$totalromney = $PLromney - $NLromney;
$romlinearray["value"] = $totalromney + $romlinearray["value"];
}