Jump to content

Help with passing mysql table to a array


dbk

Recommended Posts

Hi

 

I'm trying to get all from the row 'client_id' in a table and put it in a array, exept where the 'client_id' has a specific value. After it's in a array I can search for a specific value!

 

This is what I've been trying:

 

$except_client = 34343434;
$check = 12121212;

$check_sql = "SELECT client_id FROM clients WHERE client_id !='$except_client'";
$client_result = mysql_query($check_sql);
$client_row = mysql_fetch_array($client_result);

$search = array_search($check, $client_row);

 

When I write the mysql command in the CMD I get the result I want, but I can't get it to the array! Only the first row.

Link to comment
Share on other sites

In PHP the mysql_query() function returns a pointer to the results array. You need to put the fetch function in a loop to get all the results:

<?php
$except_client = 34343434;
$check = 12121212;

$check_sql = "SELECT client_id FROM clients WHERE client_id !='$except_client'";
$client_result = mysql_query($check_sql);
$tmp = array();
while ($client_row = mysql_fetch_array($client_result)) {
    $tmp[] = $client_row['client_id'];
}

$search = array_search($check, $tmp);
?>

 

Ken

Link to comment
Share on other sites

@dbk,

 

That process doesn't make sense. Why would you want to get all the clinet_id's not equal to a ccertain value and then do a search for a specific client_id? Why not just do a query for the client_id you are looking for?

Link to comment
Share on other sites

Hi mjdamato

 

I have a reason:

 

The table contains all our clients numbers, and this function is when you want to edit the client data in a form.

The value i get from $_POST[] could be the same as it already is if no changes is made. But it could also be the same as one of the other clients in our database, wich would be a mess!

 

That's why I want to exclude the row where the existing client number is, and only search all other!

 

Maybe there is a other way??

Link to comment
Share on other sites

Hi mjdamato

 

I have a reason:

 

The table contains all our clients numbers, and this function is when you want to edit the client data in a form.

The value i get from $_POST[] could be the same as it already is if no changes is made. But it could also be the same as one of the other clients in our database, wich would be a mess!

 

That's why I want to exclude the row where the existing client number is, and only search all other!

 

Maybe there is a other way??

 

If I am understanding you correctly the post data will have the old client ID and a (possibly) new client ID. And you are wanting to update the client ID if there was a new one submitted. You would just need a single query to the effect

UPDATE table
SET client_id = '$newID'
WHERE client_id = '$oldID'

 

If you need to ensure there are no duplicates, just do a query before that to see if the new ID already exists or not.

Link to comment
Share on other sites

Thanks a lot guys!

 

Now it's rolling  8)

With your help I got this fine working code:

 

//fictitious values
$except_client = 77131923;
$check = 65771245;

$check_sql = "SELECT client_id FROM client WHERE client_id !=$except_client";
$client_result = mysql_query($check_sql)
     or trigger_error("Query Failed: " . mysql_error());

//Place query in a array
$client_temp = array();
while ($client_row = mysql_fetch_array($client_result))
{
    $client_temp[] = $client_row['client_id'];
    
}

//Return 1 if value exists in array
$search = in_array ($check, $client_temp);

//Do the desired action if..
if ($search == 1)
    {
    echo "Client no: $check is taken!";
    }
else
    {
    echo "no: $check is a fine client number!";
    }

 

Using the 'in_array' function it returns 1 if exists in array and nothing if not.

Before I tried the 'search_array' function wich gave me problems to evaluate with 'if' because the first place in the array returns 0 if the value exists in array.

 

 

 

Link to comment
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.