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;
}
Related
$result = mysqli_query($con, "SELECT * FROM users");
$usersArray=[];
tableArrayPushData($result, $usersArray);
function tableArrayPushData($result, $tableArray){
while ($row = $result->fetch_assoc()) {
$str = '';
foreach ($row as $value) {
$str = $str.$value.'|';
}
$newStr = rtrim($str, "| ");
array_push($tableArray,$newStr);
}
}
for ($i=0; $i<count($usersArray); $i++){//array is always empty at this point
echo "Ok";
echo "<br>";
}
I don't understand why, but usersArray is empty despite the fact that I added data there.
The MySQL table has rows with data, so it can't be empty.
You should use the & operator to allow the function to access the outer variable, like this:
function tableArrayPushData($result, &$tableArray) {}
Or use return.
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;
}
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) . "'";
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);
}
If I need to select and use information of every element of a table in a database the procedure would be this:
$query = "...mySql query...";
$query_result = mysql_query($query) or die (mysql_error());
Then if I wished to access the fields of the result I would use the function mysql_fetch_array() and access them like this:
$query_result_array = mysql_fetch_array($query_result);
echo $query_result_array['field_1'];
....
echo $query_result_array['field_i'];
....
But since more elements could be returned by the query I would like to access every single of them with an array indexed from 0 to mysql_num_rows($query_result).
As an example:
echo $query_result_array['field_i'][0];
....
echo $query_result_array['field_i'][mysql_num_rows($query_result)];
should print for every selected element of the table the value of field i.
Is there a function that will do the job for me?
If not, any suggestions on how to do it?
Thanks in advance for help.
This may be an alternative
$res = mysql_query("..SQL...");
$arr = array();
while ($row = mysql_fetch_assoc($res)) {
$arr[] = $row;
}
var_dump($arr);
Or
$res = mysql_query("..SQL...");
for
(
$arr = array();
$row = mysql_fetch_assoc($res);
$arr[] = $row
);
var_dump($arr);
I don't think there is such a method; you have to do it yourself.
try with something like:
$res = mysql_query("..mySql query...");
$arr = array();
while ($row = mysql_fetch_assoc($res)) {
$query_result_array[] = $row;
}
then you access your data like:
echo $query_result_array[0]['field_i'];
based on 2 previous answers, those authors assuming that usual SO author is familiar with such a thing as creating a function
function sqlArr($sql) { return an array consists of
$ret = array();
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
if ($res) {
while ($row = mysql_fetch_assoc($res)) {
$ret[] = $row;
}
}
return $ret;
}
$array = sqlArr("SELECT * FROM table");
foreach ($array as $row) {
echo $row['name'],$row['sex'];
}
this resulting array have different structure from what you asked, but it is way more convenient too.
if you still need yours unusual one, you have to tell how you gonna use it