I have a web-based sign out sheet that displays a list of currently signed out assets (it does this by pulling the variables from an SQL table) I wan to take the same output and send it in an email but every time I try I either get resource id#4 or a blank output. For reasons of confidentiality I can't put up the source code for the mail aspect as its for a client. The below works fine at displaying on the main site. The environment is using WAMP any info would be appropriated.
mysql_connect("127.0.0.1", "helpdesk", "Password" )or die("cannot connect");
mysql_select_db("loaner")or die("cannot select DB");
$table=mysql_query("SELECT * FROM signout");
echo "
<div id='status'> <table cellspacing='0' cellpadding='0' border=''>
<tr>
<th>Equipment Currently Out</th>
<th>By Who</th>
<th>Return Date</th>
</tr>";
while ($row = mysql_fetch_array($table))
{
echo "<tr>";
echo "<td>" . $row['Equip'] . "</td>";
echo "<td>" . $row['User'] . "</td>";
echo "<td>" . $row['Return Date'] . "</td>";
}
echo "</tr>";
echo "</table></div>";
echo "</table></div>";
Note that the line breaks used with $table aren't necessary, I simply left them there to make it easier for you to see what's going on.
mysql_connect('127.0.0.1', 'helpdesk', 'Password' ) or die('cannot connect');
mysql_select_db('loaner')or die('cannot select DB');
$results = mysql_query('SELECT * FROM signout');
/**
* Store the HTML in a variable
*/
$table = "<div id='status'>
<table cellspacing='0' cellpadding='0' border=''>
<tr>
<th>Equipment Currently Out</th>
<th>By Who</th>
<th>Return Date</th>
</tr>";
while ( $row = mysql_fetch_array($results) ) {
$table .= "<tr>";
$table .= "<td>" . $row['Equip'] . "</td>";
$table .= "<td>" . $row['User'] . "</td>";
$table .= "<td>" . $row['Return Date'] . "</td>";
}
$table .= "
</tr>
</table>
</div>";
/**
* Mail the HTML
*/
$body = 'Blah blah blah '.$table.' blah blah blah';
mail($to, $from, $subject, $body);
Related
I am using this syntax to display a PHP Table on my page. I now need to add in a second table directly above this one, but all the syntax I try throws a 500 error. How can I with 1 connection to MSSQL run 2 Select statements and populate 2 individual html tables?
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'UserName';
$option['password'] = 'Password';
$option['database'] = 'DB';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT name, hiredate, bday, payrate, hourlypay from HRData ORDER BY name ASC";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . $res->hiredate . "</td>";
print "<td>" . $res->bday . "</td>";
print "<td>" . $res->payrate . "</td>";
print "<td>" . $res->hourlypay . "</td>";
print "</tr>";
}
}
EDIT
This is the syntax I am trying to adapt, but I keep getting a 500 Error
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'UserName';
$option['password'] = 'Password';
$option['database'] = 'DB';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "SELECT name, MAX(Pay) As PayYTD FROM HRINFO";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>YTD Pay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . "$" . round($res->PayYTD) . "</td>";
print "</tr>";
}
}
<br><br><br>
//Query
$query = $db->getQuery(true);
$query = "SELECT name, hiredate, bday, payrate, hourlypay from HRData ORDER BY name ASC";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->name . "</td>";
print "<td>" . $res->hiredate . "</td>";
print "<td>" . $res->bday . "</td>";
print "<td>" . $res->payrate . "</td>";
print "<td>" . $res->hourlypay . "</td>";
print "</tr>";
}
}
The problem you're having is that you are using the calls incorrectly.
$query = $db->getQuery(true);
$query = "SELECT name, MAX(Pay) As PayYTD FROM HRINFO";
$db->setQuery($query);
The first line will create an object, it doesn't matter which. The object will be in $query.
The second line will immediately destroy the object and assign a string to $query (this is incorrect).
The third line expects an object as a parameter to setQuery, but unfortunately it is a string! Error.
If you want this to work correctly, then you need to use the object in $query correctly.
I'm not a Joomla expert, so I link you to a page for how to do this correctly: https://docs.joomla.org/Selecting_data_using_JDatabase
Put the output in a variable.
$StringOut = '';
$StringOut .= '<table border="1">
<thead>
<tr>
<th>Name </th>
<th>Hire Date </th>
<th>Birthday </th>
<th>Pay Rate </th>
<th>hourlypay </th>
</tr>
</thead>';
foreach ($query as $res)
{
$StringOut .= "<tr>";
$StringOut .= "<td>" . $res->name . "</td>";
$StringOut .= "<td>" . $res->hiredate . "</td>";
$StringOut .= "<td>" . $res->bday . "</td>";
$StringOut .= "<td>" . $res->payrate . "</td>";
$StringOut .= "<td>" . $res->hourlypay . "</td>";
$StringOut .= "</tr>";
}
$StringOut .= '</table>';
#Do other logic to retrieve first table.
#You could put it in a different variable if you like. And print when and whereever you wish.
echo $StringOut;
#Optionally close connection, done.
I am new to PHP coding and don't understand where I have gone wrong, I'm trying to display data from a MySQL database onto an HTML table but doesn't register the column fields, keeps coming up with Notice: Undefined index: First, Notice: Undefined index: last etc. How do I define my column fields?
Edited: here are links to pictures of my database and the error codes I'm receiving: http://imgur.com/E5uohjr
http://imgur.com/BGDn9SQ
Here is my code:
</head>
$con = mysqli_connect('localhost', 'root', '', 'form_database') or die("Can not connect: " . mysqli_error());
$result = mysqli_query($con,"SELECT first, last, phone, class FROM form_submissions");
echo "<table border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Class interested in</th>
</tr>";
while($row = mysqli_fetch_assoc($results)){
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
check if you do have results before displaying, them.
<?php
// Create connection
$con = mysqli_connect('localhost', 'root', '', 'form_database');
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM form_submissions";
$result = mysqli_query($con, $sql);
//check if you get any results
if (mysqli_num_rows($result) > 0) {
echo "<table border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Class interested in</th>
</tr>";
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No results found";
}
mysqli_close($con);
I have a MySQL query that I know to be working in Workbench/Sequel Pro that appears to return empty in my PHP code:
$SQL = "SELECT * FROM staff, invigilation, exam, occupation, room, module
WHERE staffID='$inputID'
AND invigilation.examID=exam.exam_ID
AND exam.exam_ID=occupation.examID
AND room.room_ID=occupation.roomID
AND exam.module_ID=module.module_ID
AND staff.password='$inputPassword'";
$result = mysql_query($SQL);
if($db_field = mysql_fetch_assoc($result)) {
echo "
<table border='1'>
<tr>
<th>Exam ID</th>
<th>Module ID</th>
<th>Module name</th>
<th>Duration</th>
<th>Start time</th>
<th>Room</th>
</tr>";
while ($db_field = mysql_fetch_assoc($result)) {
echo "
<tr>
";
echo "<td>" . $db_field['exam_ID'] . "</td>";
echo "<td>" . $db_field['module_ID'] . "</td>";
echo "<td>" . $db_field['module_name'] . "</td>";
echo "<td>" . $db_field['duration'] . "</td>";
echo "<td>" . $db_field['start_datetime'] . "</td>";
echo "<td>" . $db_field['room_name'] . "</td>";
echo "
</tr>";
}
} else {
echo "Incorrect login details. Please try again";
die();
}
The result of this code is the first table row (Exam ID, Module ID etc.) is displayed but the table content is not.
I cannot work out why this is happening. Other than the SQL query, the code is identical to code used elsewhere (where it is working fine).
You are doing mysql_fetch_assoc twice. The first time it reads the record and prints the table header. The second time there are no more records so it doesn't print a row.
Change
if($db_field = mysql_fetch_assoc($result)) {
to
if(mysql_num_rows($result) > 0) {
Try this. You are using mysql_fetch_assoc($result); two times, First time it get result and second time there is no result.
$SQL = "SELECT * FROM staff, invigilation, exam, occupation, room, module
WHERE staffID='$inputID'
AND invigilation.examID=exam.exam_ID
AND exam.exam_ID=occupation.examID
AND room.room_ID=occupation.roomID
AND exam.module_ID=module.module_ID
AND staff.password='$inputPassword'";
$result = mysql_query($SQL);
$db_field = mysql_fetch_assoc($result)
if($db_field != "" ) {
echo "
<table border='1'>
<tr>
<th>Exam ID</th>
<th>Module ID</th>
<th>Module name</th>
<th>Duration</th>
<th>Start time</th>
<th>Room</th>
</tr>";
while ($db_field) {
echo "
<tr>
";
echo "<td>" . $db_field['exam_ID'] . "</td>";
echo "<td>" . $db_field['module_ID'] . "</td>";
echo "<td>" . $db_field['module_name'] . "</td>";
echo "<td>" . $db_field['duration'] . "</td>";
echo "<td>" . $db_field['start_datetime'] . "</td>";
echo "<td>" . $db_field['room_name'] . "</td>";
echo "
</tr>";
}
} else {
echo "Incorrect login details. Please try again";
die();
}
I'm trying to display members from my database using PHP, however the last name goes in with the first name and I'm not quite sure why... Could anyone point me in the right direction?
<?php
$mysql_db_hostname = "localhost";
$mysql_db_user = "alex";
$mysql_db_password="";
$mysql_db_database="gym";
$con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database");
mysql_select_db($mysql_db_database, $con) or die("Could not select database");
$query = mysql_query("select * from users WHERE Category='Member'");
echo "<table border=1>
<tr>
<th>Users ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>";
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td";
echo "<td>" . $row['Last_Name']."<br>" . "</td";
echo "</tr>";
}
echo "</table";
?>
You aren't closing the tags. Syntax highlighter would show the error.
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td"; <-----------
echo "<td>" . $row['Last_Name']."<br>" . "</td"; <------------
echo "</tr>";
}
You are not closing your <td> tags properly
echo "<td>" . $row['First_Name']."<br>" . "</td>";
//^here
echo "<td>" . $row['Last_Name']."<br>" . "</td>";
//^and here
So the output is all one cell with both variables printed inside
You forgot to close the td on your first name field.
Just change
</td
to
</td>
<?php
// Create connection
$con=mysqli_connect("localhost","root","root");
mysql_select_db("test") or die( "Unable to select database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//***************************** table 1 detail ***********
$query = "SELECT * FROM fraud WHERE a_number=" . $_GET["aNun_id"];
echo "<title>" . $_GET["aNun_id"] . "</title>";
echo "<table width='50%' border='5'>
<tr>
<th>Date & Time</th>
<th>A Number</th>
<th>B Number</th>
<th>Status</th>
</tr>";
$result=mysql_query($query) or die (mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['a_number'] . "</td>";
echo "<td><a href='?bNun_id=".$row['b_number']."'>" . $row['b_number'] . "</a></td>";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
//***************************** table 2 detail ***********
//Here i'm trying to use bNum_id in below query
$query = "SELECT * FROM fraud WHERE b_number=" . $_GET["bNun_id"];
echo "<table width='50%' border='5'>
<tr>
<th>Date & Time</th>
<th>A Number</th>
<th>B Number</th>
<th>Status</th>
</tr>";
$result=mysql_query($query) or die (mysql_error());
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['a_number'] . "</td>";
echo "<td>" . $row['b_number'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
//connection close
mysqli_close($con);
?>
Here in table one , I used aNun_id to get the data from another PHP file with "GET" command.
In the table two, I want bNun_id to show its related data in that table. How do i call bNun _id data to the second table from table one. Pls help me..
Are those two tables in the same page?
If so, You need to append the bNum_id to the same query string to use both aNum_id & bNum_id.
echo "<td><a href='?aNun_id=".$_GET["aNun_id"]."&bNun_id=".$row['b_number']."'>" . $row['b_number'] . "</a></td>";