Jump to content

Recommended Posts

i want to use values out of a GET variable in a query like this

 

SELECT * FROM `test` WHERE event IN('performance') and createdby IN('admin', 'jordo49')

 

so say the url may look like this

 

mysite.com/api.php?category=performance&createdby=admin+jordo49+moderator

 

i want to get the createdby varialbes(admin, jordo49, and moderator) and then seperate them with apostrophes and commas so it will work in my select query. How would i go about doing this?

 

so my code would look somethin like this

<?php
$uservars = $_GET['createdby'];

//code to format with apostrophes and commas
$users = part i need help with

$sql = "SELECT * FROM `test` WHERE event IN('performance') and createdby IN(&users)";

//run query, blah dee blah
?>

Link to comment
https://forums.phpfreaks.com/topic/172076-array-help-i-think/
Share on other sites

Try this:

 

$createdby_arr = explode(" ", $_GET['createdby']);
$createdby_sql = "('" . implode("','", $createdby_arr) . "')";
$sql = "SELECT * FROM `test` WHERE event IN('performance') and createdby IN ($createdby_sql)";

 

I haven't dealt with escaping of the input strings - you should do this for security.  You can escape each element of the array before calling implode().

Link to comment
https://forums.phpfreaks.com/topic/172076-array-help-i-think/#findComment-907315
Share on other sites

This will result in a sql error. Your braces are invalid after IN clause

Try this:

 

$createdby_arr = explode(" ", $_GET['createdby']);
$createdby_sql = "('" . implode("','", $createdby_arr) . "')";
$sql = "SELECT * FROM `test` WHERE event IN('performance') and createdby IN ($createdby_sql)";

 

I haven't dealt with escaping of the input strings - you should do this for security.  You can escape each element of the array before calling implode().

 

Fix

<?php
$createdby_sql = "'".implode("','", explode(" ", $_GET['createdby']))."'";
$sql = "SELECT * FROM test WHERE event IN('performance') and createdby IN ($createdby_sql)";
?>

Link to comment
https://forums.phpfreaks.com/topic/172076-array-help-i-think/#findComment-907403
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.