Hello Everyone, I have a MySQL database as follows: id , name , city 1 , abc , new york 2 , xyz , toronto 3 , jas , london I would like to learn how can i create an array that associates id to name, something like: <?php echo $array[1]; //Outputs abc ?> PHP:
So far, i have this: <?php $var = abc; $query = "SELECT id,name FROM table WHERE name='$var'"; $resource = mysql_query($query); $array[] = mysql_fetch_array($resource); //But $array here contains a multi dimensional array that i dont want.. ?>
Hello, for that type of tabulate array, you can use 2 dimensionality array which will increase reliability. For example, Consider your given table: id , name , city 1 , abc , new york 2 , xyz , toronto 3 , jas , london i j $db[3][3] // 3 rows (i) and 3 column (i). Abd iff you want to access Id of 1st record, you can call up array $db[1][1] // Out will be 1 Hope this will help you
Thanks for the reply Daksh, But the problem here is that i'm pulling the data from a MySQL database and it always spits out the data in form of a Multi Dimensional Array... Accessing the exact records become very tough for me when i have Multi dimensional arrays... Is'nt there a solution that help me create an array that associates the id column to name column or vice versa..?
Thanks for your Help... i really appreciate it... actually i'm a student and i've been hired at a company to develop their Corporate Website... so, i might need a lot of help in due course..
$var = abc; $query = "SELECT id,name FROM table WHERE name='$var'"; $resource = mysql_query($query); while($row = mysql_fetch_row($resource)){ $array[$row[0]] = $row[1]; } Code (php): Now $array[1] should output 'abc'; To sanitize your php input you should use mysql_real_escape_string This are some example to sanitize get and post variables. // Escape GET variable foreach ( $_GET as $key => $val ){ $val = mysql_real_escape_string($val); $_GET[ $key ] = $val; } // Escape POST variable foreach ( $_POST as $key => $val ){ $val = mysql_real_escape_string($val); $_POST[ $key ] = $val; } Code (php): DON'T use this code as it is, you should change it to suit your needs. (for exampleif you pass an array through a post variable you will get an error)
yes, i think you should use mysql_escape_string() function in your application. For example, in your given query you can use it like: <?php $temp2 = "SELECT id FROM shareholders_data WHERE shareholders_name='".mysql_escape_string($arrayShareholdersName[$y])."'"; ?> thank you. more questions are welcomed.