Fetch result from database to multiple variables - php

I have a site were the user fills a form and all data is stored in a database, when the user enter his/hers page all the added data is visible. Today I´m doing this but in a lot of code rows and there is for sure a much smoother way to do this.
Here´s a look of how I have done it today:
$query = mysqli_query($dbhandle, "SELECT * FROM ..."); // ... added now
$row = mysqli_fetch_assoc($query);
$m0 = $row['m1'];
$m1 = $row['m2'];
$m2 = $row['m3'];
$m3 = $row['m4'];
...
$m47 = $row['m48'];
$firstPlace = $row['firstPlace '];
$secondPlace = $row['secondPlace '];
$thirdPlace = $row['thirdPlace '];
$fourthPlace= $row['fourthPlace'];
As you can see there are a lot of rows of code. What I would like to do is to loop through my query and then add the right value in the database to the right value in the form.
Appreciate help.

There definitely are many alternative (and in every possible sense of the word) better ways to go about your business.
For a kickoff: ask yourself what an array actually is. An array is a collection of data. You store them together because one value of that array in itself doesn't mean much. The data in an array belongs together. Why then, assign it to individual variables in the first place?
Of course, your $row array has keys like $row['m1'], which you assign to a variable called $m0. so the names of the fields in the database don't quite match the names your code uses. That's something that you can, quite easily, fix by changing your query: use aliasses for those fields:
SELECT m1 as m0, ... FROM
Now your array will have a key called m0, instead of m1. This reduces the rest of your code down to:
$row = mysqli_fetch_assoc($query);
echo 'M0: ', $row['m0'];//<-- use m0 value here.
Alternatively, you could use a second array that maps these field-names to the name you want to use in your code:
$map = array(
'm0' => 'm1'
);
echo 'M0: ', $row[$map['m0']];//use value of m0, which is the actual key if the $row array
Still, if you are hell-bound on unmaintainable, messy, error-prone and just awful code, you could use variable variables:
foreach ($row as $key => $value)
{
$$key = $val;
}
Note the double $ in $$key. This is like saying "the variable that is called whatever the value of $key is". If $key is firstname, the code above evaluates to $firstname = $value. But whatever you do: forget this is possible. It's like an enema: yes, it's possible, but you don't want one if you can avoid it. And in this case, you clearly can avoid it.

Loop through the $row var grabbing the key and value. If key starts with "m" followed by a 1 or 2 digit number, get the number, subtract one, concatenate it with "m", and assign the value. Otherwise just interpolate key into variable name and assign value.
foreach ( $row as $key => $value ) {
if ( preg_match('/^m(\d{1,2})/', $key, $matches) ) {
${'m' . ($matches[1] - 1)} = $value;
}
else { $$key = $value; }
}
In the above example, $row['m1'] value gets assigned to var $m0, and $row['firstPlace'] to var $firstPlace, etc.

Related

convert mysql result to json with correct types

