I'm not that good in programming PHP, and still learning from it.
Here's my problem, I need to display the result of rows from two different tables, had researched things and tried but all failed.
Hope someone could give some advice with my line of code.
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild* FROM tblparent";
$num_results = $result->num_rows;
$result = $mysqli->query( $query );
if( $num_results ){
echo "<center><table border='1' id='members'>";
echo "<tr>";
echo "<th>Parent ID</th>";
echo "<th>Parent Firstname</th>";
echo "<th>Parent Lastname</th>";
echo "<th>Parent Middlename</th>";
echo "<th>Child ID</th>";
echo "<th>Child Firstname</th>";
echo "<th>Child Middlename</th>";
echo "<th>Child Lastname</th>";
echo "<th>Action</th>";
echo "</tr>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo "<tr>";
echo "<td>{$Parent_ID}</td>";
echo "<td>{$PFname}</td>";
echo "<td>{$PLname}</td>";
echo "<td>{$PMname}</td>";
echo "<td>{$Child_ID}</td>";
echo "<td>{$CFname}</td>";
echo "<td>{$CMname}</td>";
echo "<td>{$CLname}</td>";
echo "<td>";
echo "<a href='#' onclick='delete_mem( {$Parent_ID} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else{
echo "No records found.";
}
$result->free();
$mysqli->close();
I see two mistakes:
you should satisfy the right order of statements, $result should be
before $num_results assigment.
it seems that there is a mistake in your SQL query.
You need to adjust the following code, I am assuming that tblparent has an id and tblchild has a relation to tblparent id as parent_id:
$query = "SELECT tblparent.*, tblchild.* FROM tblparent, tblchild WHERE tblparent.id = tblchild.parent_id";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
Related
<?php
$sql = "SELECT * from movieinfo";
$sql_data = mysqli_query($db,$sql);
echo "<table>";
echo "<tr>";
echo "<td><b>Movie Name</b></td>";
echo"<td><center><b>Delete</b></center></td>";
echo"</tr>";
while($row = mysqli_fetch_array($sql_data,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td style='color:white'>";
echo $row['title'];
echo "<td><center><a href='adminpage.php?mid={$row['movieid']}'><button
class='contact100-form-btn' name='deletem'>Delete Movie</button></a>
</center></td>";
$_SESSION['delete'] = $row['movieid'];
echo "<td>";
echo"</tr>";
}
echo "</table>";
?>
if (isset($_POST['deletem'])) {
$delete=$_SESSION['delete'];
$query6 = "DELETE FROM movieinfo WHERE movieid=$delete";
mysqli_query($db, $query6);
}
i need to delete the particular row with the id selected so i am storing in session the id and deleting it but when i press on the deleting button it deletes the last row only its because session is storing all the id's because of loop how should i fix it?
<?php
$sql = "SELECT * from movieinfo";
$sql_data = mysqli_query($db,$sql);
echo "<table>";
echo "<tr>";
echo "<td><b>Movie Name</b></td>";
echo"<td><center><b>Delete</b></center></td>";
echo"</tr>";
while($row = mysqli_fetch_array($sql_data,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td style='color:white'>";
echo $row['title'];
**echo "</td>";
$mid = isset($_GET['mid']) ? $_GET['mid'] : '';
$del = "DELETE FROM movieinfo WHERE movieid=$mid";
mysqli_query($db, $del);
{
echo "<td><center><a href='adminpage.php?mid={$row['movieid']}'><button
class='contact100-form-btn' name='deletem'>Delete Movie</button></a>
</center></td>";
}
echo"</tr>";
}
echo "</table>";
?>
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I am currently trying to output a the results of a MySQL table in PHP. I have a general understanding of web based programming but not enough to debug my code. I know the SQL is good, and the database is linked to my site it's just a matter of making it post to a table. I will post the code and would appreciate some help:
<?php
$sql = "SELECT player_name AS 'Name',
position AS 'Position',
team AS 'Team',
opp AS 'Opponent'
FROM `dbname`
WHERE position = 'QB'";
$stmt = $db->query($sql);
if($stmt-> num_rows > 0) {
echo "<table class='table'>";
echo "<thead class='thead-inverse'>";
echo "<tr><th>Name</th><th>Position</th><th>Team</th><th>Opponent</th>";
echo "</thead>";
echo "<tbody>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr><td>";
echo $row['Name'];
echo "</td><td>";
echo $row['Position'];
echo "</td><td>";
echo $row['Team'];
echo "</td><td>";
echo $row['Opponent'];
echo "</td></tr>";
}
echo "</tbody>";
echo "</table>";
}
else {
echo "No Results";
}
All that I get from this is a no results output.
You forgot to close the query with a double quote.
<?php
$sql = "SELECT player_name AS 'Name',
position AS 'Position',
team AS 'Team',
opp AS 'Opponent'
FROM `dbname`
WHERE position = 'QB'";
$stmt = $db->query($sql);
$stmt -> execute();
if($stmt-> num_rows > 0) {
echo "<table class='table'>";
echo "<thead class='thead-inverse'>";
echo "<tr><th>Name</th><th>Position</th><th>Team</th><th>Opponent</th>";
echo "</thead>";
echo "<tbody>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr><td>";
echo $row['Name'];
echo "</td><td>";
echo $row['Position'];
echo "</td><td>";
echo $row['Team'];
echo "</td><td>";
echo $row['Opponent'];
echo "</td></tr>";
}
echo "</tbody>";
echo "</table>";
}
else {
echo "No Results";
}
Add $stmt -> execute();
You can also use $stmt -> store_result(); after the execute part
I am currently working on a PHP and SQLite application. It does have HTML for the webpages being created. I am currently using PDO to connect the database to my online server. When I get to the webpage that allows me to type in what I am searching for then it will display what I have found in the echo statements below. I want to be able to have just the item name, that acts as a hyperlink; when it is clicked on it will go to another webpage (I believe) that will display the item's name, amount, and a short description. Is there a way to use PHP for this action or should I go with the HTML tagging?
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>Amount</td>";
echo "<td>Detail</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
echo "<tr>";
echo "<td>$row[Name]</td>";
echo "<td>$row[Amount]</td>";
echo "<td>$row[Detail]</td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
from what i understand
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
$name = $row['Name'];
echo "<tr>";
echo "<td><a href='details.php?name={$name}'>{$name}</a></td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
To use Associative Arrays within double quotes, you need to use curly braces:
echo "<td>{$row['Name']}</td><td>{$row['Amount']}</td><td>{$row['Detail']}</td>";
I'm having trouble echoing data into a HTML table.
It comes out like that:
But it should be:
Here's the code. What am I doing wrong?
<?php
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM norse5_proov
WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "<br>";
echo "</table>";
}
}
else{
echo "No results";
}
}
?>
The problem is that you keep outputting a new table for each iteration.
Your code should look like this:
<?php
$query = $_POST['query'];
$min_length = 1;
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM norse5_proov
WHERE (`model` LIKE '%".$query."%') OR (`year` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
echo "<table>"; // Start the table
// Output the table headers
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)) {
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "<br>";
echo "</tr>";
}
echo "</table>"; // End the table
}
else{
echo "No results";
}
}
?>
just put echo "<table>"; and first tr creation statment out side of while loop and also put closing of table after finishing of while loop and see i am sure it will work.
Try
<?php
echo "<table><tr><td>Model name</td><td>Year</td></tr>";
while($results = mysql_fetch_array($raw_results))
{
echo "<tr><td>".$results['mudeli_nimetus']."</td><td>".$results['soetusaasta']."</td></tr>";
}
echo "</table>";
?>
use this code, you have to first start the table, use while loop to iterate the result, and then close the table.
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}
echo "</table>";
Remove table code from while loop and put outside.
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}
echo "</table>";
replace your if block, use the following code , hope it may help you
if(mysql_num_rows($raw_results) > 0)
{
echo "<table>";
echo "<tr>";
echo "<td>Model name</td>";
echo "<td>Year</td>";
echo "</tr>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr>";
echo "<td>".$results['mudeli_nimetus']."</td>";
echo "<td>".$results['soetusaasta']."</td>";
echo "</tr>";
}echo "</table>";
}
I am trying to execute this query but i got error " Undefined index:
lname".I want to count row from one column(fname) from table a and
select column(lname) from other table b. so please help me.
$result = mysql_query("SELECT COUNT(fname),lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
If you still get an error you can try to fetch both separately:
$result = mysql_query("SELECT COUNT(fname) FROM a");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}
$result1 = mysql_query("SELECT lname FROM b");
while ($row = mysql_fetch_array($result1))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td></tr>";
}
You need to use an alias. Use this:
$result = mysql_query("SELECT COUNT(fname) AS countfname,lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['countfname'];
echo "</td></tr>";
}
Try this code:
$result = mysql_query("SELECT COUNT(a.fname) as fname,b.lname as lname FROM a,b");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['lname'];
echo "</td>";
echo "<td>";
echo $row['COUNT(fname)'];
echo "</td></tr>";
}