Jump to content

Arrays working with MySQL


MasterACE14

Recommended Posts

Afternoon Everyone,

 

How can I grab a certain field of a Multi-Dimensional Array using the ID. I have set for that Array?

I just want to be able to create a simple function to do pretty much what this function does, but for an array.

<?php
// Select fields from the user table
function player_table($select){
    $where = $_SESSION['playerid'];
    $rs = mysql_connect("localhost", "ace_ACE", "*****");
    mysql_select_db("ace_cf", $rs);
    $sql = "SELECT `" . $select . "` FROM `cf_users` WHERE `id`='" . $where . "' LIMIT 1";
    $rs = mysql_query($sql) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
    while($row = mysql_fetch_assoc($rs)){
        if(isset($row[$select])){return $row[$select];}
    }
}

 

Sorry to anyone who doesnt understand what I mean, its hard to explain, my above example should give you a good idea of what I mean though.

 

Regards ACE

Link to comment
Share on other sites

basically this is wrong way

  while($row = mysql_fetch_assoc($rs)){

        if(isset($row[$select])){return $row[$select];}

    }

 

simply

$row = mysql_fetch_assoc($rs);

return $row;

 

 

then thats is function right so call a function like

$sample =player_table($vaiable);

echo $sample [field1];

echo $sample [field2];

 

Link to comment
Share on other sites

sorry? im lost  ???

 

basically this is wrong way

  while($row = mysql_fetch_assoc($rs)){

        if(isset($row[$select])){return $row[$select];}

    }

 

simply

$row = mysql_fetch_assoc($rs);

return $row;

 

as for that, I never knew it could be done in such a simple way before  :D

Link to comment
Share on other sites

<?php
function player_table($select,$connection,$db){
  	mysql_select_db($db,$connection);// idont know why you want this do you have lots of db?
    $rs = mysql_query($sql,$connection) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());
    while($row = mysql_fetch_assoc($rs)){
        $result[] = $row;
    }
return $result;
}
  $rs = mysql_connect("localhost", "ace_ACE", "*****");
  $query ='your complete query';
  
  $result = player_table($query,$rs,'dbname');
  print_r($result);
  //result should be multi dimensional

?>

note: not testted

edited....

Link to comment
Share on other sites

guess I didn't explain what I was after well enough. I didnt want to display information from the database in a multi dimensional array, I have a multi-dimensional array already setup, I want to select a certain array, and a certain value in the array in a similar way to how my player_table() function works.

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.