I know how to get a mysql-row and convert it to json:
$row = mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM table WHERE id=1"));
echo json_encode($row); // it's an ajax-call
but:
the db-row has different types like int, float, string.
by converting it using json_encode() all results are strings.
Is there a better way to correct the types than this:
$row['floatvalue1'] = 0+$row['floatvalue1'];
$row['floatvalue2'] = 0+$row['floatvalue2'];
$row['intvalue1'] = 0+$row['intvalue1'];
I would like to loop through the keys and add 0 because:
first coding rule: DRY - dont repeat yourself
but i can't because:
row has also other types than numbers (string, date)
there are many columns
design is in dev, so columns-names often changes
Thanks in advance and excuse my bad english :-)
EDIT (to answer the comment-question from Casimir et Hippolyte):
I call this php-code using ajax to get dynamically sql-values. in my javascript-code i use the results like this:
result['intvalue1'] += 100;
lets say the json-result of intval1 is 50, the calculated result is:
"50100", not 150
The code below is just a proof of concept. It needs encapsulation in a function/method and some polishing before using it in production (f.e. call mysqli_fetch_field() in a loop and store the objects it returns before processing any row, not once for every row).
It uses the function mysqli_fetch_field() to get information about each column of the result set and converts to numbers those columns that have numeric types. The values of MYSQLI_TYPE_* constants can be found in the documentation page of Mysqli predefined constants.
// Get the data
$result = mysqli_query($db, "SELECT * FROM table WHERE id=1");
$row = mysqli_fetch_assoc($result);
// Fix the types
$fixed = array();
foreach ($row as $key => $value) {
$info = mysqli_fetch_field($result);
if (in_array($info->type, array(
MYSQLI_TYPE_TINY, MYSQLI_TYPE_SHORT, MYSQLI_TYPE_INT24,
MYSQLI_TYPE_LONG, MYSQLI_TYPE_LONGLONG,
MYSQLI_TYPE_DECIMAL,
MYSQLI_TYPE_FLOAT, MYSQLI_TYPE_DOUBLE
))) {
$fixed[$key] = 0 + $value;
} else {
$fixed[$key] = $value;
}
}
// Compare the results
echo('all strings: '.json_encode($row)."\n");
echo('fixed types: '.json_encode($fixed)."\n");
something like
$row['floatvalue1'] = reset( sscanf ( $row['floatvalue1'] , "%f" ));
$row['floatvalue2'] = reset( sscanf ( $row['floatvalue2'] , "%f" ));
$row['intvalue1'] = reset( sscanf ( $row['intvalue1'] , "%d" ));
json_encode($row);
If you're simply trying to make sure that your values are operable with respect to their type, you need to first cast their type correctly.
Unless you need them server-side, I would just pass-on the json directly to the front-end and do the work there.
In Javascript, you could make an attempt at casting the numbers like so:
function tryNumber(string){
return !isNaN( parseInt(string) ) ? parseInt(string) : string;
}
function tryDate(string){
return !isNaN( new Date(string).getTime() ) ? new Date(string) : string;
}
tryNumber('foo'); // "hello"
tryNumber('24'); // 24
tryDate('bar'); // "bar"
tryDate('December 17, 1995'); // "Sun Dec 17 1995 00:00:00 GMT+0000 (GMT)"
These two lines attempt to cast the values as a Date/Number. If they can't be cast, they will remain String's.
A MySQLi OO version based on #axiac's answer, that produces a JSON array ($jsnAll) containing all records. In this code snippet, the method FixSQLType is called to fix a row. Note, it should be wrapped in a try{}catch{} block and "objMySQLi" has already been instantiated:
$lcAllRows = array();
// Make an SQL SELECT statement
$SQL = "SELECT * FROM $lcName WHERE $lcWhere";
// Run the query
$this->sqlResult = $this->objMySQLi->query($SQL);
// Fetch the result
while( $row = $this->sqlResult->fetch_assoc()){
$lcCount = count($lcAllRows) ;
// Call to fix, row
$fixedRow = $this->FixSQLType($row);
$lcAllRows[$lcCount]= $fixedRow;
}
$jsnAll = json_encode($lcAllRows);
The FixSQLType method. This is almost identical to #axiac's answer, except for the call to $this->sqlResult->fetch_field_direct($i). "fetch_field" seemed to get itself lost, using "fetch_field_direct" overcame that problem.
private function FixSQLType($pRow){
// FROM https://stackoverflow.com/a/28261996/7571029
// Fix the types
$fixed = array();
$i = 0;
foreach ($pRow as $key => $value) {
$info = $this->sqlResult->fetch_field_direct($i);
$i++;
if (in_array($info->type, array(
MYSQLI_TYPE_TINY, MYSQLI_TYPE_SHORT, MYSQLI_TYPE_INT24,
MYSQLI_TYPE_LONG, MYSQLI_TYPE_LONGLONG,
MYSQLI_TYPE_DECIMAL,
MYSQLI_TYPE_FLOAT, MYSQLI_TYPE_DOUBLE
))) {
$fixed[$key] = 0 + $value;
} else {
$fixed[$key] = $value;
}
}
return $fixed;
}

