PHP - Counting Number Of Rows In A Table (sql)
Hi,
I have a problem regarding counting rows in a mysql table. Here is the code: Code: [Select] <?php require_once('includes/connection.inc.php'); $conn = dbConnect('read'); $check = 'SELECT COUNT(*) FROM images'; $checkRes = $conn->query($check) or die(mysqli_error()); if($checkRes <= 0) { header('Location: empty_blog.php'); // redirect to another page } else { // do something else... } ?> The error I'm getting: "Notice: Object of class mysqli_result could not be converted to int in C:\xampp\htdocs\photoblog\index.php on line 6" Any help is greatly appreciated! Similar TutorialsFolks, Having trouble here. Forgot how you use the COUNT. Not interested in the num_rows due to it being slow compared to COUNT. I have users table like this: id|username|sponsor_username 0|barand|requinix 1|phpsane|alison 2|mickey_mouse|requinix Now, I want to check if a sponsor username exists or not. Such as does the entry "requinix" exists or not in one of the rows in the "sponsor_username" column. In our example it does twice and so the variable $sponsor_username_count should hold the value "2". Else hold "0". I get error: Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?' at line 1
My code:
$stmt_1 = mysqli_prepare($conn,"SELECT COUNT(sponsor_username) from users = ?"); mysqli_stmt_bind_param($stmt_1,'s',$sponsor_username_count); mysqli_stmt_execute($stmt_1); //Show error if 'users' tbl was not successfully queried". if (!$stmt_1) { echo "ERROR 1: Sorry! Our system is currently experiencing a problem loading this page!"; exit(); } else { mysqli_stmt_bind_result($stmt_1,$sponsor_username_count); mysqli_stmt_fetch($stmt_1); mysqli_stmt_close($stmt_1); //Check if Sponsor Username in Url ($_GET) is already registered or not. If not then show "Invalid Url" or Link message. if ($sponsor_username_count < 1) { echo "<b>Invalid Url or Link!<br> Invalid Sponsor: \"$sponsor_username\"!</b><?php "; exit(); } }
I know how much you guys hate people asking for help without any starting code but I'm not even sure how to start this.. I want a script to read this page and count all the admins and referees. If anyone is willing to help, here's a sample of the html code: Code: [Select] <div class="boxInner"><h3>Arena Referees</h3><table class="list"><thead><tr><th style="width:16px;"></th><th style="width:150px;">Name</th><th style="width:300px;">Position</th><th style="width="150px;">Gamertag</th></tr></thead><tbody><tr><td><a href="http://gamebattles.com/profile/GottaHaveFaith78"><img src="http://media.gamebattles.com/icons/16/profile.png" class="icon16" /></a></td><td class="alt1">Faith</td><td><b>Head Referee</B></td><td class="alt1">GB CombatBarbie</td></tr><tr><td class="alt2"><a href="http://gamebattles.com/profile/Jack.-"><img src="http://media.gamebattles.com/icons/16/profile.png" class="icon16" /></a></td><td>Jack</td><td class="alt2"><b>Asst. Head Referee</b></td><td>Get Jacked x</td></tr><tr><td><a href="http://gamebattles.com/profile/amghawk"><img src="http://media.gamebattles.com/icons/16/profile.png" class="icon16" /></a></td><td class="alt1">Mike</td><td><b>Asst. Head Referee (IS)</b></td><td class="alt1">GB amghawk</td></tr><tr><td class="alt2"><a href="http://gamebattles.com/profile/cory94bailly"><img src="http://media.gamebattles.com/icons/16/profile.png" class="icon16" /></a></td><td>Cory</td><td class="alt2">Referee</td><td>Cory xz</td></tr><tr><td><a href="http://gamebattles.com/profile/crago"><img src="http://media.gamebattles.com/icons/16/profile.png" class="icon16" /></a></td> I'm just wondering how I could even count them. Any help will be appreciated, thanks. hello, all: I'm a newbie, and been trying to work out a way so that the results from an array (or mysql recordset for that matter), are nicely aranged in a table. I want them to show in a grid-like manner, with rows and columns added according to number of "items". I worked out this small code snippet, with a sample array, and for the life of me, cant figure out how to make it so it automatically "breaks" columns at 5 and start new row. I got it to show me right number of rows, but then it repeats all 10 names within them, as opposed to just showing 5 names in each row... Appreciate the help... Code: [Select] <?php $names = array('Charles','Henry','Manny','Philip','Rose','Evelyn','Peter','Julia','Cary','Sophia'); $numberColumns = 5; $numberNames = count($names); $numberRows = ceil($numberNames / $numberColumns); echo "<table border='1' width='400' cellspacing='0' cellpadding='0'>"; for ($i=1; $i<=$numberRows; $i++) { echo "<tr>"; foreach ($names as $name) { echo "<td>" . $name . "</td>"; } echo "</tr>"; } echo "</table>"; ?> Hello I have a table that inserts a new row with data when a member views a page. I wish to count all the rows for each member, and then be able to show what the cuurent members "position" is eg. what members has the highest row count. Example. counting the rows for member_A returns 1000 rows, the number of rows for member_B returns 1500 rows. How can I display for member_A that he is in "second" place? Hi I'm using php file to retrieve the amount of records in my database, but all of a sudden I get 0 results. It worked fine a few days ago. I got PHP 5.2.* on my host. Here is the current script that I use.
Did I made a mistake somewhere?
<?php $con=mysqli_connect("******","*****","*****","****"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="SELECT * FROM mxit"; if ($result=mysqli_query($con,$sql)) { $rowcount=mysqli_num_rows($result); //printf("Result set has %d rows.\n",$rowcount); // Free result set mysqli_free_result($result); } mysqli_close($con); ?> I am getting the result I expected with ORDER BY/GROUP BY queries WITH a troubling EXCEPTION: the data is being IN ORDER from 0 thru 9, but 10 (which in this test sample is my LARGEST quantity) is being inserted between the ONE and 2 quantity. Not sure if this will occur with ALL teen values, as well as hundreds. The column is a SMALLINT field (not sure if that makes a difference)> Is there a solution for this situation? One more question to ask. I'm trying to do a count of total logins that have happened in the last 5 months including the current one so far. My users_logins table has the following structu users_id, session_id, login_date(datetime), last_activity(datetime). What the end result is going to be is: <tr> <td></td> <th scope="col"><?php echo date("M", strtotime("-4 month")); ?></th> <th scope="col"><?php echo date("M", strtotime("-3 month")); ?></th> <th scope="col"><?php echo date("M", strtotime("-2 month")); ?></th> <th scope="col"><?php echo date("M", strtotime("-1 month")); ?></th> <th scope="col"><?php echo date("M"); ?></th> </tr> <tr> <th scope="row">Logins</th> <td>94</td> <td>53</td> <td>124</td> <td>92</td> <td>105</td> </tr> Where the first td would be May and the last td would be September. Do I have to do this in 4 separate queries. If so here's what I have for the first query. I'm not sure what woudl go for the WHERE. $four_months_ago_query = "SELCT COUNT(date_login) FROM users_logins WHERE ? "; Alright, I need to find a way to display the top ten duplicated rows for a table in my database. I have tried multiple methods but I have failed. Can you please assist me with this problem. I currently run a query in phpmyadmin to get the result, but i cant figure out how to properly code the php script. Code: [Select] if ($month <= 9){ if ($list_day <= 9){ $event_day = $year.'-'."0".$month.'-'."0".$list_day; if(isset($events[$event_day])) { foreach($events[$event_day] as $event) { $calendar.= '<div class="event">'.$event['event_date'].'</div>'; } } } Right, using a mysql query the value from $event['event_Date'] prints out in a div. I need a number count of the amount of times this value is printed. any help? I wish to have a page that will display a form if there aren't already enough registrations in the database. The following it an outline of how it will be: <?php require_once "../scripts/connect_to_mysql.php"; //this works fine // Count how many records in the database (1) $sqlCommand = "SELECT * FROM teams"; (2) $sqlCommand = "SELECT COUNT (*) FROM teams"; (3) $sqlCommand = "SELECT COUNT (id) FROM teams"; $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); (1) $numRegistrations = mysql_num_rows($query); //Count how many rows are returned from the query above (2) $numRegistrations = $query (3) $numRegistrations = mysql_num_rows($query); mysqli_free_result($query); ?> (Some HTML code, like DOCTYPE, head, start of body tags) <?php if($numRegistrations > 35){ echo "Sorry, registrations for this event is at it's maximum."; }else{ ?> <form blah blah blah> </form> <?php } ?> (end of body and html tag) You'll see above there is (1), (2), (3). These are the main 3 that I've been trying, and they match up the $sqlCommand to the $numRegistrations. What would be the problem with this code? I use wamp server to test, have been using it for ages. Chrome browser to test in. If I remove the database query, the rest of the page will load. With the query in there, only the HTML code up until the database query is parsed, so therefore nothing is display on the page. Please help. Thanks Denno Hi I wanted to know how once I have selected and echoed the rows from mysql table how can I get it to number those rows that echo? etc 1. echo $variable 2. echo $variable 3. echo $variable 4. echo $variable 5. echo $variable Any help is appreciated I was just wondering when you run a mysql query what the easiest way to find out the number of rows returned. Just so I can do a number of results found on a search. Thanks. Hey guys was wondering if anyone could modify the code below to have $output = 'yes' if the number of rows equal 10, 20, 30, 40, 50, 60, 70, 80......... etc $dbhost = '******'; $dbuser = ''******';'; $dbpass = ''******';'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('MYSQL N/A - Please Contact Admin'); $dbname = ''******';'; mysql_select_db($dbname); $query="SELECT * FROM documentaryinfo"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close($conn); ?> Thanks -------------------------------------------------------------------------------------------------------- Watch Free Documentaries Online - The Documentary Database www.free-documentaries-online.com -------------------------------------------------------------------------------------------------------- Hi,
I am trying to make a list of all countries listed in a database and the number of times they appear. But I am clearly doing something wrong as I am not able to list out the number of times that they appear.
This is what I have got so far:
<h4>Countries</h4> <table><col width='150px'><col width='150px'> <tr> <th> Country </th> <th> Number of Occurences </th> </tr> <?php $location = DB::getInstance()->query("SELECT country, COUNT(country) as countCountry FROM users GROUP BY country ORDER BY count DESC"); foreach ($location->results() as $locations) { echo "<tr><td>"; echo $locations->country; echo "</td><td>"; echo $locations->countCountry; echo "</td></tr>"; } ?> </table>Can anyone point me in the right direction? Hi Guys! Trying to display data in 3 rows horizontally and 25 vertiacally Code: [Select] // Print out result while($row = mysql_fetch_array($result)){ echo "There are ". $row['COUNT(biddersId)'] ." bidders logged."; echo "<br />"; } $query = "SELECT * FROM bidders ORDER BY biddersId"; $result = mysql_query($query) or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Bidders</th>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo"<font face= \"calibri\" size=\"3\">"; echo $row['biddersId']; echo "</br>"; echo "</td><td>"; } echo "</table>"; ?> I have a html table displaying data from mysql database. I can count rows properly with mysql_num_rows but is there a way to echo which row number it is. What I want to do is count the rows ordered by cities and echo row number. Thanks for any help. Hi Please take a look at my code snippet. I don't know how to get the number of rows returned from a MySQL stored procedure so that I can handle it accordingly. Any ideas please? Thanks Hi, I'm trying to get a number of different keywords from many table fields, and i want to count them according to the occurrence of each keyword, so it should be like (keyword1 occurred 10 times for example ). the problem is when i use count in for loop: Code: [Select] $tags = explode("|", $row[0]); $i=1; $count = 0; for($i; $i<count($tags); $i++){ print "$count {$tags[$i]}<br />"; $count++; } it counts the occurrence of key words in each field separately from the rest (i.e. Code: [Select] 0 day 1 night 0sun 1 moon 0 clouds 1 rain ) 2 keywords in each field (some other fields got more). I want the count to be more like: Code: [Select] 1 day 2 night 3 sun 4 moon 5 clouds 6 rain thanks I am in need of suggestions on how too achive this: I have a surf exchange and whenever a member has viewv a site, it ads this members Id and time to a table called vtp_tracking. I have now created a team script, so I can count the total of surf for members within this team from the vtp_table. What I am in need of is this: I would like to add a bonus for the teams, so my idea was this: every hour run a cronjob to get the results for the last hour by team id lik so: Code: [Select] WHERE vtp_members.team_id="teamid" AND vtp_tracking.credit_members_id=vtp_members.id AND vtp_tracking.action_date>date_sub(NOW(),interval 60 minute I believe the above code will work fine and get me the results for team members for the last hour. Problem: how can I get this result, multiple it with the value from another field and divide by 100 and add this to a third field? eg: (result from above * value from other field / 100) then add to a third field. Anyone willing to try and answer this one? |