I'm attempting to populate a table in HTML with data from a DB. It's not working properly (a blank white page is displayed), but I can't find the source of the error.
<?php
$sql = "SELECT * FROM Orders";
$result = mysql_query($sql)or die(mysql_error());
echo "<table>";
while($row = mysql_fetch_array($result)){
$order_id = $row['orderID'];
$order_due = $row['order_due'];
$order_subject = $row['order_subject'];
$order_level = $row['order_level'];
$order_pages = $row['order_pages'];
$order_cost = $row['order_cost']);
echo "<tr><td>".$order_id."</td><td>".$order_due."</td><td>".$order_subject."</td><td>".$order_level."</td><td>".$order_pages."</td><td>".$order_cost."</td></tr>";
}
echo "</table>";
?>
$order_cost = $row['order_cost']);
you have an extra paranthesis
Also change this
while($row = mysql_fetch_array($result))
to
while($row = mysql_fetch_assoc($result))
I think you need to change third line to:
result = mysql_query($sql) or die(mysql_error());
(space before "or")
Related
How to get total number of row by using this function php mysql ?
i use this code for display data from mysql. It's work good,
Buy i want to know can i get total number of row by using this code ?
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
Try this mysql_num_rows ($result)
Use this line of code
<?PHP
include("connect.php");
$query = "SELECT * FROM table";
$result = mysql_query($query) or die(mysql_error());
$number_of_rows = mysql_num_rows($result); // this will return the number of rows found by the $result query.
echo $number_of_rows;
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
}
?>
I am creating a function that will retrieve an id number from a table with multiple columns, put it in a variable which i want to use to send as a "claims number" in an email to a claimant and also echo on a confirmation screen. Here's what I have so far.
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$claim_id = $result;
$result = mysql_fetch_array(mysql_query($sql));
$claim_id = $result['id'];
Supposing you are sure that you will receive 1 row.
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$c = mysql_fetch_assoc($result);
$claim_id = $c['lname'];
NB*: And get ready for a lecture. Someone is going to tell you that mysql is depracated.
Start using MySQLi or PDO (recommended).
Try :
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$claim_id = $row[0];
or
$sql = "SELECT id FROM warranty_claim where lname=$lname";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$claim_id = $row['id'];
}
$row = mysql_fetch_array($result);
$claim_id = $row['id'];
please note that you should also check for errors (e.g. if($result === FALSE)). Also, there are other ways to fetch data - I recommend looking at mysql_fetch_array, mysql_fetch_row, mysql_fetch_assoc and mysql_fetch_object
Also, don't rely on the statement always returning exactly one row - make sure that the result is as expected: mysql_num_rows
Give this a try:
$sql = "SELECT id FROM warranty_claim where lname='".$lname."'";
$result = mysql_query($sql);
$objResult = mysql_fetch_array($result);
$claim_id = $objResult['id'];
Hy,
I'm new in php and I'm having some problems with mysql_fetch_array().
$sql = "SELECT mandant_kurz FROM pu_mandant ORDER BY mandant_kurz ASC";
$result = mysql_query($sql);
$list = mysql_fetch_array($result);
There are more than 100 entries in the database but the mysql_fetch_array() delivers only one. When I'm trying it with a while-loop it isn't working either.
Here it goes with my while-loop
$sql = "SELECT mandant_kurz FROM pu_mandant ORDER BY mandant_kurz ASC";
$result = mysql_query($sql);
while($list = mysql_fetch_array($result));
Update:
You are not doing anything inside your loop:
while($list = mysql_fetch_array($result));
Try:
while($list = mysql_fetch_array($result){
echo $list['mandant_kurz'];
}
Also try running your query in MySQL client to make sure it actually returns 100 rows.
You will have to use loop:
while($row = mysql_fetch_array($result)){
echo $row['mandant_kurz'];
}
This echoes just first row.
$list = mysql_fetch_array($result);
echo $list['mandant_kurz'];
Moves pointer to first row and echoes all rows
mysql_data_seek($result,0);
while( $list = mysql_fetch_array($result) ) {
echo $list['mandant_kurz'];
}
Well first of all I am French so I hope my question will be understandable ;-)
I know some people have already experienced problems with queries in php that worked in phpmyadmin. The thing is that each time (or so) these people had "echo" their queries and copy/paste in phpmyadmin, but as php does not always display spaces it was always the problem.
Actually my problem is different :
if I use the query
$sql = "SELECT * FROM jos_dtregister_invoice_sent";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
it returns all the rows in both phpmyadmin and my php code, but if I want to look in a different table (with same structure), it just works in phpmyadmin and not via my php code (only one row instead of all of them)
Here is the query not working:
$sql = "SELECT * FROM jos_dtregister_receipt_sent";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
Perhaps the answer is very simple but I admit this is kind of tricky for me...
Many thanks in advance!
Here is my complete code :
function sendReceipt($row) {
$to = getUserInformation($row,10);
$from = getEventAdminEmailFromEmail($row);
$subject = getEventTitle($row)." - Invoice #".$row["confirmNum"];
$message = $row["userFirstName"]." ".$row["userLastName"]." \n\n".getMessageToSendUser($row);
$headers = "From: ".$from."\r\n";
$sql1 = "SELECT * FROM `jos_dtregister_invoice_sent`";
$query1 = mysql_query($sql1);
echo 'Fetched rows number: '.mysql_num_rows($query1)."<br />";
while($row1 = mysql_fetch_array($query1)) {
echo "Invoice Sent: ".$row1["sent"]."<br />";
}
$sql2 = "SELECT * FROM `jos_dtregister_receipt_sent`";
$query2 = mysql_query($sql2);
echo 'Fetched rows number: '.mysql_num_rows($query2)."<br />";
while($row2 = mysql_fetch_array($query2)) {
echo "Receipt Sent: ".$row2["sent"]."<br />";
}
}
mysql_fetch_array() fetches only a row.The name suggests that it fetches a result row as an array
$sql = "SELECT * FROM jos_dtregister_receipt_sent";
$query = mysql_query($sql) or die(mysql_error());
$result = mysql_fetch_array($query);
Try to execute this. The mysql_error() line will spit out what exactly went wrong when trying to execute the SQL. Post it back here and we can help you a little more.
$sql = "SELECT * FROM jos_dtregister_receipt_sent";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
// $row will have your data
}
You need to loop over the results with mysql_fetch_array().
while ($result = mysql_fetch_array($query)) {
print_r($result);
}
Or if you want all your results in one big array ($results):
$results = array();
while ($row = mysql_fetch_array($query)) {
$results[] = $row;
}
I have my data stored in a MySQL table, which includes an auto_increment ID number (unique) for each new row.
I'd like users to be able to get a certain ID number, using the $_GET function.
eg. User loads http://mysite.com/id.php?id=123
Page displays ID number 123 along with the row.
echo $row['id'];
echo "<table>";
echo "<tr> <th>Unit</th> <th>Message</th> <th>Date</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['title'];
echo "</td><td>";
echo $row['description'];
echo "</td><td>";
echo $row['pubDate'];
echo "</td></tr>";
}
echo "</table>";
echo "</center>";
I'm stuck as to where I put the $_GET bit.
Thanks :)
You should append it to your query (using intval to avoid SQL injection) like this:
// use the id in your WHERE clause, convert it to an integer to avoid sql injections
$query = 'SELECT fields FROM table WHERE id = ' . intval($_GET['id']);
$result = mysql_query($query);
$row = mysql_fetch_row($result);
... do stuff with $row ...
Firstly, your code does not make much sense, since you use $row before it was defined.
Secondly, $result isn't defined at all, and it should be, for example like this:
$id = intval($_GET['id']);
$result = mysql_query("SELECT FROM table WHERE id = '$id'");
And now you know how and where to use $_GET['id'].
Dont waste your time doing the comparison afterwards, you'll save yourself alot of time by adding it to the original query
$id = intval($_GET['id']);
$query = "SELECT whatever FROM table WHERE id=$id";
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `Table` WHERE `id`='" . $id . "'";
$res = mysql_query ($query);
$exist = mysql_num_rows($res);
if ($exist) {
$row = mysqlfetch_assoc($res);
...
}