How to fix error notice Array to String conversion [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
I have written this code for fetch data from MySQL Server. There is an error, how to fix it?
<?php
$status=$_GET['status'];
if ($status=="disp") {
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"mainstock");
$res=mysqli_query($link,"SELECT * FROM 1st_year");
echo "<table>";
while ($row=mysqli_fetch_array($res)) {
echo "<tr>";
echo "<td>"; echo $row=["id"]; echo "</td>";
echo "<td>"; echo $row=["BooksName"]; echo "</td>";
echo "<td>"; echo $row=["Avaibality"]; echo "</td>";
echo "<td>"; echo $row=["Price"]; echo "</td>";
echo "<td>"; echo $row=["Quantity"]; echo "</td>";
echo "</tr>";
};
echo "</table>";
}
?>
Error in line 14, 15, 16, 17 which are
echo "<td>"; echo $row=["id"]; echo "</td>";
echo "<td>"; echo $row=["BooksName"]; echo "</td>";
echo "<td>"; echo $row=["Avaibality"]; echo "</td>";
echo "<td>"; echo $row=["Price"]; echo "</td>";
echo "<td>"; echo $row=["Quantity"]; echo "</td>";
Please help me!

echo "<td>". $row["id"] . "</td>";
echo "<td>". $row["BooksName"]. "</td>";
echo "<td>". $row["Avaibality"] . "</td>";
echo "<td>" . $row["Price"]; . "</td>";
echo "<td>". $row["Quantity"]; . "</td>";
fix these lines like above

Related

