Tasos
Tasos

Reputation: 37

SQL switch statement

( I don't know if this is correct i am just asking if possible and how )

So what i want to do is when page is index.php select something, and when jobs.php select something else if possible.

I am trying to execute this SQL statement, but that does not work.

$sql_q;
$path=$_SERVER['PHP_SELF'];
$page=basename($path);
switch("$page") 
{ 

case 'index.php': 
     $sql_q = 'SELECT * FROM jobs WHERE title LIKE '%news%'" '; 
     break; 
case 'jobs.php': 
     $sql_q = 'SELECT * FROM jobs WHERE title LIKE '%jobs%'" '; 
     break;
}

And here:

$getquery = mysql_query("$sql_q LIMIT $p_num, $per_page");

Is that possible somehow ?

Thanks.

Upvotes: 0

Views: 59

Answers (1)

motanelu
motanelu

Reputation: 4025

You can do it like this:

switch ($page) {
    case 'index.php':
        $toSelect = '%news%';
        break;
    case 'jobs.php':
        $toSelect = '%jobs%';
        break;
}

$query = sprintf(
    'SELECT * FROM `jobs` WHERE `title` LIKE "%s" LIMIT %s, %s',
    $toSelect,
    $p_num,
    $per_page
);

Also, consider using mysqli* functions, as mysql* are deprecated.

Upvotes: 1

Related Questions