1

I have a string array value but I don't know how to use it in a sql statement.

I've tryied using implode but nothing happens.

this is my code:

$select_tbl=mysqli_query($conn, "SELECT * FROM users WHERE firstname='$firstname' AND lastname='$lastname'");
while($fetch=mysqli_fetch_object($select_tbl)) {
    $r = $fetch->lessons;
    $i = explode(",",$r);
    foreach ( $i as $item ) {
        $string .= $item.',';
        echo $string;
    }
}


$_SESSION['library'] = $_GET['library'];
$_SESSION['lessonID'] = $_GET['lessonID'];
$_SESSION['department'] = $_GET['department'];
$dept = $_SESSION['dept'];
$library = $_SESSION['library'];

$checkID = mysqli_real_escape_string($conn, $_GET["lessonID"]);
$checkdept = mysqli_real_escape_string($conn, $_GET["department"]);
$department = mysqli_real_escape_string($conn, $_SESSION['dept']);
$sql = "SELECT * FROM gallery 
        WHERE lessonID = '$checkID' AND departmentg='$checkdept' AND library='$library' AND titleGallery='$string'";
$stmt = mysqli_stmt_init($conn);

I want to get the string array of the foreach statement in my code so that I can use it for the WHERE clause in my SELECT statement.

8
  • what is the output of $i Commented Feb 12, 2019 at 4:21
  • Side comment - don’t use echo $string; inside the foreach() loop as it will echo it $i number of times. Commented Feb 12, 2019 at 4:25
  • @blackbird Paradise Forest,Water Drop,pdf. But when I implement it on my sql statement, nothing happens. Commented Feb 12, 2019 at 4:26
  • Please mention $r & $i values bcz we don`t know the input coming from = $fetch->lessons; Commented Feb 12, 2019 at 4:27
  • @blackbird I just want to see if it really echo out the value from my database. But thank you for your advice sir, I'll keep that in mind. Commented Feb 12, 2019 at 4:28

1 Answer 1

2

Here you are doing string to array & array to string conversion:

$r = $fetch->lessons; //string
$i = explode(",",$r); //array
foreach ( $i as $item ) { //array parsing
  $string .= $item.','; //string
}

instead of that you can use:

//declare string array before while loop
$string_array = array();
$string_array = array_merge($string_array,explode(',',$fetch->lessons)); //Final Array

You can use in sql query like:

$sql = "SELECT * FROM gallery WHERE lessonID = '$checkID' AND departmentg='$checkdept' AND library='$library' AND titleGallery IN (".implode(',',$string_array).")";
Sign up to request clarification or add additional context in comments.

6 Comments

This is the error handler sir: if (!mysqli_stmt_prepare($stmt, $sql)) {echo "SQL statement failed!";}
SELECT * FROM gallery WHERE lessonID = '1' AND departmentg='opex' AND library='video' AND titleGallery IN (Paradise Forest,Water Drop,pdf,)
This error is coming bcz of the space between the string & also bcz of the last ',' to remove that change my code.. where i created on string_array & merging that array with new lessons & finally imploding into the $sql. Bcz of implode every array index value converting to string...
I have an error sir, line 43 is "Declare $string_array = array();": Parse error: syntax error, unexpected '$string_array' (T_VARIABLE), expecting '(' in C:\xampp\htdocs\project\admin\lesson1_1_user.php on line 43.
$string_array = array(); before the while loop & don`t use 'Declare' word
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.