I'm actually a little bit confused with all these AJAX techniques. The aim is to display the new rows on my site when new rows are inserted into my database, but i dont know how. The following code has been inserted into my Joomla site threw an extension:
<!-- You can place html anywhere within the source tags -->
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
$today = date("d/m/Y");
$sql = "SELECT * FROM queue WHERE";
$sql =$sql . " Date>='$today' ORDER BY Qnumber Desc LIMIT 3";
// echo $sql;
$con=mysqli_connect("localhost","root","db","pass");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Αριθμός Εξυπηρέτησης</th>
<th>Πελάτες σε Αναμονή</th>
<th>Ώρα</th>
<th>Ημερομηνία</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Qnumber'] . "</td>";
echo "<td>" . $row['WaitingCustomers'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Related
//replace the following with your details. Dbname is your username by default.
$con = mysqli_connect("info20003db.eng.unimelb.edu.au","geehwank","2058","geehwank");
// Check connection
if (mysqli_connect_errno()) {
echo "Could not connect to MySQL for the following reason: " . mysqli_connect_error();
}
/* this lists the name and release date of all action movies */
echo "<table border='1'>";
$result = mysqli_query($con,"SELECT name, release_date FROM Movie WHERE genre = 'action'");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td><td>" . $row['release_date'] . "</td>";
echo "</tr>";
}
mysqli_close($con);
This code is from my uni
and it reads in tables from mysql
and displays them in a table
im a php noob
ive been trying to add headnings to the table called
name, releasedate
how can i do this in code??
can anyone help??
Here you go:
echo "<table border='1'><thead><tr><th>Name</th><th>Release Date</th></tr></thead><tbody>";
$result = mysqli_query($con,"SELECT name, release_date FROM Movie WHERE genre = 'action'");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td><td>" . $row['release_date'] . "</td>";
echo "</tr>";}
mysqli_close($con);
?>
</tbody></table>
Try as below :
<html>
<head><title>Lab F</title></head>
<body>
<h1> Lab F - SELECT Example</h1>
<p>
This PHP code runs an SQL query, and displays the answers in an HTML table.
</p>
<p>
<br>
<?php
//replace the following with your details. Dbname is your username by default.
$con = mysqli_connect("info20003db.eng.unimelb.edu.au","geehwank","2058","geehwank");
// Check connection
if (mysqli_connect_errno()) {
echo "Could not connect to MySQL for the following reason: " . mysqli_connect_error();
}
/* this lists the name and release date of all action movies */
echo "<table border='1'>";
$result = mysqli_query($con,"SELECT name, release_date FROM Movie WHERE genre = 'action'");
echo "<tr><td>Name</td><td>Release Date</td></tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td><td>" . $row['release_date'] . "</td>";
echo "</tr>";}
mysqli_close($con);
?>
</table>
</body>
</html>
<html>
<head>
<meta http-equiv = "content-type" content = "text/html; charset = utf-8" />
<title>Using file functions PHP</title>
</head>
<body>
<h1>Web Development - Lab05</h1>
<?php
require_once("settings.php");
$dbconnect = #mysqli_connect($host, $user, $pswd, $dbnm);
if($dbconnect->connect_errno >0)
{
die('Unable to connecto to database [' . $db->connect_error . ']');
}
$queryResult = "SELECT car_id, make, model, price FROM cars";
echo "<table width='100%' border='1'>";
echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";
//initiate array
$displayrow= mysqli_fetch_array($queryResult);
//initiate while loop to iterate through table
while($displayrow)
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Make'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($dbconnect);
?>
</body>
</html>
This is doing my head in, I cannot figure out why it will not display the actual data apart from the Table header. No matter what I used.
I have tried mysqli_fetch_array, mysqli_fetch_row, mysqli_fetch_assoc but nothing works.
Help and explanation why it was not displaying the data would be much appreciated :)
First: You aren't running a query, you are only putting the query text in a variable. You need to use mysqli_query.
Second: You should add mysqli_fetch_array to the loop.
For example:
while($displayrow = mysqli_fetch_array($queryResult))
{
}
Otherwise you are only getting the first row.
Third: Array keys are case sensitive. There is no $row['ID'], as Jeribo pointed out, it is $row['car_id'] as referenced in your query. $row['Make'] is not the same as $row['make'].
Please Precision to names of field in Query ( car_id,make,...)
while($displayrow= mysql_fetch_assoc($queryResult) )
{
echo "<tr>";
echo "<td>" . $displayrow['car_id'] . "</td>";
echo "<td>" . $displayrow['make'] . "</td>";
echo "<td>" . $displayrow['model'] . "</td>";
echo "<td>" . $displayrow['price'] . "</td>";
echo "</tr>";
}
If you want to query outside you still have to set it in the loop:
$result = $db->query($queryResult)
while($row = $result ->fetch_assoc()){
...
}
a Good Tutorial is shown here: http://codular.com/php-mysqli
$row needs to be initialized so why don't you try:
while($row = mysqli_fetch_array($queryResult))
{
....
}
You have to get the result set first and then try fetching array from result set
<?php
require_once("settings.php");
$dbconnect = #mysqli_connect($host, $user, $pswd, $dbnm);
if($dbconnect->connect_errno >0)
{
die('Unable to connecto to database [' . $db->connect_error . ']');
}
$query = "SELECT car_id, make, model, price FROM cars";
$resultSet=mysqli_query($dbconnect,$query)
echo "<table width='100%' border='1'>";
echo "<tr><th>ID</th><th>Make</th><th>Model</th><th>Price</th></tr>";
//initiate array
$displayrow= mysqli_fetch_array( $resultSet);
//initiate while loop to iterate through table
while($displayrow)
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Make'] . "</td>";
echo "<td>" . $row['Model'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($dbconnect);
?>
http://www.w3schools.com/php/func_mysqli_fetch_array.asp
I need to filter a session table, normally this is how I show the table list.
<?php
$con=mysqli_connect("localhost","root","","esc");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>PIN</th>
<th>Author</th>
<th>Author ID</th>
<th>Entry Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['sid'] . "</td>";
echo "<td>" . $row['spin'] . "</td>";
echo "<td>" . $row['spuname'] . "</td>";
echo "<td>" . $row['suid'] . "</td>";
echo "<td>" . $row['sdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I was wondering if it's possible on this part...
$result = mysqli_query($con,"SELECT * FROM sponsor ;");
to add Where spuname = 'spuname'
I guess the way I'm doing it is wrong.
What I wanted to do is to show filtered tables, so the one that has the session will see only his logs.
mysqli_query("SELECT * FROM sponsor Where spuname = 'spuname'",$con);
try putting $con after your query
You can use where clause in your query. it would be
$result = mysqli_query($con,"SELECT * FROM sponsor Where spuname = 'spuname'");
I'm trying to throw together a simple inventory database for a small customer of mine (I normally don't do WebDev stuff) but I'm a little stumped. I have what I think should work, but I get no results in my table. I know the query is good since I get the expected results when querying directly to the database, unless PHP expects different formatting of my SQL statement. here is my page:
<html>
<head>
<title>Inventory</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","user","pass","db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT
products.name,
products.sku,
inventory.quantityfry,
inventory.quantityjuv,
inventory.quantityadult,
inventory.notes,
inventory.location,
inventory.owner
FROM
products
INNER JOIN
inventory
ON
products.sku=inventory.sku";
$result = mysqli_query($query);
echo "<table border='1'>
<tr>
<th>Species</th>
<th>SKU</th>
<th>Fry Count</th>
<th>Juvie Count</th>
<th>Adult Count</th>
<th>Notes</th>
<th>Location</th>
<th>Owner</th>
</tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['sku'] . "</td>";
echo "<td>" . $row['quantityfry'] . "</td>";
echo "<td>" . $row['quantityjuv'] . "</td>";
echo "<td>" . $row['quantityadult'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['owner'] . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
When I load the page, all I see is my HTML table headers, but no data. No error messages, either. What am I missing?
you don't see error messages because you don't bother checking for them. You're calling mysqli_query incorrectly, and since you don't check for errors, never see them:
$result = mysqli_query($con, $query) or die(mysqli_error($con));
^^^^---required
Since you used it incorrectly, the query call returns false. You then blindly try to fetch result rows from that boolean FALSE, which would lead to further errors and your while() loop never executing at all.
I am trying to display data from mysql using php. Please find the code which I have used. I am only getting the field names in the output. The referred table in the query containing more than 50 rows.
Please help to solve my issue...
<?php
$con=mysqli_connect("localhost","root","password","ayrilmana");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT txn_amount,donated_by FROM tempe_txn");
echo "<table border='1'>
<tr>
<th>txn_amount</th>
<th>donated_by</th>
</tr>";
if($result === FALSE){
die(mysql_error());
}
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['txn_amount'] . "</td>";
echo "<td>" . $row['donated_by'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>