I am trying to add multiple language options with the database. I created a table as follows.
And my PHP function is:
public function Languages(){
$query=mysqli_query($this->db,"
SELECT * FROM languages") or die(mysqli_error($this->db));
while($row=mysqli_fetch_array($query,MYSQLI_ASSOC)) {
$data[]=$row;
}
if(!empty($data)) {
// Store the result into array
return $data;
}
}
So also I used the following code for showing result but I am getting the empty result:
<?php
$language = $ReSult->Languages();
$userLanguage = 'english';
echo $language['your_family'][$userLangauge];
?>
I know if I use $language['1'][$userLanguage]; then I get a result but I don't want to use id's here. Is there any way to do that to show results without using ids?
You could store the results in an associative array:
while ($row = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
$data[$row['lang_key']] = $row;
}
This way, you can access the data directly by its key:
echo $language['your_family'][$userLangauge];
checkout this
function getIndex($language , $key)
{
foreach ($language as $row)
{
if (strcmp($row["lang_key"] , $key)==0) {
return $row["id"]
}
}
return null;
}
...
echo $language[getIndex($language , "your_family")][$userLanguage];
You can try like this way,
while ($row = mysqli_fetch_assoc($query)) {
$data[$row['tagKey']] = $row;
}
Related
I want to return the multiple records in table but do not know how. To return all values of table and return ?
please tell how can i do it.
function get_semester_data($id) {
$query_1="SELECT sem.smstr_id,sem.smstr_no,sem.start_mnth,sem.start_year,sem.end_mnth,sem.end_year,sem.status FROM `semesters` sem,sessions s where s.s_id=sem.s_id and s.s_id=$id";
$result=mysql_query($query_1);
while($row=mysql_fetch_array($result)) {
}
return $row;
}
i want to return the multiple records in table but do not know how. to return all values of table and return ?
please tell how can i do it.
try like this
while($row=mysql_fetch_array($result)){
$data[]=$row;
}
return $data;
You can return an array of rows!
function get_semester_data($id) {
$query_1="SELECT sem.smstr_id,sem.smstr_no,sem.start_mnth,sem.start_year,sem.end_mnth,sem.end_year,sem.status FROM `semesters` sem,sessions s where s.s_id=sem.s_id and s.s_id=$id";
$result=mysql_query($query_1);
$all_rows = array();
while($row=mysql_fetch_array($result)) {
$all_rows[] = $row;
}
return $all_rows;
}
//Get the values like this
$rows = get_semester_data(12);
//print all smstr_no like this
foreach ($rows as $row) {
echo $row[1];
}
First the code:
function get_semester_data($id) {
$query_1="SELECT sem.smstr_id,sem.smstr_no,sem.start_mnth,sem.start_year,sem.end_mnth,sem.end_year,sem.status FROM `semesters` sem,sessions s where s.s_id=sem.s_id and s.s_id=$id";
$result=mysql_query($query_1);
$result = array();
while($row=mysql_fetch_array($result)) {
array_push($result, $row);
}
return $result;
}
Just declare the array, add the rows and return it.
Suggestions:
the extension you are using is old and will be deprecated, use mysqli
or pdo instead
be careful and sanitize your input because you are prone to SQL
Injection on the $id parameter. Use this:
$query_1="SELECT sem.smstr_id, sem.smstr_no, sem.start_mnth, sem.start_year, sem.end_mnth, sem.end_year, sem.status FROM semesters sem,sessions s where s.s_id=sem.s_id and s.s_id = '" . htmlentities($id, ENT_QUOTES) . "'";
Since the servers version is older than 5.3.0, I need to rewrite the following piece of function to do the same as it does now:
else {
$res = mysqli_query($_con, "SELECT * FROM house");
$row = mysqli_fetch_all($res, MYSQLI_ASSOC);
return $row;
}
In html I call it like this:
$results = getResults();
foreach ($results as $value) {
echo $value['Title']." / "; echo $value['Version'];
}
How can I call the results in my html the same way but with different function?
EDIT: I want to get all of the results from table "house" but without the use of function mysqli_fetch_all()
It's just a simple loop that calls mysql_fetch_array() and collects all the rows in an array.
function mysqli_fetch_all($res, $mode) {
$array = array();
while ($row = mysql_fetch_array($res, $mode)) {
$array[] = $row;
}
return $array;
}
I'm not sure if it is possible or not but what I am looking for a way making auto "set" for mysql results.
function test(){
$pDatabase = Database::getInstance();
$site = new Template("sites.tpl");
$query = 'SELECT * FROM sites';
$result = $pDatabase->query($query) or die('Query failed: ' . mysql_error());
while ($row = mysql_fetch_array($result)) {
$site->set("id",$row['id']);
$site->set("category",$row['category']);
$site->set("name",$row['name']);
$site->set("html",$row['html']);
$site->set("css",$row['css']);
$site->set("js",$row['js']);
$site->set("php",$row['php']);
$site->set("details",$row['details']);
$site->set("link",$row['link']);
}
mysql_free_result($result);
}
Maybe there is a better way doing all that $site->set ? I mean my code looks way too big and pointless. Any other ways ?
If you want to call $site->set on all key/value pairs in $row
while ($row = mysql_fetch_array($result)) {
foreach($row as $key => $value) {
$site->set($key,$value);
}
}
Change to this...
Assuming your site->set function will always take the exact table column names.
while ($row = mysql_fetch_array($result)) {
foreach($row as $k=>$v){
$site->set($k,$v);
}
}
You could also alter teh $site->set function and do the loop there. And then just do this...
while ($row = mysql_fetch_array($result)) {
$site->set($row);
}
And the function. Just an outline. Not really sure what you have going on in the actual function. But this is just an idea
function set($arrorkey, $value=null){
// If passed value is an array, do this...
if(is_array($arrorkey)){
foreach($arrorkey as $k=>$v){
$_SESSION[$k] = $v;
//Or database binding or whatever you're actually doing inside here
}
} else {
// If passed value, is a column, and value pair...do this.
$_SESSION[$arrorkey]=$value;
//Or database binding or whatever you're actually doing inside here
// This is just an example
}
return;
}
i searched all over the web or i just don't see it.
I want to do the following:
<?php
function abc(){
$sql = "SELECT * FROM table";
$result = mysql_fetch_assoc($sql);
$data = mysql_fetch_assoc($result);
}
?>
<? while(abc()) : ?>
<?=article_title()?>
<?=article_content()?>
<?=endwhile;?>
Could somebody give me a direction and/or example?
Thank you very much
PHP does not have generator/iterator functions, so you cannot do that.
You can however return an array and iterate over that array:
function abc() {
// ...
$rows = array();
while($row = mysql_fetch_assoc($result)) { $rows[] = $row; }
return $rows;
}
foreach(abc() as $row) {
// do something
}
If the functions you call need access to the row, pass it as an argument. Using global variables for it would be very bad/nasty
have you tried to do something like that?
<?php
function getData(){
//get the data from the database
//return an array where each component is an element found
}
$elems = getData();
foreach($elems as $e){
doSomethingWith($e);
}
I am trying to build a function that extracts information from a database and inserts it into an associative array in PHP using mysql_fetch_assoc, and return the array so another function can display it. I need a way to display the returned assoc array. This should be a different function from the first one
print_r($array) will give nicely formatted (textually, not html) output.
If you just want information about what is in the array (for debugging purposes), you can use print_r($array) or var_dump($array), or var_export($array) to print it in PHP's array format.
If you want nicely formatted output, you might want to do something like:
<table border="1">
<?php
foreach($array as $name => $value) {
echo "<tr><th>".htmlspecialchars($name).
"</th><td>".htmlspecialchars($value)."</th></tr>";
}
?>
</table>
This will, as you might already see, print a nicely formatted table with the names in the left column and the values in the right column.
while ($row = mysql_fetch_assoc($result)) {
foreach ($row as $column => $value) {
//Column name is in $column, value in $value
//do displaying here
}
}
If this is a new program, consider using the mysqli extension instead.
Assuming you've made the call, and got $result back:
$array = new array();
while($row = mysql_fetch_assoc($result)){
$array[] = $row;
}
return $array;
This should get you going:
$rows = mysql_query("select * from whatever");
if ($rows) {
while ($record = mysql_fetch_array($rows)) {
echo $record["column1"];
echo $record["column2"];
// or you could just var_dump($record); to see what came back...
}
}
The following should work:
$rows = mysql_query("select * from whatever");
if ($rows) {
$header = true;
while ($record = mysql_fetch_assoc($rows)) {
if ($header) {
echo '<tr>';
foreach (array_keys($record) AS $col) {
echo '<td>'.htmlspecialchars($col).'</td>';
}
echo '</tr>';
$header = false;
}
echo '<tr>';
foreach (array_values($record) AS $col) {
echo '<td>'.htmlspecialchars($col).'</td>';
}
echo '</tr>';
}
}
(Yes, blatant mod of Fosco's code)
This should print the column headers once, then the contents after that. This would print just whatever columns were retrieved from the DB, regardless of the query.