what is the error on my code. it says"
Fatal error: Function name must be a string in C:\wamp\www\Unnamed Site 2\admin_area\view_prod.php on line 27"
<?php
$con = mysqli_connect("localhost","root","","dbname");
$get_prod = "select * from products";
$query_prod = mysqli_query($con,$get_prod);
$i = 0;
while($row_pro = mysqli_fetch_array($query_prod)){
$prod_id = $row_pro('prod_id');
$prod_name = $row_pro('prod_name');
$prod_price = $row_pro('prod_price');
$prod_image = $row_pro('prod_image');
$i++;
?>
<tr>
<td><?php echo $prod_id; ?></td> /*this is line 27/*
<td><?php echo $prod_name; ?></td>
<td><?php echo $prod_image; ?></td>
<td><?php echo $prod_price; ?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php } ?>
Change the brackets on the following lines:
$prod_id = $row_pro('prod_id');
$prod_name = $row_pro('prod_name');
$prod_price = $row_pro('prod_price');
$prod_image = $row_pro('prod_image');
to brackets
$prod_id = $row_pro['prod_id'];
$prod_name = $row_pro['prod_name'];
$prod_price = $row_pro['prod_price'];
$prod_image = $row_pro['prod_image'];
as array identifiers must be in square brackets.
Related
In phpMyAdmin I have a simple query:
SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1
BUT when I try to do this in my clear_recent.php:
<?php $result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1"); ?>
<?php foreach ($result->fetch_assoc() as $row): ?>
<?php while($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
I get an error:
Invalid argument supplied for foreach() in /database/chron/clear_recent.php
Cannot for the life of my figure out what's wrong!!!! Please help!
You should check if the query returns any row. try the following code.
<?php
$result = $conn->query("SELECT * FROM `recent` WHERE datediff(now(), `timestamp`) > 1");
$rows = $result->fetch_assoc();
//check if some rows available.
if (count($rows) > 0) {
foreach ($rows as $row) {
while ($row = $rows) {
?>
<tr>
<td><?php echo($title = $row["id"]); ?></td>
<td><?php echo($title = $row["pid"]); ?></td>
<td><?php echo($title = $row["user_id"]); ?></td>
<td><?php echo($title = $row["timestamp"]); ?></td>
</tr>
<?php
}
}
}
?>
additional question: why you use while loop under the foreach loop? is it right?
You are getting this error because no rows return from this query.
I've been troubleshooting for a while now, and I really can't find any answer. Basically this is what it looks like:
When I type the name of a real user: Nothing posted on the page with an exception of the header
When I type a fake user: User has not been found (direct translation from Norwegian in the code)
<?php
include_once("moduler/head.php");
$user = $_GET['user'];
$query1 = $con->query("SELECT * FROM players WHERE name='$user'");
echo $con->error;
if ($query1->num_rows == 0) {
echo "<h1>Ingen spiller funnet med: $user.</h1>";
return;
}
while ($row == mysqli_fetch_array($query1)) {
$styrkeLvl = $row['styrke'];
$beskyttelseLvl = $row['beskyttelse'];
$bueskytingLvl = $row['bueskyting'];
$trehuggingLvl = $row['trehugging'];
$gruvedriftLvl = $row['gruvedrift'];
$fiskingLvl = $row['fisking'];
$kills = $row['kills'];
$deaths = $row['deaths'];
$rank = $row['rank'];
$money = $row['money'];
$donstatus = $row['donationstatus'];
$lastlogin = $row['lastlogin'];
$regtime = $row['registrationtime'];
$rankName = getRankString($rank);
?>
<h1><?php echo $user; ?></h1>
<table class=\"userView\">
<tbody>
<tr><td>Brukerstatus</td>
<td><?php echo $rankName; ?></td>
</tr>
<tr><td>Donasjon status</td>
<td>D<?php echo $donstatus; ?></td>
</tr>
<tr><td>Styrke level</td>
<td><?php echo $styrkeLvl; ?></td>
</tr>
<tr><td>Beskyttelse level</td>
<td><?php echo $beskyttelseLvl; ?></td>
</tr>
<tr><td>Bueskyting level</td>
<td><?php echo $bueskytingLvl; ?></td>
</tr>
<tr><td>Trehugging level</td>
<td><?php echo $trehuggingLvl; ?></td>
</tr>
<tr><td>Fisking level</td>
<td><?php echo $fiskingLvl; ?></td>
</tr>
<tr><td>Drap</td>
<td><?php echo $kills; ?></td>
</tr>
<tr><td>Dødsfall</td>
<td><?php echo $deaths; ?></td>
</tr>
<tr><td>Sist pålogget</td>
<td><?php echo $lastlogin; ?></td>
</tr>
<tr><td>Registreringsdato</td>
<td><?php echo $regtime; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
Any ideas? I've also tried running the entire thing in an echo with no result
Thanks
All your variables inside the while loop are only available inside the while loop. so you cannot echo them in the tables. so <td><?php echo $rankName; ?></td> will give you an undefined variable error. The same applies for the other variables you are trying to display.
To avoid this you should try to declare your variables globally, or put your table inside the loop.
Also the other issue is that as it is the code inside the loop will never execute because of the double equal (==). You should use double equal (==) for comparison and single equal (=) for assignment.
So you have to change while ($row == mysqli_fetch_array($query1)) to while ($row = mysqli_fetch_array($query1))
I'm trying to make an page where all the things in the table in the database are displayed on the page. But in a nice format like something ebay where it shows the picture on the left and the title in the middle. Whereas I just have a list.
I can only seem to find tutorials which just echo out all the information which I don't think I can style around to make it look like what I want. I also tried using a variable to store it in, but I only get the last thing in my database showing up.
Here is some of my code:
$query = mysql_query("SELECT * FROM BoatsForRent") or die("Error Searching");
$count = mysql_num_rows($query);
if ($count == 0) {
$output = 'No results found';
} else {
while ($row = mysql_fetch_array($query))
{
$id = $row['boatId'];
$title = $row['title'];
$type = $row['type'];
$address = $row['address'];
$city = $row['city'];
$postcode = $row['postcode'];
$rooms = $row['rooms'];
$decks = $row['decks'];
$price = $row['price'];
$title = $row['title'];
echo "$title<br>";
}
}
To style the output of the table, you can do some CSS like this:
strong {background: #0ff; text-align: right; display: inline-block; width: 50px;}
span {display: inline-block;}
And you need to give your HTML as:
echo "<strong>Title</strong> <span>$title</span><br>";
Preview
Fiddle: http://jsfiddle.net/praveenscience/WBh52/
Use divs, spans or tables to display your data in any way you wish to. Here is an e.g.
<table boder="0" cellspacing="5" cellpadding="5">
<tr>
<td>Boat ID</td>
<td>Title</td>
<td>Type</td>
<td>Address</td>
<td>City</td>
<td>Post Code</td>
<td>Rooms</td>
<td>Decks</td>
<td>Price</td>
</tr>
<?php
$id = $row['boatId'];
$title = $row['title'];
$type = $row['type'];
$address = $row['address'];
$city = $row['city'];
$postcode = $row['postcode'];
$rooms = $row['rooms'];
$decks = $row['decks'];
$price = $row['price'];
$count =mysql_num_rows($query);
if($count == 0){
$output = 'No results found';
} else {
while ($row = mysql_fetch_array($query))
{
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['type']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['postcode']; ?></td>
<td><?php echo $row['rooms']; ?></td>
<td><?php echo $row['decks']; ?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php
}
?>
</table>
Alternatively you can put complete table structure inside echo ''. You can now use css either external or inline as per you wish.
You need to put the data into html tags. Here are just some very basic examples. Then you need to use CSS to add styles.
$query = mysql_query("SELECT * FROM BoatsForRent") or die("Error Searching");
$count = mysql_num_rows($query);
if ($count == 0) {
$output = 'No results found';
} else {
while ($row = mysql_fetch_array($query))
{
$id = $row['boatId'];
$title = $row['title'];
$type = $row['type'];
$address = $row['address'];
$city = $row['city'];
$postcode = $row['postcode'];
$rooms = $row['rooms'];
$decks = $row['decks'];
$price = $row['price'];
$title = $row['title'];
echo "<h1>{$title}</h1>";
echo "<h2>{$type}</h2>";
echo "<h3>{$address}, {$city} {$postcode}</h3>";
echo "<p>This boat has {$rooms} rooms and {$decks} decks!</p>";
echo "<p>The selling price is ${$price}</p>";
}
}
The code you have regarded should be in your head section then the output of while should be changed to arrays:
...
while ($row = mysql_fetch_array($query))
{
$id[] = $row['boatId'];
$title[] = $row['title'];
$type[] = $row['type'];
$address[] = $row['address'];
$city[] = $row['city'];
$postcode[] = $row['postcode'];
$rooms[] = $row['rooms'];
$decks[] = $row['decks'];
$price[] = $row['price'];
$title[] = $row['title'];
}
...
In the body section of your page choose good design for a grid or table and use for loop to populate the data:
<table>
<tr>
<td>ID</td>
<td>Title</td>
....
<?php for ($i =0; $i < count($id); $i++){ ?>
<tr>
<td><?php echo $id[$i];?></td>
<td><?php echo $title[$i];?></td>
....
<?php } ?>
</table>
This is my query:
$rs = pg_query("select sj.*, SUM(p.paid_amount) as Total, sj.subject_fee - SUM(p.paid_amount) as Balance
from tbl_subject as sj, tbl_payment_details as p, tbl_student as s, tbl_student_block as sb
where p.student_block_id = sb.student_block_id and s.student_id = sb.student_id and sj.subject_id = sb.subject_id and
sb.student_id = ".$_POST['student']." group by sj.subject_id order by sj.subject_id;");
displayRecordset($rs);
The problem is when I echo $r['Total'], it doesn't show column Total in my query.
This is my function:
$counter = 1;
while ($r = pg_fetch_array($rs, NULL, PGSQL_ASSOC))
{
$total = $r['Total'];
?>
<tr>
<td><?php echo $counter; ?></td>
<td><?php echo $r['subject_id']; ?></td>
<td><?php echo $r['subject_fee']; ?></td>
<td><?php echo $total; ?></td>
<td><?php echo $r['Balance']."Balance"; ?></td>
<td>
Edit
</td>
</tr>
<?php
$counter++;
}
I have the next issue, when I need the code show me all the users in the table always show me one data the first one or the last one if I change the ASC to DESC inside of SELECT..
I need to show me all users... can you please help me with this?
Here the code and the table with the row I need to show:
<?
include '../include/config.php';
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
$id_paciente = $row['id_paciente'];
$id_tipo = $row['id_tipo'];
$nombre = $row['nombre'];
$apellido = $row['apellido'];
$ciudad = $row['ciudad'];
$telefono = $row['telefono'];
$foto = $row['foto'];
}
?>
<tr>
<th><?php echo $id_paciente; ?></th>
<td><img src="../<?php echo $foto;?>" class="image_thumbnail" /></td>
<td><?php echo $nombre; ?></td>
<td><?php echo $apellido; ?></td>
<td><?php echo $id_tipo; ?></td>
<td><?php echo $ciudad; ?></td>
<td><?php echo $telefono; ?></td>
You are echoing your variables outside of the loop.
So, move it inside:
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
foreach ($conn->query($sql) as $row) {
?>
<tr>
<th><?php echo $row['id_paciente'] ?></th>
<td><img src="../<?php echo $row['foto']?>" class="image_thumbnail" /></td>
<td><?php echo $row['nombre'] ?></td>
<td><?php echo $row['apellido'] ?></td>
<td><?php echo $row['id_tipo'] ?></td>
<td><?php echo $row['ciudad'] ?></td>
<td><?php echo $row['telefono'] ?></td>
<tr>
<? } ?>
well I get my answer with my problem...
now I see all the users, the code neccesary is:
<?
$sql = 'SELECT * FROM PACIENTES ORDER BY id_paciente ASC';
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
?>
for somebody want to
Best Regards!