Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] SQL statement with array?


[SOLVED] SQL statement with array?

Reply
  #1 (permalink)  
Old 10-30-08, 02:56 AM
hakeem hakeem is offline
Newbie Coder
 
Join Date: Sep 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] SQL statement with array?

hello

I need a little help on using an array data variable to query mysql db. I have a list of an array that holds string from A-Z so I want to use it in search for the initial letters on my table, though am using LIKE '$array[]%' for the query. I dont have idea on how to go using the array variable in SQL statement.

I will be glad if any further information is needed.

thank you in anticipation.
Reply With Quote
  #2 (permalink)  
Old 10-30-08, 07:21 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I am not sure I understand what you have in your array.
I am imagining two possibilities.
An array with 26 elements, each element containing a letter of the alphabet.
Or an array with x number of elements. with one of the element containing "A-Z".

The first possibility is easy to implement.
You can use regex and the RLIKE operator, no external array needed.
Example:
PHP Code:

"SELECT * FROM table WHERE column RLIKE '^[a-zA-Z]'" 

In order to help with the second possibility, I would need to know more about the array and how the data in the array is going to be used in the query.
__________________
Jerry Broughton
Reply With Quote
  #3 (permalink)  
Old 10-31-08, 12:41 AM
hakeem hakeem is offline
Newbie Coder
 
Join Date: Sep 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Question SQL Statement with array?

i am using the array in the sql query statement in a PHP script. i.e

$search = $_post['string'];

if $search = 'A';{
$result = select column from table where column like '$search%';
mysql_query($result); exit(); }


if $search = 'B';{
$result = select column from table where column like '$search%';
mysql_query($result); exit();}
...
continuously down to 'Z'

all am saying is how can i put the search string in an array (A-Z) and used it in the sql query at a time instead of write lot of codes.

hakeem
Reply With Quote
  #4 (permalink)  
Old 10-31-08, 09:33 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by hakeem View Post
i am using the array in the sql query statement in a PHP script. i.e

$search = $_POST['string'];

if $search = 'A';{
$result = select column from table where column like '$search%';
mysql_query($result); exit(); }


if $search = 'B';{
$result = select column from table where column like '$search%';
mysql_query($result); exit();}
...
continuously down to 'Z'

all am saying is how can i put the search string in an array (A-Z) and used it in the sql query at a time instead of write lot of codes.

hakeem
I don't understand what you are saying here.
From the looks of your code, you are only dealing with one value at a time.
The value collected by $_POST['string'].
Then you are doing a query with the value in $_POST['string'].

So what do you need an array for?

If you are using a single value each time, then the code becomes very simple.
PHP Code:

<?php
# Initialize the variables. #

# Please fill in all the necessary database values. #
$host "";     # MySql host address
$userName ""# MySql username
$password ""# MySql password
$db "";       # MySql database name
$table "";    # MySql table name
$column "";   # MySql table column name
# End database values. #

# This array could be filled in from the database. #
$array = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
# End initalize array. #

$msg "";
$query_results "";
$output "<div class='queryDiv'>Please select a value to query.<br />The results will be displayed here.</div><br class='clearFloat'/>";

# End initalize variables. #

if(!empty($_POST["submit"]))
{
 if(
$search = empty($_POST['string']) ? "" $_POST['string'])
 {
  
mysql_connect($host,$userName,$password) or die(mysql_error());
  
mysql_select_db($db) or die(mysql_error());
  
$result mysql_query("select $column from $table where $column like '$search%'") or die(mysql_error());
  if(
mysql_num_rows($result)==0)
  {
   
$msg "<p><span class='msg'>No records found using ' $search '.</span></p>";
   }
  else
  {
   
$query_results "<div class='queryDiv'>";
   while(
$row mysql_fetch_assoc($result))
   {
    
$query_results .= $row[$column]."<br />";
    }
   
$query_results .= "</div><br class='clearFloat'/>";
   }
  
mysql_free_result($result);
  
mysql_close();
  }
 else{
$msg "<p><span class='msg'>You must select a value to query.</span></p>";}
 
$output $msg.$query_results;
 }
?>
<html>
<head>
<title></title>
<style>
.msg
{
 border:2px solid #f00;
 padding:5px;
 color:#f00;
 font-weight:bold;
 }
.queryDiv
{
 border:1px solid #000;
 padding:5px;
 float:left;
 }
.clearFloat
{
 clear:both;
 }
</style>
</head>
<body>
<form action="#" method="post">
 <select name="string">
  <option></option>
  <?php
  
for($i=0;$i<count($array);$i++){echo $array[$i] == $search "<option SELECTED>$array[$i]</option>" "<option>$array[$i]</option>";}
  
?>
 </select>
 <input type="submit" name="submit" value="Submit">
</form>
<?php
echo $output;
?>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 10-31-08 at 09:50 AM.
Reply With Quote
  #5 (permalink)  
Old 10-31-08, 10:17 PM
hakeem hakeem is offline
Newbie Coder
 
Join Date: Sep 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
thanks guys question resolved.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
JS: If/Else statement with an Array Rollo Tamasi JavaScript 3 07-20-06 08:47 PM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
SQL Statement Ajtessin ASP 2 03-26-04 04:02 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM
change my field in this example sal21 ASP 3 07-14-03 02:49 AM


All times are GMT -5. The time now is 08:21 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.