How to iterate through a mysql array and append corresponding text from an PHP array

I am making a pricing table to upgrade membership.
In mySQL I have a table with limits on what each membership can allow see picture
I grab the info from the table and try to iterate through it and append some text to it from another array. I have tried thoroughly looking online for an answer but I'm probably not even searching for the right terms. For the purpose of this question i'm using 2 fields but in reality i will be iterating and appending many columns.
Here's my code:
$sql = "SELECT maxProducts,maxImages FROM memproducts_membership_settings WHERE useThis='1'";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs,MYSQL_ASSOC))
{
foreach($row as $field)
{
$append = array('Product Slots','Images per product');
print $field.' '.$append.'<br>';
}
}
All i get is:
5 Array
2 Array
and not what i need which would be:
5 Product Slots
2 Images per product
I'm sure there is a way to do this but i just cannot figure it out, please anyone give me some pointers? Many regards :)
This is pretty basic stuff...
while($row = mysql_fetch_array($rs,MYSQL_ASSOC))
{
echo $row['maxProducts'] . ' Product Slots<br>';
echo $row['maxImages'] . ' Images per product<br>';
}
Or if you are needing to do this with dynamic fields in whatever order you return from database like your question suggests, perhaps you might consider providing a mapping of database column names to the appropriate label content like this:
$labels = array(
'maxProducts' => 'Product Slots',
'maxImages' => 'Images per product',
...
);
...
while($row = mysql_fetch_array($rs,MYSQL_ASSOC)) {
foreach ($row as $key => $value) {
echo $value . ' ' . $labels[$key] . '<br>';
}
}
Note if you are just learning PHP, you REALLY should NOT learn mysql_* functions. They are deprecated and need to die. Please learn to use PDO or mysqli.
To append values to the end of an array use array_push()
http://php.net/manual/en/function.array-push.php
There's plenty about your code and your approach that I don't like at all, but the most immediate solution to your current problem would be to iterate the array of strings from within the foreach loop, using each():
$strings = array('Product Slots','Images per product');
while($row = mysql_fetch_array($rs,MYSQL_ASSOC))
{
reset($strings);
foreach($row as $field)
{
$append = each($strings);
print $field.' '.$append[1].'<br>';
}
}
You should at very least make yourself aware of:
the deprecation of ext/mysql (use either mysqli or PDO); and
the risk of XSS attacks (escape any HTML in values from your database before returning them to the browser).
It's doing what it should... You have created an array and asked it to print its name. As print can't automatically iterate an array it just tells you what the variable type is.

Take data between a comma from a SQL row PHP

I'm about to write this code which stores numbers in a database, for example it's 1,2,3,4. I know how to store it in the database, etc, but I need to know to get those numbers out of the database, everyone on it's own where it can be used as a variable.
Example:
Database has a column called IDs, IDs contains the following:
1,2,3,4,5
In PHP I want to take every number in the column IDs, so there's a variable for every number.
$variable = 1;
$variable = 2;
etc
explode() can parse the string into an array of variables with one element of the array per number.
$str = '1,2,3,4,5';
$arr = explode(',', $str);
echo $arr[0]; // 1
echo $arr[1]; // 2
echo $arr[2]; // 3
echo $arr[3]; // 4
echo $arr[4]; // 5
However, storing strings of IDs in a database is generally undesirable. Usually there is a better database structure that can be used.
For example by using an additional table to associate rows from two other tables:
customers ( id, name, date_of_birth )
customer_addresses ( id, line1, line2, town, city )
customer_address_assoc ( customer_id, customer_address_id )
Using the explode method, you may have stored a field on the customer table such as address_ids. But by using an association table, this is a much better structure. It's easier to query, faster and generally more optimized. The association table would store a row for every address associated to the customer.
Best way to to this is using a foreach cycle but it depends on what you need.
This examples echoes all your numbers:
<?php
foreach($querynumberas $number){
$id = $number['id'];
echo $id;
}
?>
But this doesn't store all variables as you need, so to do this you should use this construct:
<?php
for ($i = 1; $i <= array.lenght(); ++$i) {
${'number'.$i} = $number['number'.$i]; // it stores: number1 = 1; number2 = 7; ecc
}
php?>

