Click to See Complete Forum and Search --> : PHP array in sql query


cachopa
02-25-2008, 08:27 AM
Hello!

I'm developing a website in PHP and MySQL database.

I have an inline query in my page and one of the columns is an array.

I need this query to select data dependending on the value that the variable has.

Like this:

$arrayA = array("News","Events");

$news = $arrayA[0];
$events = $arrayA[1];
(...)
vsprintf("SELECT * FROM table WHERE var='%s'",$arrayA);
(...)

I need to select the data that it's in the row of the table 'table', depending if var=News or/and var=Events...

This code is just giving the same id, wich is the id of the row "News"...

Can anyone help me?

chazzy
02-25-2008, 08:45 AM
so you're saying you're putting an array into the database? this is not recommended. you should use a reference table to keep the multiple values in. what type is the column

cachopa
02-25-2008, 08:52 AM
Oh no, not an array into the database. The array has the values of a column.. diferent rows of the same column...

chazzy
02-25-2008, 09:38 AM
I don't think there's a direct way to do what you're looking to do with PHP's API.

First, try to use this function, it will wrap each "value" in single quotes needed for mysql


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

And change your code to create the query to this one:

$params = arrayToSQLString($arrayA);
vsprintf("SELECT * FROM table WHERE var in %s",$params);


That should do it.

yashmistrey
11-17-2009, 07:47 AM
Dear friends !

Some error ocuring in m phpmyadmin like

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$dbtables = array( "chatgroup" => array( "groupid" => "int NOT NULL auto_in' at line 1
-------------------------------------------------------------------
i copied these code from dbinfo.php

$dbtables = array(
"chatgroup" => array(
"groupid" => "int NOT NULL auto_increment PRIMARY KEY",
"vcemail" => "varchar(64)",
"vclocalname" => "varchar(64) NOT NULL",
"vccommonname" => "varchar(64) NOT NULL",
"vclocaldescription" => "varchar(1024) NOT NULL",
"vccommondescription" => "varchar(1024) NOT NULL",
),



Regards
Yashmistrey