I am getting "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result" Error in Php [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
Attempting to create display order page for admin, but getting this error in my code. I am not getting the error after so much effort. help me!!
Error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result
Here's my Code:
<div class="grid_10">
<div class="box round first">
<h2>Display Order</h2>
<div class="block">
<?php
$res = mysqli_query($link, "select * from 'confirm_order_address' order by id desc");
echo "<table border='1'>";
echo "<tr>";
echo "<td style='font-weight:bold'>"; echo "fullname"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "mobileno"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "email"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "pincode"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "address"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "landmark"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "city"; echo "</td>";
echo "</tr>";
while($row = mysqli_fetch_array($res))
{
echo "<tr>";
echo "<td>"; echo $row["fullname"]; echo "</td>";
echo "<td>"; echo $row["mobileno"]; echo "</td>";
echo "<td>"; echo $row["email"]; echo "</td>";
echo "<td>"; echo $row["pincode"]; echo "</td>";
echo "<td>"; echo $row["address"]; echo "</td>";
echo "<td>"; echo $row["landmark"]; echo "</td>";
echo "<td>"; echo $row["city"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</div>
</div>
</div>
<div class="grid_10">
<div class="box round first">
<h2>Display Order</h2>
<div class="block">
<?php
$sql_select = 'SELECT * FROM confirm_order_address ORDER BY id DESC';
$result_select = $link->query($sql_select);
if ($result_select->num_rows > 0) {
echo "<table border='1'>";
echo "<tr>";
echo "<td style='font-weight:bold'>"; echo "fullname"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "mobileno"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "email"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "pincode"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "address"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "landmark"; echo "</td>";
echo "<td style='font-weight:bold'>"; echo "city"; echo "</td>";
echo "</tr>";
while ($row_select = $result_select->fetch_assoc()) {
echo "<tr>";
echo "<td>"; echo $row["fullname"]; echo "</td>";
echo "<td>"; echo $row["mobileno"]; echo "</td>";
echo "<td>"; echo $row["email"]; echo "</td>";
echo "<td>"; echo $row["pincode"]; echo "</td>";
echo "<td>"; echo $row["address"]; echo "</td>";
echo "<td>"; echo $row["landmark"]; echo "</td>";
echo "<td>"; echo $row["city"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</div>
</div>
</div>

How do I get my SQL results to appear in one table instead of two within PHP/HTML

I have a PDO connection to a database that returns any results for an item number that is searched. It currently returns each instance in its own table but i would like to see all instances in one table. Here is my code.
if (count($resultssample) > 0) {
echo "<br>";
echo "Number in the business: ".count($resultssample)."<br>";
echo "<br>";
foreach ($resultssample as $r) {
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>site name</th>";
echo "<th>location name</th>";
echo "<th>creation time</th>";
echo "<th>creation date</th>";
echo "<th>damage status</th>";
echo "<th>colour</th>";
echo "<th>inventory status</th>";
echo "<th>notes</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
echo "<tr>";
echo "<td>". $r['site_name'] ."</td>";
echo "<td>". $r['location_name'] ."</td>";
echo "<td>". $r['CreationTime'] ."</td>";
echo "<td>". $r['CreationDate'] ."</td>";
echo "<td>" . $r['damage_status'] . "</td>";
echo "<td>" . $r['colour'] . "</td>";
echo "<td>" . $r['inventory_status'] . "</td>";
echo "<td>" . $r['Notes'] . "</td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
}
} else {
echo "No results found";
}
How can this be achieved I cant currently get my head around it.
Any help would be greatly appreciated.
All you have to do is move table header out of the foreach loop:
if (count($resultssample) > 0) {
echo "<br>";
echo "Number in the business: ".count($resultssample)."<br>";
echo "<br>";
echo "<table>";
echo "<thead>";
echo "<tr>";
echo "<th>site name</th>";
echo "<th>location name</th>";
echo "<th>creation time</th>";
echo "<th>creation date</th>";
echo "<th>damage status</th>";
echo "<th>colour</th>";
echo "<th>inventory status</th>";
echo "<th>notes</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach ($resultssample as $r) {
echo "<tr>";
echo "<td>". $r['site_name'] ."</td>";
echo "<td>". $r['location_name'] ."</td>";
echo "<td>". $r['CreationTime'] ."</td>";
echo "<td>". $r['CreationDate'] ."</td>";
echo "<td>" . $r['damage_status'] . "</td>";
echo "<td>" . $r['colour'] . "</td>";
echo "<td>" . $r['inventory_status'] . "</td>";
echo "<td>" . $r['Notes'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
} else {
echo "No results found";
}

How to set a table cel color based on its value in PHP?

<?php
$res=mysqli_query($con,"select * from tab_cont");
while($rows = mysqli_fetch_assoc($res))
{
echo "<tr>";
echo "<td>"; echo $rows["id"]; echo"</td>";
echo "<td>";echo $rows["date"]; echo "</td>";
echo "<td>";echo $rows["category"];echo "</td>";
echo "<td>";echo $rows["issue"];echo "</td>";
echo "<td>";echo $rows["cr_status"];echo "</td>";
echo "<td>";echo $rows["priority"];echo "</td>";
echo "<td>"; echo $rows["assigned"]; echo "</td>";
echo "<td>"; echo $rows["escalation"]; echo "</td>";
echo "<td>"; echo $rows["action"]; echo "</td>";
echo "<td>"; echo $rows["resol"]; echo "</td>";
echo "<td>"; ?> <a href="edit.php?id=<?php echo $rows["id"];?>"><button type="button" class=btn btn-success">Edit</button> <?php echo "</td>";
echo "<td>"; ?> <a href="delete.php?id=<?php echo $rows["id"];?>"><button type="button" class=btn btn-danger">Delete</button> <?php echo "</td>";
}
?>
Here I want to set the color for cr_status based on its value "open:green" and "red:close".I tried foreach() code, after echo $rows["cr_status"];echo..... . But I got "unexpected foreach()" error in the browser. The code I used is:
foreach($rows["cr_status"] as $cell)
{
if($cell=="OPEN")
{
echo '<td style="background:#FF6347">'.$cell.'</td>';
}
elseif ($cell=="CLOSE")
{
echo '<td style="background:#228B22">'.$cell.'</td>';
}
}
Can anyone, Plz help me with this code?
$rows["cr_status"] isn't an array, so it makes no sense to loop through it. That's the reason for your error. It's not quite clear what you thought this code was supposed to be doing.
If you just want to set the colour of the cr_status cell (and open/close are the only two statuses), then
echo "<td style='background:".($rows["cr_status"] == "OPEN" ? "#FF6347" : "#228B22")."'>".$rows["cr_status"]."</td>";
should do what you need.

Echo Mysql into a table - presentation and DOM

Two questions in one:
First question, mainly about presentation:
I'm echo-ing the following code which should create a table. The table should have a single column, but it's being rendered with the elements above the image as a single line. can anyone see why?
<?php
$sql = "SELECT * FROM catdata WHERE featured='yes' LIMIT 2";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['manufacturer'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td><img src=\"6.diesel.png\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['size'] . "l</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['mileage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['fitsmodel'] . "</td>";
echo "</tr>";
echo "<tr class=\"tablePriceBlock\">";
echo "<td>£" . $row['pricefitted'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>£" . $row['pricedel'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else {
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Unable to execute $sql. " . mysqli_error($link);
}
?>
Question 2:
Can I show the second result in a second column, or as a separate table? Also, is it possible to access the $result elements like say, $[manufacturer][1]?
Always look at the emitted source your code generates by either "View Source" or using a tool like curl. You'll find this mistake:
echo "</tr";
You're missing a >.
Many people who write HTML have browser plugins that can link through to an HTML validator to ensure they've got the correct syntax. You may want to find and install one of these.
To generate the second result is a separate table follow the following code.
echo "<table>";
$count = 1;
while($row = mysqli_fetch_array($result)){
if($count == 2){
echo "<table>";
echo "<tr>";
echo "<td>put your code here</td>";
echo "</tr>"
echo </table>
}else{
echo "<tr>";
echo "<td>" . $row['manufacturer'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "</tr";
echo "<tr>";
echo "<td><img src=\"6.diesel.png\"></td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['size'] . "l</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['mileage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row['fitsmodel'] . "</td>";
echo "</tr>";
echo "<tr class=\"tablePriceBlock\">";
echo "<td>£" . $row['pricefitted'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>£" . $row['pricedel'] . "</td>";
echo "</tr>";
}
$count ++;
}
echo "</table>";
As you are limiting your query to only two record so this method is not bad for that and hope this will help.

How to display blob image in <td>

Here's my database connection code.
I want to display the blob image into td
<?php while ($news = mysql_fetch_assoc($content))
{
echo "<tr>";
echo "<td>" .$news['<img src = "data:image/jpg;base64,'.base64_encode($img).'"/>']. "</td>";
echo "</td>";
echo "<tr>";
echo "<td>" .$news['title']."</td>";
echo "</td>";
echo "<tr>";
echo "<td>" .$news['content']."</td>";
echo "</td>";
echo "<tr>";
echo "<td>" .$news['date']."</td>";
echo "</td>";
echo "<tr>";
echo "<td>" .$news['author']."</td>";
echo "</td>";
}
?>
and I got a error says undefined index
You have a typo in your code. Change your line :
echo "<td>" .$news['<img src = "data:image/jpg;base64,'.base64_encode($img).'"/>']. "</td>";
By
echo "<td><img src='data:image/jpg;base64,".base64_encode($news['img'])."'/></td>";

Categories