Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was making a search box, and I want to count the result, i try to put count but nothing happens.
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("concatenate", $con);
if($_POST['lname'] != '' or $_POST['fname'] != ''){
$searchq = $_POST['lname'];
$searchw = $_POST['fname'];
$result = mysql_query("SELECT * FROM sheet1
WHERE lname like '%$searchq%' AND fname like '%$searchw%' ");
I was hoping to count my result but i can't.
while($row = mysql_fetch_array($result))
{
echo "<tr>" .
"<td><b>" . $row['lname'] . "</b></td>" .
"<td>" . $row['fname'] . "</td>" .
"<td>" . $row['address'] . "</td>" .
"<td>" . $row['telnum'] . "</td>" .
"<td>" . $row['network'] . "</td>" .
"<td>" .
"<select class = \"report\">" .
"<option value = \"Wrong Number\" >" . 'Wrong Number' . "</option>" .
"<option value = \"Discontinue\">" . 'Discontinue' . "</option>" .
"<option value = \"Add Number\">" . 'Add Number' . "</option>" .
"<option value = \"Change Address\">" . 'Change Address' . "</option>" .
"</select>" .
"<input type = \"submit\" value= \"Report\" class= \"classname \" name= \"report\">" .
"</tr>";
}
}
Please help me guys
counting is easy just use mysql_num_rows. this method works for any select query. For queries using update or insert use mysql_affected_rows
$count = mysql_num_rows($result);
Also note that the mysql extension is deprecated. I suggest migrating to mysqli or pdo
After your query execution make sure that returns the result. It should not be 0.
You can check it by printing this line after mysql_query.
echo mysql_num_rows($result)
Hope you get your error.
Simply use mysql_num_rows for your select query
If you are using update or insert query then use mysql_affected_rows
$count = mysql_num_rows($result)
or
$count = mysql_affected_rows($query);
You can use mysql_num_rows() to count the number of rows of your search
$result = mysql_query("SELECT * FROM sheet1
WHERE lname like '%$searchq%' AND fname like '%$searchw%' ");
$num_rows = mysql_num_rows ($result);
On a side note, remember to always check and mysql_real_escape_string() your user input (the values in $_POST).
$count = mysqli_num_rows($result);
mysql_num_rows($result) wont work in updated mysql versions
Related
* if(!empty) retrieves first entry in array and shouldn't be used *
This script searches a database for a url that matches the one submitted by a user in a form. Currently the $query printed by the echo statement works fine in mysql and returns one entry. This script executes and prints the table header, but doesn't enter the while($row =.. ) loop.
Any thoughts or suggestions would be really appreciated. I've used this method before with no trouble so I'm sort of stumped right now.
//1. Query DB for results
$query = "SELECT * FROM projects WHERE projectsurl='".$url."';";
echo "<br>".$query;
$projects = mysqli_query($conn, $query);
// 2. Print Project Results
if(!empty(mysqli_fetch_assoc($projects))){
//Print Project Results
echo "Is this your project?<br>";
echo "<table style=width:'100%'>";
while ($row = mysqli_fetch_assoc($projects)){
echo $tabler . $thh . "Title: <br>" .$row['title'] . $thsp . "No. Rewards: <br>" . $row['rewards'] . $thf . $xtabler;
echo $tabler . $thh . "ID: " .$row['id'] . $thf . $xtabler;
// Echo two rows for the URL Strings because they are longer.
echo $tabler . $thh . "<a href='" . $row['projectsurl'] . "'>Projects</a>" . $thsp . "<a href='" . $row['rewardsurl'] . "'> Rewards " . $thf . $xtabler;
echo "<form id='confirmation' action='../index.php' method='POST'>
<input type='hidden' value='2' name = 'stage'>
<input type='hidden' value='".$row['id']."' name='id'>
<input type='hidden' value='".$row['title']."' name='title'>
<input type='submit' value='Confirm'></form>";
}
echo "</table>";
}else{
//trigger ruby script to search for lost file
echo "Project Not Found. <br>";
}
Oh and the random table stuff is defined elsewhere as
$tabler = "<tr>";
$xtabler = "</tr>";
$thh = "<th><b>";
$thsp = "</th><th>";
$thf = "</b></th>";
$csp = "</td><td>";
$ch = "<td>";
$cf = "</td>";
If there's only one row, then if(!empty(mysqli_fetch_assoc($projects))){ will fetch it and your while loop will not be entered. Instead, to check if a row exists, use mysqli_num_rows($projects).
Also, Reminder, escape your user submitted data before using it in your MySQL query if you haven't already. This can be done by: $query = "SELECT * FROM projects WHERE projectsurl='".mysqli_real_escape_string($conn, $url)."';";
EDIT: Alternatively, prepare your MySQL statements before executing them. This is the preferred method, although both protect you from injections.
I have a table which is full with data from my database. I am now trying to implement a delete feature on that table, I do have the correct syntax to echo checkboxes but I don't know how to format the line correctly so the corresponding checkbox aligns correctly with each row. Any ideas? (I currently have the line echo'd above the closing table tag. Which makes the boxes appear above the table)
$sql = "SELECT * FROM products";
$answer = mysqli_query($connection, $sql);
if ($answer->num_rows > 0)
{
echo "<table><tr><th>ID</th><th>Name</th><th>Description</th><th>Price</th><th>Stock</th><th>Delete Product</th></tr>";
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["product_id"]
echo "0 results";
}
Insert the checkbox in an additional table cell (td).
A <tr> element contains one or more <th> or <td> elements. Another content is not treated as part of that row.
echo "<tr><td>" . $row["product_id"]
. "</td><td>" . $row["product_name"]
. "</td><td> " . $row["product_description"]
. "</td><td> " . $row["product_price"]
. "</td><td> " . $row["product_cost_price"]
. "</td><td> " . $row["product_stock"]
. "</td><td>" . $row["product_ean"]
. "</td><td>" .'<input name="delete['.$row['product_id'].']" type="checkbox">'
. "</td></tr>";
I'm creating a cart system and trying to find a way to have a simple button that once pressed deletes the corresponding row. However, I cannot seem to find a way to dynamically do this with while loop I currently have.
<?php
//connect to DB.
$con = mysqli_connect("localhost", "root", "", "books");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySql: ". mysqli_connect_error();
}
$query = "SELECT * FROM cart WHERE customerID = '$_SESSION['id']'";
$result = mysqli_query($con, $query);
//creates a table for dumping the DB to, loops through the DB and posts the contents elegantly.
echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>" . $row['bookAuthor'] . "</td><td>" . $row['bookTitle'] . "</td><td>" . $row['bookPrice'] . "</td></tr>";
$totalprice += $row['bookPrice'];
}
echo "</table>";
echo "The total present price is: $".$totalprice;
//closes the conncection to the DB
mysqli_close($con);
?>
I've considered trying to put an echo query statement into the database and adding "$row['deletebutton']" to the while loop but I'm not sure that would necessarily work.
The easy way is to create a new page and send the message to this page to delete the item.
So, in the table you add a new column with a link
delete
And in the page you treat this value.
To expand on the comment posted to the question, you could add this to your row:
echo "<tr><td>" . $row['bookAuthor'] . "</td><td>" . $row['bookTitle'] . "</td><td>" . $row['bookPrice'] . "</td><td><input type='checkbox' name='remove[]' value='" . $row['ROW_ID'] . "' /></td></tr>";
And then with the data submitted in the form you could do something like this:
$query = "DELETE FROM cart WHERE ROW_ID IN (" . implode(',',$_POST['remove']) . ")";
Keep in mind to check the value of remove before using it in queries.
Basically I have a ranking-page that I'm working on for my CS:GO server. So far it's working fine, and it's looking like this:
https://puu.sh/aJlTj/b6fb5a7a06.png
And the next-page arrow(s) works fine, but the problem is, at page 3+, it's empty because there haven't been more players on the server yet, and it looks like this:
https://puu.sh/aJlXH/7e69071a2b.png
I am using the answer from Make multiple pages out of a mysql query in order to create the pages for all the players.
My table-printing-part looks like this:
echo "<div class='TableGen'><table border='1'><tr><td>Name</td><td>Wins</td><td>Losses</td><td>ELO</td></tr>";
$query = mysqli_query($db, "SELECT * FROM multi1v1_stats ORDER BY wins DESC LIMIT $offset,15");
while($row = mysqli_fetch_array($query)) {
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['wins'] . "</td><td>" . $row['losses'] . "</td><td>" . $row['rating'] . "</td></tr>";
}
?>
</table>
$offset is declared by using $offset = 15 * intval($_GET['page']); unless it's the first page, then it's set to 0.
I understand that this code will only echo out <tr><td></td></tr>'s as long as there's data to post, so how would I go about checking if the page's cells are empty, then just echo out empty cells? (due to this, I can't use empty-cells: show; either as there are no cells to show, not even empty ones)
Thanks in advance.
I think this is what you want... It basically finds the number of rows that were returned and then continues with blanks until the limit is reached.
$limit = 15;
echo "<div class='TableGen'><table border='1'><tr><td>Name</td><td>Wins</td><td>Losses</td><td>ELO</td></tr>";
$query = mysqli_query($db, "SELECT * FROM multi1v1_stats ORDER BY wins DESC LIMIT $offset,$limit");
$i=0;
while($row = mysqli_fetch_array($query)) {
$i = $i+1;
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['wins'] . "</td><td>" . $row['losses'] . "</td><td>" . $row['rating'] . "</td></tr>";
}
for($i=$i;$i<=$limit;$i++)[
echo "<tr><td> </td><td> </td><td> </td><td> </td></tr>";
}
?>
</table>
I am getting no where with this, I am not getting any output from my echo ,can someone help, thanks in advance...singhy
code below...
$strSQL = "SELECT * FROM <tablename> WHERE id='" . $_GET["serviceName"] . "'";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)){
echo "<dt>Name:</dt><dd>" . $row["serviceType"] . " " . $row["serviceName"] . "</dd>";
echo "<dt>Phone:</dt><dd>" . $row["Phone"] . "</dd>";
echo "<dt>Birthdate:</dt><dd>" . $row["BirthDate"] . "</dd>";
}
// Close the database connection
mysql_close();
?>
<p>Return to the list</p>
</body>
</html>
can someone tell me where i am going wrong, ive tried various options, thanks in advance, singhy
Try this debugging code:
$serviceName = mysql_real_escape_string($_GET['serviceName']); // Read PS note at the end
$strSQL = "SELECT * FROM `tablename` WHERE id='$serviceName'";
$rs = mysql_query($strSQL) or die(mysql_error()); // Display any query error
echo "Total number of rows: ". mysql_num_rows($rs); // Echo number of rows
while($row = mysql_fetch_assoc($rs)){
echo "<dt>Name:</dt><dd>" . $row["serviceType"] . " " . $row["serviceName"] . "</dd>";
echo "<dt>Phone:</dt><dd>" . $row["Phone"] . "</dd>";
echo "<dt>Birthdate:</dt><dd>" . $row["BirthDate"] . "</dd>";
}
Please note
You should escape the $_GET request and never use it directly in a query statement. Use mysql_real_escape_string() for that. (This method will be deprecated, read next bullet)
many of the functions you are using will be deprecated starting php 5.5.0 Alternatively you can use PDO prepared statements
replace
$_GET["serviceName"]
with this
$_GET['serviceName']
use single quotes in $_GET in your case.