Hey all I am stuck on displaying a table on my webapge using php. Everytime I try something new, I seem to get a new error. Im really stuck here I am trying to display a table Client which has Columns ID,Name,Phone,Email. I can't seem to get the data into the table. Can anyone help using mysqli?
<?php
include 'connect.php';
include 'form.php';
echo "<table border=1>";
echo "<tr><th>Id</th><th>Name</th><th>Phone</th><th>Email</th></tr>";
if($result = $mysqli_query("SELECT * from Client")){
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row["ID"];
echo "</td><td>";
echo $row["name"];
echo "</td><td>";
echo $row["email"];
echo "</td><td><a href=delclient.php?id=";
echo $row["id"];
echo ">DEL</a> ";
echo "<a href=addclient.php?id=";
echo $row["id"];
echo ">EDIT</a>";
echo "</td></tr>";
}
echo "</table>";
}
?>
Instead of $mysqli_query, it should be mysqli_query:
if($result = mysqli_query("SELECT * FROM Client")) {
As Tristan points out, your call $mysqli_query() is wrong; it should be either
$mysqli->query("SELECT * from Client")
or
mysqli_query($conn, "SELECT * from Client")
where $conn is the database connection created in connect.php. Given your use of object syntax to reference the result fields, I suspect the first of the above to be the correct one.
Related
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 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;
Can someone take a look at my code? I am trying to display a record from a link from the previous page. When I echo $id (below)... it displays the value of 'id' from the previous page. However the query returns all rows of the table. I had it working earlier. I have since changed something. Pleas point out what I over looking. Thank you, in advance.
<?php
mysql_connect('host', 'userid', 'password') or die (mysql_error());
mysql_select_db('my_database') or die (mysql_error());
//$result = mysql_query("SELECT * from table");
$id= mysql_real_escape_string($_GET["id"]);
echo $id;
$result = mysql_query("SELECT * FROM uatbeta1 WHERE id=".intval($_REQUEST['id']));
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $result;
while($row = mysql_fetch_array($result)){
//Display the results from the current row and a line break
echo "<table border='1'>";
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['company']."</td>";
echo "<td>".$row['constructioncontractor']."</td>";
echo "<td>".$row['weldingcontractor']."</td>";
echo "<td>".$row['ndtcontractor']."</td>";
echo "<td>".$row['weldid']."</td>";
echo "<td>".$row['projectname']."</td>";
echo "<td>".$row['examinationdate']."</td>";
echo "<td>".$row['shift']."</td>";
echo "</tr>";
Why not use $id? You have it nicely escaped, so use that variable:
$result = mysql_query("SELECT * FROM uatbeta1 WHERE id=".$id);
I have a php script that tries to find a particular name in a database where the specified program is $q, a variable passed from an html page. I'm very new to this so I'm having trouble figuring out how to code an if not found, then display type of message. Below is what I currently have:
$sql="SELECT * FROM names WHERE program='".$q."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
And I need it to echo a message saying if nothing was found. I tried looking at NOT IN condition in SQL and http://www.techonthenet.com/sql/exists.php along with other things on the internet but I'm not sure if this is the right thing to use. Any help would be appreciated.
use th php function
mysql_num_rows($result);
to check results found
So your code should be like:
$sql="SELECT * FROM names WHERE program='".addslashes($q)."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
while($row = mysql_fetch_array($result))
{
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
else
echo "No data found";
please note i added
addslashes($q)
in query, in order to avoid SQL injection problems.
$sql="SELECT * FROM names WHERE program='".$q."'";
$result = mysql_query($sql);
$found = false;
while($row = mysql_fetch_array($result))
{
$found = true;
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
if ($found == false)
echo "I found nothing";
I have this simple bit of PHP that should be showing a table of invites:
// connect to the database
$host = '###';
$username = '###';
$pass = '###';
mysql_connect($host,$username,$pass);
mysql_select_db("###");
// select everything from the news table
$query = "SELECT * FROM creathive_applications";
$result = mysql_query($query);
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['url']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
mysql_close();
However it's not working? Any ideas why and how to find out. THANKS :)
Just to confirm, the <table> and <tr> are being outputted and the table name is creathive_applications with a H
Did your query work? You're not checking if $result is false after the query call. The table name sort of appears to have typo in it, possibly it should be "creative_applications" (no h)?
Okay just realised that the damn table was empty! :(
Sorry for wasting people's time!
You should try something less dependant of your database structure :
For example :
echo '<pre>';
print_r($result);
echo '</pre>';
and check if the result is null, or return a completely different set of rows
I think your problem is its not showing up as expected in a 1 row per record layout.
That's because you have your <TR></TR> tags outside your loop through the records.
That section of code should look like the following
while( ($row = mysql_fetch_array($result)))
{
echo "<tr>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['url']."</td>";
echo "</tr>";
}
<table>
<?php
$host = '###';
$username = '###';
$pass = '###';
$con = mysql_connect($host,$username,$pass);
$db = '###';
mysql_select_db($db,$con);
$query = "SELECT * FROM creathive_applications";
$querycon = mysql_query($query,$con);
while($row = mysql_fetch_row($querycon)){
echo "<tr>"
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "</tr>"
}
?>
</table>
use this this will work