TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   using variables to run different queries (http://www.talkphp.com/absolute-beginners/2980-using-variables-run-different-queries.html)

sarmenhb 06-19-2008 05:57 PM

using variables to run different queries
 
here is an excerpt of my code. i have navigation buttons and when each button and each button has a href of group.php?num=# where # is the group number i want to query. the problem is i did this and it wont work. for some reason the variable wont be passed. any ideas?

Code:

if(isset($num)) {
if($num = 1) {
       
        $viewrec = mysql_query("select * from tbl_clone where grp = '1' order by priority");
}

if($num = 2) {
       
        $viewrec = mysql_query("select * from tbl_clone where grp = '2' order by priority");
}

if($num = 3) {
       
        $viewrec = mysql_query("select * from tbl_clone where grp = '3' order by priority");
}

if($num = 4) {
       
        $viewrec = mysql_query("select * from tbl_clone where grp = '4' order by priority");
}

if($num = 5) {
       
        $viewrec = mysql_query("select * from tbl_clone where grp = '5' order by priority");
}

if($num = 6) {
       
        $viewrec = mysql_query("select * from tbl_clone where grp = '6' order by priority");
}

if($num = 7) {
       
        $viewrec = mysql_query("select * from tbl_clone where grp = '7' order by priority");
}

if($num = 8) {
       
        $viewrec = mysql_query("select * from tbl_clone where grp = '8' order by priority");
}

if($num = 9) {
       
        $viewrec = mysql_query("select * from tbl_clone where grp = '9' order by priority");
}


if($num = 10) {
       
        $viewrec = mysql_query("select * from tbl_clone where grp = '10' order by priority");
}

if($num = 0) {
       
        $viewrec = mysql_query("select * from tbl_clone where grp = 'notset' order by priority");
}
}
else { die('page not set'); }


sarmenhb 06-19-2008 06:29 PM

nm, i found out i had to put == instead of =

Seraskier 06-19-2008 07:38 PM

heres a cleaner one.
Im writing it right here, so no testing.

Code:

//use some way to get the $i; ifs, using the date function, anyway you choose.
$i=rand(1,6)*2;

$mysql=mysql_query("select * from tbl_clone where grp = '.$i.' order by priority");
if(!mysql_query($mysql))
  { die('Could not process request.'); }


delayedinsanity 06-19-2008 08:22 PM

PHP Code:

if (isset($num)) {
    
$q sprintf("SELECT * FROM `tbl_clone` WHERE grp = '%d'"mysql_real_escape_string($num));
    if (!
mysql_query($q)) echo 'Invalid group ID';


-m

sketchMedia 06-19-2008 08:29 PM

just a tip, if you have a need for a conditional with that many different options depending on a single variable a switch is better suited:

PHP Code:

<?php
switch($num)
{
  case 
1:
    
$viewrec mysql_query("select * from tbl_clone where grp = '1' order by priority");
    break;
  case 
2:
    
$viewrec mysql_query("select * from tbl_clone where grp = '2' order by priority");
    break;
   ...

  default:
    die(
'Page not set');
}

PHP: switch - Manual

Kalle 06-19-2008 10:37 PM

My version:

PHP Code:

<?php
    
if(!isset($num) || !is_numeric($num) || $num 0)
    {
        
$num 'notset';
    }
    else
    {
        
/** Force integer */
        
$num = (integer) $num;
    }

    
$query = @mysql_query('SELECT * FROM ... WHERE ... = $num');

    if(!
$query || !@mysql_num_rows($query))
    {
        
/** No page set or no results, halt execution */
    
}

    
/** Continue */
?>

Hope it helps ;)


All times are GMT. The time now is 01:11 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0