Foreach loop declare variable on select table array [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
Is it possible to create variable inside foreach loop that each variable name depend on column that I select?
Here is my code:
//sql select string declaration
$sql = "select [Rec_ID],[Bike_ID],[Station],['Line']
from [rfttest].[dbo].[RFT_Records_Log]
where [Rec_ID] = '{$_GET['recid']}'";
$query = sqlsrv_query($conn,$sql); //query
//if query fail print out error
if($query === false)
{
die(print_r(sqlsrv_errors(),true));
sqlsrv_close($conn);
}
//continue with fetch array
$recdata = sqlsrv_fetch_array( $query, SQLSRV_FETCH_ASSOC);
//foreach to declare variable
foreach($recdata as $x => $a)
{
$"$x" = $"$a";
}
In this code I should successfully declare variable like:
$Rec_ID , $Bike_ID , $Station , $Line
I still get a syntax error:
Parse error: syntax error, unexpected '"', expecting variable (T_VARIABLE) or '$'

Simply you can do this:
foreach($recdata as $x => $a)
{
$x = $a;
}
Use following query:
$sql = "select [Rec_ID],[Bike_ID],[Station],['Line']
from [rfttest].[dbo].[RFT_Records_Log]
where [Rec_ID] = '{".$_GET['recid']."}'";
or escape ' with \ like:
$sql = "select [Rec_ID],[Bike_ID],[Station],['Line']
from [rfttest].[dbo].[RFT_Records_Log]
where [Rec_ID] = \'{$_GET['recid']}\'";

try without quotes
$$x = $$a;
.

I think an array is want you need.
$data["$x"] = $a;

Related

how to get variables from column names [duplicate]

This question already has answers here:
variable variables
(5 answers)
Closed 1 year ago.
how to get variables from column names
$sq = "select * from arts where id = :aid limit 1";
$st = $db->prepare($sq);
$st->execute([":aid" => $id]);
$row = $st->fetch();
now, instead of:
$cat = $row['cat'];
$title = $row['title'];
$subtitle = $row['subtitle'];
... so on
I need something like:
foreach($column_name as $el){
$var_name = $el;
}
There's rarely a good reason to do this, just use the array. However, you can use variable variables from the keys if there is only one row as you show:
foreach($row as $key => $val){
$$key = $val;
}
There is also extract() but may be even worse practice.

The array does not show the information echo [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
No I'm doing wrong, I am modifying a system created by someone else but I can not show the information ... This is the code ...
$db = DB::getInstance();
$id = 1;
$query = $db->query("SELECT * FROM users WHERE id = ?", array($id));
$x = $query->results();
echo $x;
The error: Notice: Array to string conversion in...
Try using var_dump (instead of echo) if the return value is an object. For example:
var_dump($x);
use print_r($x) instead of echo $x
echo is used to print string and numbers but could not print array,
you may use var_dump too ... actually var_dump is used to print objects
use FOR :
$db = DB::getInstance();
$id = 1;
$query = $db->query("SELECT * FROM users WHERE id = ?", array($id));
$x = $query->results();
for ($i=0;$i<count($x); $i++){
echo $x[$i]."<br/>";
}

Json database links [duplicate]

This question already has answers here:
json_encode() escaping forward slashes
(4 answers)
PHP, why do you escape my quotes? [duplicate]
(6 answers)
Closed 7 years ago.
Hello guys im with a problem handling PHP and JSON
This is my php code
<?php
mysql_connect("127.0.0.1","root","");
mysql_select_db("spadramatico_db");
$query = mysql_query("SELECT * FROM feed_table ORDER BY id");
$records = array();
while($obj = mysql_fetch_object($query)) {
$records [] = $obj;
}
print (json_encode($records));
?>
And this is the output result:
[{"id":"1","title":"Teste Title","image":"http:\/\/catalinaseaspa.com\/wp-content\/uploads\/2015\/03\/island-girl.jpg","desc":"Desc test","price":"1"}]
My Problem its with the link, the output its like this:
http:\/\/catalinaseaspa.com\/wp-content\/uploads\/2015\/03\/island-girl.jpg
But its to be like this:
http://catalinaseaspa.com/wp-content/uploads/2015/03/island-girl.jpg
How can i fix that?
Thank you :D
try this code. Add the JSON_UNESCAPED_SLASHES to the function.
<?php
mysql_connect("127.0.0.1","root","");
mysql_select_db("spadramatico_db");
$query = mysql_query("SELECT * FROM feed_table ORDER BY id");
$records = array();
while($obj = mysql_fetch_object($query)) {
$records [] = $obj;
}
print (json_encode($records,JSON_UNESCAPED_SLASHES));
?>

Undefined index from a foreach() loop [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
ItemDb class
public function getRandomItem() {
$query = "SELECT * FROM `items` ORDER BY RAND() LIMIT 2";
return $this->query($query);
}
Index.php
$item = new Item();
$result = $item->getRandomItem();
while ($row = $result->fetch_assoc()) {
foreach ($row as $key => $value) {
//I want to put them in a array but the two items need to be separated
}
}
I get two different items out of the database how can I split them and put them in one array separated like:
$array[$key][$value]
Sorry for my English its my second language and I hope you guys understand me.
You need to declare $itemArray[$key] before you use it. So your code needs to look like
$itemArray = array();
while ($row = $result->fetch_assoc()) {
foreach ($row as $key => $value) {
if(!isset($itemArray[$key])) {
$itemArray[$key] = array(); //Declare it
}
$itemArray[$key][] = $value;
}
}

msqli_result could not be converted to string [duplicate]

This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
I'm trying to query the DB with mysqli and then fetch the result, but it's throwing an Object of class could not be converted to a string error.
Here's what I am trying to accomplish:
<?php
include ('conn.php');
$query= "select value from the_table where item = 'url'";
$result = mysqli_query($conn, $query);
?>
And then I am trying to populate a link with the $result:
This is the Link to the URL
I saw this post: PHP and MySQL error: Object of class mysqli_result could not be converted to string
So, I tried to format the echo with echo $result->fetch_object()->url;, but that didn't work.
I'm not sure if I have to fetch the result and then throw it into a look with a mysqli_fetch_array() and if so, how do I get it to populate that the value outside of the loop?
Assuming that $db is your database connection link you can perform the query:
$result = mysqli_query($db, 'select name from fruits');
Then you can get name using procedural style:
echo mysqli_fetch_object($result)->name;
Or object style:
echo $result->fetch_object()->name;
In your code you're trying to output url property which is not defined - as we can see in your query.
Try some thing like this:-
$query= "select value from the_table where item = 'url'";
$result = mysqli_query($conn, $query) or die ("Couldn't execute query.");
// use returned data
while($row = mysqli_fetch_assoc($result))
{
echo $row['value'];
}
OR
while ( $row = mysqli_fetch_assoc( $result ) )
{
foreach ($row as $key => $value)
{
print ($row . " = " . $value . "\n");
}
print("================");
}

Categories