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);
Related
I am trying to display my query results on page. However, whenever I run the code although the query is correct it does not display anything.
Can anyone help? Would be muchly appreciated
<?php
session_start();
require_once("../config1.php");
if(isset($_POST["DailySales"])) {
$linkid = mysqli_connect(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Could not connect:" . connect_error());
$sql = "SELECT Retdatetime AS date , sum(rentalrate + overduecharge) AS mny
FROM frs_FilmRental
WHERE shopid='2'
Order BY retdatetime DESC ";
$result = mysqli_query($linkid, $sql);
if (!$result)) {
printf("Errormessage: %s\n", mysqli_error($linkid));
}
echo "<table border = '1' align='center'>";
echo "<th> Shop ID 2</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2><center>Shop ID 2 daily sales : </center></h2>";
echo "<tr><td>";
echo $row['mny'];
echo "</td><td>";
echo $row ['date'];
echo "</td></tr>";
}
}
?>
replace
echo $row ['date'];
by
echo $row ['Retdatetime'];
To understand query errors you should use mysqli_error() in your code. If there are no errors for executed query, then you can run while loop for it.
<?php
session_start();
require_once("../config1.php");
if(isset($_POST["DailySales"])) {
$linkid = mysqli_connect(DB_DATA_SOURCE, DB_USERNAME, DB_PASSWORD, DB_DATABASE) or die("Could not connect:" . connect_error());
$sql = "SELECT Retdatetime , sum(rentalrate + overduecharge) AS mny
FROM frs_FilmRental
WHERE shopid='2'
Order BY retdatetime DESC ";
$result = mysqli_query($linkid, $sql);
if (!$result)) {
printf("Errormessage: %s\n", mysqli_error($linkid));
} else {
echo "<table border = '1' align='center'>";
echo "<tr><th> Shop ID 2</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<h2><center>Shop ID 2 daily sales : </center></h2>";
echo "<tr><td>";
echo $row['mny'];
echo "</td><td>";
echo $row ['date'];
echo "</td></tr>";
}
echo "</table>";
}
}
?>
fixes given in comments also applied
Please copy/paste the error, given by mysqli_error()
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.
I'm trying to get a table to display and keep getting an error. Could someone out there with better coding skills help me?
Here is the code:
<?php
// connect to the database
$host = "###";
$username = '###';
$pass = '###';
mysql_connect($host,$username,$pass) or die(mysql_error());
mysql_select_db("Employees") or die(mysql_error());
// select everything from the table
$query = "SELECT * FROM Employees";
$result = mysql_query($query') or die(mysql_error());
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
}
echo "</tr>";
echo "</table>";
// disconnect from the database
mysql_close();
?>
When the page runs it yields the following error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/content/35/4683335/html/crosshill/display.php on line 36
Line 34 is: echo "<td>".$row['employeeid']."</td>";
I suggest that you need check 3 points:
You could use or die(mysql_error()) in dev environment to check if you have an error (with sql functions related).
Connection
mysql_connect($host,$username,$pass) or die(mysql_error());
Select db
mysql_select_db("Employees") or die(mysql_error());
Query (I see that you query are correct, but probably you table name are wrong, remember that names are case sensitive)
$result = mysql_query($query) or die(mysql_error());
I noticed your database is called Employees as well as your table, is this a mistake or are they both the same name?
You also have syntax error here:
$query = "SELECT * FROM Employees";
$result = mysql_query($query') or die(mysql_error());
Remove the ' so it is like this:
$result = mysql_query($query) or die(mysql_error());
Try this:
$query = "SELECT * FROM Employees";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result)< 1){
echo 'no rows founds';
} else {
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_assoc($result)))
{
echo "<td>".$row['employeeid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['department']."</td>";
}
echo "</tr>";
echo "</table>";
}
Side note:
As suggested by others you should switch to either mysqli_* or perhaps pdo.
I want this data to display all the results, in the query I get 129 results. But when I display it on the page I only get one row. I have used very similar code to get multiple results before, so I know it`s something simple, but I just can't get it. Any thoughts would be greatly appreciated!
<?php
$sql = "SELECT SUM(datamb) AS value_sum FROM maindata GROUP BY phonenumber";
$sql1 = "select dataplan as currentplan from maindata GROUP BY phonenumber";
$sql2 = "SELECT DISTINCT phonenumber AS value_sum1 FROM maindata";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
$result1 = mysql_query($sql2);
if (!$result1) {
echo "Could not successfully run query ($sql1) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result1) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
$result2 = mysql_query($sql2);
if (!$result2) {
echo "Could not successfully run query ($sql2) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result2) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
while ($row = mysql_fetch_assoc($result)){
echo "<TABLE id='display'>";
echo "<td><b>Data Usage This Period: ". ROUND ($row["value_sum"],2) . "MB</b></td> ";
}
while ($row1 = mysql_fetch_assoc($result1)){
echo "<TABLE id='display'>";
echo "<td><b>Data Plan: ". $row1["currentplan"] . "</b></td> ";
}
while ($row2 = mysql_fetch_assoc($result2)){
echo "<TABLE id='display'>";
echo "<td><b>Phone Number: ". $row2["value_sum1"] . "</b></td> ";
}
?>
Updated based on suggestions - Very helpful thank you, I am very close, all values are correct but I can not get them in the same table, thoughts?
TRY To use ,
Loop in you code eg.
$result1 = mysql_query("SELECT DISTINCT phonenumber AS value_sum1 FROM maindata");
echo '<table>';
echo '<tr>';
echo '<th>id</th>';
echo '</tr>';
while($record = mysql_fetch_assoc($result))
{
echo '<tr>';
foreach ($record as $val)
{
echo '<td>'.$val.'</td>';
}
echo '</tr>';
}
echo '</table>';
Add
"LIMIT 0, 1" at the end of query
or
edit query like "select TOP 1 from....."
Lets make it easy on you ;)
have a look here how to use mysql_fetch_assoc
http://php.net/manual/en/function.mysql-fetch-assoc.php
follow the examples, you will be done in a sec.
I am trying to make the results from mysql results clickable links in php. I am new to php and please help.
<?php mysql_connect("localhost", "root", "pass") or die(mysql_error());
mysql_select_db("Branches") or die(mysql_error());
$data = mysql_query("SELECT * FROM items order by ID desc Limit 5") or die(mysql_error());
while ($info = mysql_fetch_array($data)) {
echo $info['title'];
echo " <br>";
echo $info['descr'];
echo "<br>";
echo "<br>";
} ?>
while ($info = mysql_fetch_array($data)) {
echo ''.$info['title'].'';
echo " <br>";
echo $info['descr'];
echo "<br>";
echo "<br>";
}
Then in somefile.php, use $_GET to capture the id and show the results
$id = $_GET['id'];
// pull info from db based on $id
$sql = mysql_query('SELECT * FROM items WHERE ID = "'.$id.'"');
.
.
.
.
just use anchor tag like this
while ($info = mysql_fetch_array($data)) {
echo "".$info['title'];. "";
}
echo '$info['descr']'