Jump to content

Small problem + Array

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Jerryn

Jerryn

    Newbie

  • Members
  • PipPip
  • 16 posts
I'm trying to create a script where the code checks for data inside the selected table and if there is some data that doesn't match values set by array $GOODVALUE, then it would show an error.

I've never really had to use arrays in my previous scripts so I can't figure this one out.

The way I made this script is also bad I think, totally not what I wanted to make it but previous tests all failed. :sleep: (learning PHP still)


<?php


/* these should be the only values that should exist in the selected table. */

$GOODVALUE = array(

  "11",

  "12",

  "14",

  "21",

  "22",

  "23",

  "24",

  "31",

  "32",

  "150"

);


/*

 now this is the query, it would select data from Table that have values in GOODVALUE array.

 i know this isn't the proper way to use array but I've never really used arrays before.

*/

$test  = mysql_query("SELECT * FROM MyDB.Table WHERE ID='$GOODVALUE'");


mysql_query($test);


  if(!$test){

    echo "something is wrong <br />";

  }

  else{

    echo "everything is ok<br />";

  }


?>

I want the query to return an error even if there are normal data with selected values but also some data with totally random values for ID column.

I hope this explanation is good enough, please ask if you didn't understand some point.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
It would be just a straight forward iterative algorithm. This code is purely in example, I have not tested its syntax.
$successful = 0;
$test = mysql_query("SELECT ID FROM MyDB.Table"); 
if( $test === false || mysql_num_rows($test) == 0 )  {
    // An error 
}

while( $cur_row = mysql_fetch_assoc($test) ) {
    if( in_array($cur_row['id'], $GOODVALUE) === false ) {
        break; // An invalid value has occurred
    }
    $successful++;
}

if( $successful != count($GOODVALUE) ) {
  // Less than all of the good values were found
}

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Jerryn

Jerryn

    Newbie

  • Members
  • PipPip
  • 16 posts
Thanks Alexander, got it working now. ;)

#4
rySch

rySch

    Newbie

  • Members
  • Pip
  • 8 posts
You could also do it the other way around

$test  = mysql_query("SELECT * FROM MyDB.Table WHERE'");// store all the good options

if(!in_array('value', $test)){

   echo "not a good value";

} 



#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

rySch said:

You could also do it the other way around
$test  = mysql_query("SELECT * FROM MyDB.Table WHERE'");// store all the good options
if(!in_array('value', $test)){
   echo "not a good value";
} 

I am not sure that makes sense, the query is not called, there is no condition for the WHERE clause, and also it does not satisfy if there are less than all valid options (although not specified precisely by the user)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
rySch

rySch

    Newbie

  • Members
  • Pip
  • 8 posts
sorry my mistake the query should read
SELECT * FROM MyDB.Table

the idea is to get all the good values, put them into an array for later use, then if the a value submitted by a use isnt in that array return th ere




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users