Running a statement based on information from POST variable

I'm in a bit of pickle but the answer is probably pretty simple.
So I have my POST variable:
Array ( [accept] => accept,29 [accept1] => accept,30 [submit] => Save Selections )
That's just a print_r of $_POST.
Basically what I want to do is get the first variable, remove the 'accept,' part and store the number next to it in a variable, run some MySQL queries using that variable (containing that number) then move onto the next one; removing the 'accept,' storing the number next to it and running a query using that stored number. It want to do this for as many times is necessary to go through all the elements containing 'accept,' then a number.
Any help would be appreciated.
I was playing around with some ideas and have this code. It obviously doesn't work but perhaps I could fix and build on it?
while($i <= $elements)
{
while($x == 1)
{
$id = explode(',', next($_POST));
echo $id;
$x = 0;
}
$i++;
}
Im not sure this would fix your problem but I hope this helps and gives you an idea:
$i = 0;
while($i <= count($your_array))
{
$your_var = substr($your_array[$i], 7, 2); //get the number from your accept,29
$query = "SELECT * FROM TABLE WHERE COLUMN = ". $your_var; //use it to run queries as you said
$i+=1;
}
UPDATE - Im not sure if this would work
foreach($your_post_array as $items)
{
$your_var = substr($items, 7, 2);
$query = "SELECT * FROM TABLE WHERE COLUMN = ". $your_var; //use it to run queries as you said
$i+=1;
}
Maybe you should try to echo first the variables to make sure echo $your_var = substr($items, 7, 2);
I would use a foreach loop to loop through $_POST, check if the key starts with accept (stripos?), explode the value if it does and get the second value of the found array and store that somewhere for later use.
By the way, if possible I would change the front-end so that you have just one variable (an array) in $_POST you have to loop through.

Storing array items in MySQL using foreach

I have arrays within an array and I want to loop through each item of the nested arrays and put them into different mysql columns. There is a limit of 3 entries per array, and I need $workexp_array_t to go into the work experience column, the $credentials_array_t items to go into the credentials column etc.
The problem I'm having is that using these nested foreach loops it just places the first letter of the entry into the column instead of the proper entry. How can I get each array items to go into their proper column? Do I really need to set up separate tables for each thing (ie. education experience, credentials, work experience, etc.)?
$tutor_background = array($workexp_array_t, $credentials_array_t, $education_array_t, $extra_array_t);
foreach ($tutor_background as $entry) {
foreach ($entry as $background) {
$query = "INSERT INTO tutor_background (login_value, work_history, credentials, education_history, extra_skills) VALUES ('{$_SESSION['login_value']}', '{$background[0]}', '{$background[1]}', '{$background[2]}', '{$background[3]}')";
$process_query = mysql_query($query);
}
}
Going to take a guess here and say you only need one foreach.
$tutor_background = array($workexp_array_t, $credentials_array_t, $education_array_t, $extra_array_t);
foreach ($tutor_background as $entry) {
$query = "INSERT INTO tutor_background (login_value, work_history, credentials, education_history, extra_skills) VALUES ('{$_SESSION['login_value']}', '{$entry[0]}', '{$entry[1]}', '{$entry[2]}', '{$entry[3]}')";
$process_query = mysql_query($query);
}
}

Categories