Jump to content

Recommended Posts

Hi,

 

I have tried two different approaches for introducing a list of names to a MySQL query without any success.

The input file ($contig) contains the following:

00001

00002

 

Approach 1:

<?php
$fileArr = file($contig);
print_r($fileArr);

try 
{ 
$bdd = new PDO('mysql:host=localhost;dbname=brain_transcriptome', 'root', 'root'); 
} 
catch (Exception $e) 
{ 
        die('Error : ' . $e->getMessage()); 
} 

$req = $bdd->query('SELECT name FROM crocodylus_contig_info WHERE 1=1 AND name IN ("'.implode('", "', $fileArr).'")') or die(print_r($bdd->errorInfo()));
while ($donnees = $req->fetch(PDO::FETCH_ASSOC))
{
echo ''.$donnees['name'].'<br />';
}
$req->closeCursor();

?>

Approach 2:

<?php
$fileArr = file($contig);
print_r($fileArr);

function arrayToSQLString($fileArr)
{
   return '("'.implode('","',$fileArr).'")';
} 

$params = arrayToSQLString($fileArr);
$query = vsprintf('SELECT name FROM crocodylus_contig_info WHERE 1=1 AND name IN %s',$params);
echo $query;

try 
{ 
$bdd = new PDO('mysql:host=localhost;dbname=brain_transcriptome', 'root', 'root'); 
} 
catch (Exception $e) 
{ 
        die('Error : ' . $e->getMessage()); 
} 

$req = $bdd->query($query) or die(print_r($bdd->errorInfo()));

while ($donnees = $req->fetch(PDO::FETCH_ASSOC))
{
echo ''.$donnees['name'].'<br />';
}
$req->closeCursor();

?>

 

I have check if the input file is well upload and if the query without variables works, and there was no problem.

I would greatly appreciate ANY suggestions!

 

Link to comment
https://forums.phpfreaks.com/topic/199406-import-list-to-sql-query/
Share on other sites

echo the query, check for whitespaces

 

Check the query with a single name

ie

SELECT name FROM crocodylus_contig_info WHERE name IN ("0001")

if that works, try

SELECT name FROM crocodylus_contig_info WHERE name IN ("0001","0002")

 

logically it looks fine so i would have to say its down do the data

I echo the query and indeed there were spaces, so I modified the code to:

function arrayToSQLString($fileArr)
{
return '("'.implode('","',array_map('trim', $fileArr)).'")';
} 

so the query looks like this now:

SELECT name FROM crocodylus_contig_info WHERE name IN ("00001","00002","00003","00004")

 

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.