The following PHP program is to search for a student number in database and display the details if found or give a message if it does not exist.
<html>
<body>
<?php
$sno=$_POST['studNo'];
$connection = mysql_connect("localhost", "root", "")
or die("couldn't connect to the server");
$db = mysql_select_db("student", $connection)
or die("<b>connection fails");
$query = "select * from performance where Number = '$sno'";
if($result = mysql_query($query))
{
echo "<table border = 1 align = center>";
echo "<tr>";
echo "<th>Number<th>Name<th>Address<th>Mobile Number";
echo "</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>",$row['Number'],"</th>";
echo "<th>",$row['Name'],"</th>";
echo "<th>",$row['Address'],"</th>";
echo "<th>",$row['MobileNo'],"</th>";
echo "</tr>";
}
echo "</table>";
echo "The student data updated";
}
else
{
echo "<b>Customer number does not exist";
}
mysql_close($connection);
?>
</body>
</html>
If i search for a number which does not exist the else block runs. When i give a number which exists then the if block runs but the while loop does not run. Can anybody help me out? Database field names are correct.
mysql_query() only returns false if there's an error in the query. Not finding any matching rows is not an error. To tell if any rows were found use mysql_num_rows().
$result = mysql_query($query) or die("Query error: " . mysql_error());
if (mysql_num_rows($result) > 0) {
...
} else {
echo "<b>Customer number does not exist</b>";
}
Related
I am trying to create a panel list that shows certain rows from my database, however when I'm trying to call it, it only shows 1 data in the row even though i limit it by 5.
Here is my script:
<?php
$link = mysqli_connect("localhost", "root", "", "test");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM updates order by id desc limit 5";
if($result = mysqli_query($link, $sql)){
if($list=mysqli_fetch_array($result)){
echo "<div class=\"container\">";
echo "<table>";
echo "<tr>";
echo "</tr>";
echo "<div class=\"col-md-4\">";
echo "<div class=\"panel panel-primary\">";
echo "<div class=\"panel-heading\">Updates";
echo "</div>";
echo "<ul class=\"list-group\">";
echo "<li class=\"list-group-item\">";
echo "<b><h4>{$list["updates"]}</b></h4>";
echo "</li>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
echo "</table>";
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
That's because you are fetching only once, here:
if ($list=mysqli_fetch_array($result) )
Instead wrap this up inside a loop like this:
$row_count = mysqli_num_rows($result);
if ($row_count <= 0) {
echo "No records matching your query were found.";
}
else {
// you have results
// panel starts here
while ($list = mysqli_fetch_array($result)) {
// do some magic with your results
}
// panel ends here
mysqli_free_result($result);
}
I think you got the idea, now you can do the rest. If not let us know again. Happy coding!
You use sql to get five data, but you use mysqli_fetch_array() function only took out a line.
error line 8;
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 created a PHP Drop Down which is populated from a MySql Database and works just fine, the problem occurs when I want to post the selected in another script. The question is how to post the data to the other script?
This is the source code of the script that implements the drop downs. Please Help!!!!
<?php
$conn = mysql_connect("localhost", "admin", "admin");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("ekupuvac")) {
echo "Unable to select EKupuvac: " . mysql_error();
exit;
}
$query = "SELECT ImeK, KupuvacID FROM kupuvac ORDER BY Saldo DESC";
$result = mysql_query($query) or die(mysql_error());
if (!$result) {
echo "Could not successfully run query ($query) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so I am exiting";
exit;
}
$dropdown = "<select name='ImeK'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown.= "\r\n<option value='{$row['KupuvacID']}'>{$row['ImeK']}</option>";
}
$dropdown .= "\r\n</select>";
echo"Izberi Kupuvac:";
echo $dropdown;
// Second Combo
$conn = mysql_connect("localhost", "admin", "admin");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("ekupuvac")) {
echo "Unable to select EKupuvac: " . mysql_error();
exit;
}
$query2 = "SELECT ImeP, ProzivodID FROM proizvod ORDER BY ImeP";
$result2 = mysql_query($query2) or die(mysql_error());
if (!$result2) {
echo "Could not successfully run query ($query2) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result2) == 0) {
echo "No rows found, nothing to print so I am exiting";
exit;
}
$dropdown2 = "<select name='ImeP'>";
while($row = mysql_fetch_assoc($result2)) {
$dropdown2.= "\r\n<option value='{$row['ProzivodID']}'>{$row['ImeP']}</option>";
}
$dropdown2.= "\r\n</select>";
echo"<br> Izberi Proizvod:";
echo $dropdown2;
echo"<br>";
mysql_free_result($result);
?>
A <select> box is not enough, you need to enclose it in a form
?>
<form method="post" action="somescript.php">
<?
//your controls go here
?>
</form>
then create somescript.php and access your form variables using $_POST
Also use PDO not mysql_ functions as these arent safe
Okay, none of those responses I used but I fixed it myself.
Im having trouble though trying to sum up all the prices for the books:
Code:
<style type="text/css">
table{font-size:1.11em;}
tr{background-color:#eee; border-top:1px solid #333;}
</style>
<?php
$con = mysql_connect("localhost","root","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bookorama", $con);
$sql="SELECT customers.name, books.title, books.isbn, books.price
FROM customers, orders, order_items, books
WHERE customers.customerID = orders.customerID
AND orders.orderID = order_items.orderID
AND order_items.isbn = books.isbn;";
$result = mysql_query($sql); // You actually have to execute the $sql with mysql_query();
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
echo "<h1 style='color:#3366ff;'>Each customer book orders</h1>";
echo "<table>"; //start the table
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) //Loop through the results
{
//echo each row of the table
echo "<tr>";
echo "<th><strong>Customer Name:</strong><br></th>";
echo "<td>$row[name]</td>";
echo "<th><strong>Book Title</strong><br></th>";
echo "<td>$row[title]</td>";
echo "<th><strong>ISBN</strong><br></th>";
echo "<td>$row[isbn]</td>";
echo "<th><strong>Book Price</strong><br></th>";
echo "<td>$row[price]</td>";
echo "</tr>";
}
echo '</table>'; //close out the table
?>
mysql_query returns FALSE on error, and you didn't check for this before trying to pass it to mysql_fetch_array.
You really should put error checking in your code!
Well maybe $result contains false because of error, add this block after mysql_query call.
if (!$result) {
die('Invalid query: ' . mysql_error());
}
Because your call $result = mysql_query($sql); has an error and returns false. You need to check for errors before you try to use a resource:
$result = mysql_query($sql);
if( !$result ) {
echo "SQL Query failed! " . mysql_error();
}
else {
// rest of your code here
}
this program must output a directory that you searched for how ever if its not found a message must appear that the org_name is not found,i don't know how to do that, i keep trying on some if-else but it just won't output it.
<?php
$con=mysql_connect("localhost","root","");
if(!$con) {
die('could not connect:'.mysql_error());
}
mysql_select_db("final?orgdocs",$con);
$org_name = $_POST["org_name"];
$position = $_POST["position"];
$result = mysql_query("SELECT * FROM directory WHERE org_name = '$org_name' OR position = '$position' ORDER BY org_name");
echo '<TABLE BORDER = "1">';
$result1 = $result;
echo '<TR>'.'<TD>'.'Name'.'</TD>'.'<TD>'.'Organization Name'.'</TD>'.'<TD>'.'Position'.'</TD>'.'<TD>'.'Cell Number'.'</TD>'.'<TD>'.'Email-Add'.'</TD>';
echo '</TR>';
while ( $row = mysql_fetch_array($result1) ){
echo '<TR>'.'<TD>'.$row['name'].'</TD>'.'<TD>'.$row['org_name'].'</TD>';
echo '<TD>'.$row['position'].'</TD>'.'<TD>'.$row['cell_num'].'</TD>'.'<TD>'.$row['email_add'].'</TD>';
echo '</TR>';
}
echo '</TABLE>';
?>
What you want is mysql_num_rows to check your query for how many result rows it has.
http://de2.php.net/manual/en/function.mysql-num-rows.php
if (mysql_num_rows($result)>0) {
// your thing above with mysql_fetch_array($result1) etc
} else {
echo 'nor found';
}