PHP MYSQLi: Array to string conversion [duplicate] - php

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>

Related

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

I want to get all of my database items [duplicate]

This question already has answers here:
How to select multiple rows from mysql with one query and use them in php
(4 answers)
Closed 6 years ago.
I've included this PHP file in my index.php, but I only get the first item from the database. How can I get all items from my database?
<?php
$sql = "SELECT name,price FROM items ORDER BY `id`";
$res = mysqli_query($conn, $sql);
$page = mysqli_fetch_assoc($res);
mysqli_close($conn);
?>
<h2><?=$page["name"]?></h2>
<?=$page["price"]?>
As you said you want all records from the database, so you need to use while loop like below:-
<?php
$sql = "SELECT name,price FROM items ORDER BY `id`"; // query
$res = mysqli_query($conn, $sql); // execute query
while($page = mysqli_fetch_assoc($res)){ // loop
echo "<h2>". $page["name"]."</h2>".$page["price"]; // print all record (You can change pattern of printing according to your wish )
}
mysqli_close($conn); // close connection
?>
This is because you need to loop your results that are in $page array. Try this:
<?php foreach($page as $value): ?>
<h2><?=$value["name"]?></h2>
<?=$value["price"]?>
<?php endforeach; ?>
if you need more info about foreach method go here

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

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("================");
}

passing array to string not ok [duplicate]

This question already has an answer here:
making an array from database [closed]
(1 answer)
Closed 9 years ago.
i have this script and im having a problem passing this array to a string with commas.
i want it to be a string so i can insert it as a variable inside another query in the select...where... IN($variable);
i tried the implode and it dont echo the ids as a string it echoes Array.
error_reporting(E_ALL);
mysql_connect("localhost", "root", "root");
mysql_select_db("wall");
$sql = "select id from table where id=1";
$result = mysql_query( $sql);
$myArray='';
while($row = mysql_fetch_array($result)){
$popurl = $row['id'];
$myArray[] = $popurl;
}
echo "<pre>";
print_r($myArray);
Try this code:
$myArray= array() ; //Here you must declare it as array
while($row = mysql_fetch_array($result)){
$popurl = $row['id'];
$myArray[] = $popurl;
}
$string = "'" . implode("', '", $myArray) . "'" ;
//Will make ready string like 'data','data2', 'data3'

Categories