PHP - Counting Number Of Rows Via Count() And Not Num_rows
Folks, 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(); } }
Similar TutorialsHi, 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! 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. I'm creating a login that uses PDO statements, however when I try to count how many rows was selected, it gives me an error mentioning its an unidentified property, I always tried rowCount, but it did not work either... Notice: Undefined property: PDOStatement::$num_rows in /Users/JPFoster/Sites/Jaipai.Blog/engine.php on line 50 Here's my class method: Code: [Select] public function login($user, $pw){ $user = addslashes($this->user); $pw = md5($pw); $query = "SELECT * FROM `users` WHERE `username` = '".$user."' AND `password` = '".$pw."'"; //set up query $results = $this->pdo->query($query); if ($results->num_rows == 1){ $row = $results->fetch_assoc(); echo "Worked!"; //$this->set_session($row['id'], $row['username'], $row['email']); } else { echo "Your Username and Password did not match"; } //if user + passs match { redirect (member page) and set up sessions // Else ... Retry... } Any help with this will be greatly appreciated! 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 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. 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 ? "; 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? 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 guys, I want to count the number of rows in a table and want to display them. However that field stays empty. Everythign else works. What do I do wrong? Code: [Select] <?php //$sql="SELECT * FROM $tbl_name ORDER BY user_id"; $sql="SELECT * from sp_users,sp_schools where sp_users.user_id=sp_schools.school_id"; $result=mysql_query($sql); $num_rows=mysql_num_rows($result); ?> <style type="text/css"> <!-- .style2 {font-weight: bold} .style3 { font-family: Arial, Helvetica, sans-serif; color: #000000; } .style10 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color: #000000; } --> </style> <title>User overview</title><table width="779" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#996600"> <tr> <td width="777"> <div align="left"> <table width="779" border="1" cellspacing="0" cellpadding="3"> <tr> <td colspan="6" align="center"><div align="center" class="style1 style3"><strong>SchoolPorta.com Users </strong></div></td> </tr> <tr> <td width="25" align="center"><span class="style2">No.</span></td> <td width="62" align="center"><span class="style2">Name</span></td> <td width="104" align="center"><span class="style2">Lastname</span></td> <td width="130" align="center"><span class="style2">Email</span></td> <td width="342" align="center"><span class="style2">school</span></td> <td width="64" align="center"><span class="style2">Update</span></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><span class="style10"><? echo $rows['num_rows']; ?></span></td> <td><span class="style10"><? echo $rows['user_first_name']; ?></span></td> <td><span class="style10"><? echo $rows['user_surname']; ?></span></td> <td><span class="style10"><a href="mailto:<?php echo $rows['user_login']; ?>"><?php echo $rows['user_login']; ?></a></span></td> <td><span class="style10"><? echo $rows['school_name']; ?></span></td> <td align="center"><a href="update.php?id=<? echo $rows['user_id']; ?>" class="style10">update</a></td> </tr> <?php } ?> Can any one tell me whats the best ( actually error free ) which i can use to count the number of rows finally resulting after select command. I use this ... Code: [Select] $sql = mysql_query("select * from usname where name1='$name1' and name2='$name2' and amm='$amm'"); $num=mysql_num_rows($sql); echo $num; But it always gives the same error.... and the error is.......... Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\money\insert.php on line 20 where line 20 is... Code: [Select] $num=mysql_num_rows($sql); - Pranshu Agrawal pranshu.a.11@gmail.com I have a mysql table that holds data for cars - each car has it's own ID, name, and year. Some cars are repeated. Example: (ID - year - car name) 0001 - 2009 - Honda Civic 0002 - 2008 - Toyota Prius 0003 - 2009 - Honda Civic 0004 - 2008 - Toyota Prius 0005 - 2008 - Toyota Prius 0006 - 2007 - Honda Civic How would I count how many of the same type of cars I have in that table and then display the quantity next to the car name? Like for the above example it would be: 2009 Honda Civic: 2 2007 Honda Civic: 1 2008 Toyota Prius: 3 Thanks in advance for any help :D Hi. I having trouble with counting rows in phpmyadmin. It works fine this way: Code: [Select] $result_rows = mysql_query("SELECT * FROM events"); But when i modify the code to this it doesnt work at all. Code: [Select] $result_rows = mysql_query("SELECT * FROM events WHERE category = 'adults' ORDER BY 'date'"); Any idea what is wrong? Hi I'm not at all good at PHP and haven't really played with it for some years, so bare with me if code examples are old I'm looking for a simple solution to count rows (MySQL), without using multiple queries. The below code is quite simple, counting rows where 'Department' is 'Sales': $sql ="SELECT * FROM DB WHERE Department ='Sales'"; $result=mysqli_query($con,$sql); $rowcount=mysqli_num_rows($result); But what If I need to count the rows for the different departments?: $sql ="SELECT * FROM DB WHERE Department ='Sales'"; $result=mysqli_query($con,$sql); $rowcount=mysqli_num_rows($result); $sql ="SELECT * FROM DB WHERE Department ='Support'"; $result=mysqli_query($con,$sql); $rowcount=mysqli_num_rows($result); $sql ="SELECT * FROM DB WHERE Department ='Online'"; $result=mysqli_query($con,$sql); $rowcount=mysqli_num_rows($result); There must be a better / simpler way to this with a single query? Hope for someone to show me the light
PS. Once this is resolved, I'll probably need further help, to accomplish my final result. month a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 1 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 [20] [21] 22 23 24 25 [26] 27 28 29 30 31 2 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 3 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 4 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 5 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 6 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 7 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 8 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 9 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 10 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 11 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 12 puudu 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ; i need help .. with calculating how many days are between example: january = 1 ; day =23 to feb =2; day 23 in php ... how can i do that Hi Everyone, I am working on implementing a blog comment system using the query below to display comments. My question is what is the best way to do a row count for the id column with this query or do I need to do a second db query to accomplish this? The purpose of this is to echo out the number of comments for that post, before displaying them. Any help is appreciated. Thanks in advance, kaiman <?php // get url variables $post_id = mysql_real_escape_string($_GET['id']); include ("../../scripts/includes/nl2p.inc.php"); // connects to server and selects database include ("../../scripts/includes/dbconnect.inc.php"); // table name $tbl_name3="blog_comments"; // select info from comments database $result3 = mysql_query ("SELECT count(*) FROM $table_name3 WHERE id='$post_id' ORDER BY id DESC LIMIT 1") or trigger_error("A mysql error has occurred!"); if (mysql_num_rows($result3) > 0 ) { while($row = mysql_fetch_array($result3)) { extract($row); // display number of comments // display date $row_date = strtotime($row['date']); putenv("TZ=America/Denver"); echo "<p class=\post\">On ".date('F, jS, Y', $row_date)." "; // display commenter name if($row['url'] == "") { echo $row['name']." wrote:</p>\n"; } else { echo "<a href=\"".$row['url']."\" target=\"_blank\">".$row['name']."</a> wrote:</p>\n"; } // display content $comments = $row['comment']; echo nl2p($comments); } } else { echo "<p class=\"large_spacer\">No Comments</p>"; } ?> $result = mysql_query("SELECT * FROM `friend_system` WHERE `friend_system_accepter` = '$myusername' "); echo mysql_num_rows($result);This displays the total rows in the table. $result = mysql_query("SELECT COUNT(friend_system_accepter) FROM `friend_system` WHERE `friend_system_accepter` = '$myusername' "); echo mysql_num_rows($result);This displays '1', which is incorrect. I want to echo out the number of rows where 'friend_system_accepter' = $myusername Thanks It's late and I'm not thinking straight. I'm posting this question. Hopefully I get a reply in the morning. I have two tables. TABLE 1 - PRODUCTS TABLE 2 - LIKES I am a User who has posted these products. I want to find out ALL the Likes I have received for all my products. Here is my code. $find_products = $db->prepare("SELECT product_id FROM products WHERE user_id = :user_id"); $find_products->bindParam(':user_id', $my_user_id); $find_products->execute(); $result_products = $find_products->fetchAll(PDO::FETCH_ASSOC); if(count($result_products) > 0) { foreach($result_products as $row) { $product_id = $row['product_id']; $find_likes = $db->prepare("SELECT like_id FROM product_likes WHERE product_id = :product_id"); $find_likes->bindParam(':product_id', $product_id); $find_likes->execute(); $result_likes = $find_likes->fetchAll(PDO::FETCH_ASSOC); if(count($result_likes) > 0) { $get_likes = 0; foreach($result_likes as $row) { $get_likes++; } } } } The issue with the above code is that It only shows the Likes if I echo inside the foreach loop. And it'll show combined Likes from each of my products. But I want to actually combine ALL the Likes from ALL the products and be able show them as a single number, outside of the foreach loop. How do I do that? Edited August 17, 2019 by imgrooot |