The array does not show the information echo [duplicate] - php

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/>";
}

Related

Display the results of a query if the query is executed [duplicate]

This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
What I am trying to do is get the values of the $r variable (returns some vehicle id values) to display on the page. However, it returns the following error.
Recoverable fatal error: Object of class mysqli_result could not be
converted to string in
C:\xampp\htdocs\SAMADHI\system\module\reservation\controller\reservationcontroller.php
on line 166
The problem is on the echo $r statement. I tried echo '$r' but then it shows nothing even though it displays the the message 'vehicle available'.
if ($nor > 0) {
$r = $objs->searchVehicle($vhandover, $vreturn, $seatcap);
if ($r) {
$msg = "Vehicle available";
$status = 1;
echo $r;
} else {
$msg = "Something is not right!";
$status = 0;
}
}
What am I doing wrong here and how can I correct it?
Let me assume, your vehicle table have following column - id, name, type. When your query executes, $r holds an associative array with search result-
$r = [ ['id'=>1, 'name'=>"toyota", 'type'=> "regular"], ....]
This is not any String. So if you want to echo any of the column value you need to mention it like -
echo $r['name'];
But if your query returns multiple results then, you need to put the echo inside a foreach loop.
foreach($r as $row) {
echo $row['name'];
}
UPDATE
If the above code doesn't work for you then try the following-
while($row = $r->fetch_assoc()) {
echo $row['id'];
}
UPDATE: 2
You can use fetch_array($r) -
while ($row = fetch_array($r)) {
echo $row['name'];
}
Hope that clears your concept!

Foreach loop declare variable on select table array [duplicate]

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;

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));
?>

PHP MYSQLi: Array to string conversion [duplicate]

This question already has answers here:
Notice: Array to string conversion in
(6 answers)
Closed 7 years ago.
I want to get the id of a table and make it a variable so I can use it in a link.
But I am getting Notice: Array to string conversion in .
$getpID = "SELECT id from posts";
$res = mysqli_query($conn, $getpID) or die(mysqli_error());
$pid = mysqli_fetch_assoc($res);
<a id='del' href='deletepost.php?del=$pid'>Delete</a>
This is just a part of the code where I have problems.
You need to do like this:-
<?php
$getpID = "SELECT id from posts";
$res = mysqli_query($conn, $getpID) or die(mysqli_error($conn));
while($pid = mysqli_fetch_assoc($res)){
echo "<a id='del' href='deletepost.php?del=$pid['id']'>Delete</a><br/>";
}
Note:- this is because $res is an result-set array object having 1 or more than value. So you need to iterate like this.
$pid is an array, you need to access the ID from the array then you can use it in the link.
Example:
$id = $pid['id'];
<a id="del" href="deletepost.php?del=$id">Delete</a>

Recursive PHP function is not returning a result [duplicate]

This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
Here is my function:
function loop($id) {
unset($result, $sql, $query);
$sql = " SELECT parent_id FROM page_entries WHERE id = '$id' ";
$query = mysql_query($sql) or die(mysql_error());
$result = mysql_fetch_assoc($query) or die(mysql_error());
if ($result['parent_id'] != 0) {
echo $result['parent_id'] . "... looping<br>";
loop($result['parent_id']);
} else {
echo $result['parent_id'] . "... done loop";
return $result['parent_id'];
}
}
echo loop('2');
I'm echoing the parent_id for testing. This is what is output to the browser:
1... looping
0... done loop
Where I'm not sure: the echo loop('2') doesn't echo anything from return $result['id'] if I comment out the echo lines in the function. I've tried testing by changing the return to return 'foo'; and still nothing.
How can I fix it?
At a glance, I think
loop($result['parent_id']);
should be
return loop($result['parent_id']);
otherwise your if branch is returning nothing.

Categories