PHP - Mysql_num_rows Not Working
Code: [Select]
while($row10000=mysql_fetch_array($result10000)){ $product_id3=$row10000['product_id']; $sql15000="SELECT * FROM $tbl_name WHERE product_id=".$product_id3; $result15000=mysql_query($sql15000); while($row15000=mysql_fetch_array($result15000)){ extract($row15000); if(mysql_num_rows($result15000)==0){ $content='<div class="center">Search found no results.</div>'; } else{ $content.=$product_name; } } } The above does not display the $content variable when there are no rows returned, just comes up with a blank page. Works fine when there are results returned. Similar TutorialsI have the following code... Code: [Select] $num_rows = mysql_num_rows($rsSinglesMatch); //$num_rows now contains the number of rows from the query echo $num_rows; if ($num_rows > 0 ) { echo $ssPlayerNumber; echo ("YEX"); } else { echo "ROWS"; } $myrow = mysql_fetch_row($rsSinglesMatch); //$rsSinglesMatches = mysql_fetch_assoc($rsSinglesMatch); print_r($myrow); // if (!($rsSinglesMatches==0) || !($rsSinglesMatches_BOF==1)) if ($myrow > 0) { echo $myrow; } else { echo "Rows not counted: "; } And I can't get the mysql_num_rows and mysql_fetch_rows to work. Currently it displays Rows not counted: and ROWS if I ask it to display the number of rows it shows it as being 0. Let me know if you see anything or if I need to post anything else. Im trying to figure out if a user has already downloaded something so im seeing if they have a uid (user id) in the database along with the nid (note id) that they are trying to download. I echoed out the sql and it returned this: Quote SELECT * FROM purchases WHERE uid =1 AND nid =7 i manually ran that sql in phpmyadmin and it returned a couple rows. However my var $rows = mysql_num_rows($res2) has no value when i echo is out. Appriciate any help here is my code: Code: [Select] <?php $sql2 = "SELECT * FROM purchases WHERE uid =".$user_id." AND nid =".$id; $res2 = mysql_query($sql2) or die (mysql_error()); $rows = mysql_num_rows($res2); ?> So im trying to figure out if a user is already in the table so im checking for the number of rows where the user id shows up on a record and if its greater then one redirect them. My problem is the if statment does not work and I know there is more then one row. Also die ($row2); wont output data but it will in a sentence (see code below for example) Code: [Select] $sql2 = "SELECT * FROM purchases WHERE nid = '$id' AND uid = '$user_id'"; $res2 =mysql_query($sql2) or die (mysql_error()); $row2 = mysql_num_rows($res2); die($row2);//no output die("There are a total of ".$row2." rows");//outputs There are a total of 20 rows if ($row2 > 0){//if statment doesnt work. //already bought the note header("Location: view.php?error=2"); } Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource. if ($_POST['subscribe']) { $email_subscribe = $_POST['email']; if (mysql_num_rows(mysql_query("SELECT * FROM newsletter WHERE email=$email_subscribe"))) { mysql_query("UPDATE newsletter SET active=1 WHERE email=$email_subscribe") or die ('Error updating the database'); echo "Thank you for subscribing!"; echo "<meta http-equiv=\"refresh\" content=\"0;url=javascript:history.back();\">"; } else { mysql_query("INSERT INTO newsletter (`email`,`active`) VALUES ('$email_subscribe', '1')"); echo "Thank you for subscribing!"; echo "<meta http-equiv=\"refresh\" content=\"0;url=javascript:history.back();\">"; } } elseif ($_POST['unsubscribe'] != 0) { $email_unsubscribe = $_POST['email']; if (mysql_num_rows(mysql_query("SELECT * FROM newsletter WHERE email=$email_unsubscribe"))) { mysql_query("UPDATE newsletter SET active=0 WHERE email=$email_unsubscribe") or die ('Error updating the database'); echo "You have been unsubscribed."; echo "<meta http-equiv=\"refresh\" content=\"5;url=javascript:history.back();\">"; } else { echo "Email does not exist."; echo "<meta http-equiv=\"refresh\" content=\"5;url=javascript:history.back();\">"; } $sql = "SELECT current FROM LIBusersX WHERE UserKey = '$Key' AND K_Type = $type AND current > 0 "; // echo $sql . "<br>"; $query = mysql_query ($sql) or die ("E112-100A"); $total = mysql_num_rows($query) or die ("E112-101A");Hi I have a website with a few users. I set up a system so that some users (as a trial) can only log on a fixed number of times. I have a field called current that counts down to zero. However when it reaches zero I get my error E112-101A Why do I get an error? Surely I should just get $total = 0 I'm trying to loop out 10 rows of comment, but this code only displays comment one time, how do I display all rows? $query = "SELECT * FROM ".$prefix."comments WHERE userto = '".$user."' ORDER BY comment DESC LIMIT 0, 10"; $result = mysql_query($query); $num = mysql_num_rows($result); //Loop out code $i=0; while ($i < $num) { $comments=@mysql_result($result,$i,"comment"); $i++; } Hi I am trying to create a login script using the code below Code: [Select] <?php session_start(); include_once('includes/connect.php'); $user=$_POST['username']; $password=$_POST['password']; //check if username and password exists $checkuser=mysql_query("SELECT * from `tbl_users` where username = '$user' and password=md5('$password')"); if(mysql_num_rows($checkuser) > 0) //login name was found //if they do return to home page with relevant include { $user_row = mysql_fetch_array($checkuser); $_SESSION['auth']="yes"; $_SESSION['logname'] = $user_row['forename']; $_SESSION['usertype'] = $user_row['usertype']; header("Location: index.php"); exit; } //if not display login form with message that says logon details are incorrects else { echo('Do something else'); } ?> However I get a message saying that mysql_num_rows is not a valid resource. Any ideas where I am going wrong? Hello, I am having a problem.. I get this message " Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in... " whenever i am trying to work my log in script. The script is: Quote <?php $host="localhost"; $username="root"; $password="pass"; $db_name="webDB"; $table_name="webmembers"; mysql_connect($host, $username, $password) or die ('Unable to connect'); mysql_select_db($db_name) or die ('Error'); $uname= $_POST['uname']; $pword= $_POST['pword']; $urname= stripslashes($uname); $pword= stripslashes($pword); $uname= mysql_real_escape_string($uname); $pword= mysql_real_escape_string($pword); $sql= "SELECT * FROM '$table_name' WHERE username: '$uname' and password: '$pword'"; $result=mysql_query($sql); $count= mysql_num_rows($result); <----------- problem if ($count == 1) { session_register($uname); session_register($pword); header("location: loginsuccess.php"); } else { echo $result,$count; echo "Wrong Username and Password"; } mysql_close(); ?> and Quote "loginsuccess.php" <?php session_start(); if(!sesssion_is_registered($uname)) { header("location: mainpage.php"); } ?> <html> <head> </head> <body> Log in Successful </body> </html> why do i get this warning? what am i doing wrong? Is there any simpler way to create a log in page but being secure as well? Thank in advance. Hi All, I think im having one of those days when you want to rip your hair out... My problem is... I want to produce the total number of results in my table that match a particular query... that query in English is... calculate the total number of records where field category = C Category... When im in phpMyadmin and perform a search it finds all the records that have the value of C Category in the category field. If I then choose 'show php code' i get: $sql = "SELECT * FROM `staff` WHERE `category` = \'C Category\' LIMIT 0, 30 "; In my php document, i use... Code: [Select] <?php include ('config.php'); $sql="SELECT * FROM `staff` WHERE `category` = \'C Category\' LIMIT 0, 30 "; $result=mysql_query($sql); $num_rows = mysql_num_rows($result); echo $num_rows; ?> But I get a blank page... what am I doing wrong? Cheers Hi all, I have been running into a couple of errors messages that I have not been able to debug (below): 1. Undefined var Use1 2. mysql_num_rows(): supplied argument is not a valid MySQL result resource My code is as follows: Code: [Select] <?php // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php $results = ""; if(isset($_GET['PlantID'])) { include_once "scripts/connect_to_mysql.php"; $PlantID = $_GET['PlantID']; $sql = mysql_query("SELECT * FROM plants WHERE PlantID='$PlantID'"); $plantCount = mysql_num_rows($sql); if($plantCount > 0) { while ($row = mysql_fetch_assoc($sql)) { $PlantID = $row["PlantID"]; $BotanicalName = $row["BotanicalName"]; $CommonName = $row["CommonName"]; $HardinessZones = $row["HardinessZones"]; $Type = $row["Type"]; $Height = $row["Height"]; $Spread = $row["Spread"]; $Flower = $row["Flower"]; $BloomTime = $row["BloomTime"]; $Foliage = $row["Foliage"]; $FallColor = $row["FallColor"]; $Fruit = $row["Fruit"]; $Light = $row["Light"]; $Soil = $row["Soil"]; $Growth = $row["Growth"]; $Trimming = $row["Trimming"]; $Fertilization = $row["Fertilization"]; $OtherMaintenance = $row["OtherMaintenance"]; $Use1 = $row["Use1"]; $Use2 = $row["Use2"]; $Use3 = $row["Use3"]; $Use4 = $row["Use4"]; $Use5 = $row["Use5"]; $UseDesc1 = $row["UseDesc1"]; $UseDesc2 = $row["UseDesc2"]; $UseDesc3 = $row["UseDesc3"]; $UseDesc4 = $row["UseDesc4"]; $UseDesc5 = $row["UseDesc5"]; $Image1 = $row["Image1"]; $Image2 = $row["Image2"]; $Image3 = $row["Image3"]; $Image4 = $row["Image4"]; $Image5 = $row["Image5"]; $ImageDesc1 = $row["ImageDesc1"]; $ImageDesc2 = $row["ImageDesc2"]; $ImageDesc3 = $row["ImageDesc3"]; $ImageDesc4 = $row["ImageDesc4"]; $ImageDesc5 = $row["ImageDesc5"]; } } else { $results = "<h1>No Data to Run this Page!</h1>"; } } ?> <?php session_start(); // See if they are a logged in member by checking Session data $toplinks = ""; $booklinks = ""; if (isset($_SESSION['memberID'])) { $memberID = $_SESSION['memberID']; $username = $_SESSION['username']; $toplinks = '<a href="memberaccount.php?id=' . $memberID . '">Welcome, ' . $username . '!</a> | <a href="memberaccount.php?id=' . $memberID . '">My Account</a> | <a href="book.php">Your Plant Book</a> | <a href="logout.php">Log Out</a>'; $booklinks = '<form name="add_to_book" method="post" action="plantdetails.php"> <input name="memberID" type="hidden" value="' . $memberID . '" /> <input name="PlantID" type="hidden" value="' . $PlantID . '" /> <input name="BotanicalName" type="hidden" value="' . $BotanicalName . '" /> <input name="CommonName" type="hidden" value="' . $CommonName . '" /> <input name="Use" type="hidden" value="' . $Use1 . '" /> <input name="add_to_book_btn" type="submit" value="Add ' . $BotanicalName . ' (' . $Use1 . ') to Your Book!" /> </form>'; } else { $toplinks = '<a href="join.php">Join Now</a> | <a href="login.php">Login</a>'; $booklinks = '<h1>You must be logged in to Add Plant Pages and Create Plant Books! <img src="images/AddtoBook.gif" border="0" /></h1>'; } ?> <?php $errorMsg = ""; if(isset($_POST['memberID'])) { include_once "scripts/connect_to_mysql.php"; $memberIDtemp = $_POST['memberID']; $PlantID = $_POST['PlantID']; $BotanicalName = $_POST['BotanicalName']; $CommonName = $_POST['CommonName']; $Use = $_POST['Use']; $sqlCheck = mysql_query("SELECT * FROM book_temp WHERE PlantID='$PlantID' AND Use='$Use'"); $plantCountCheck = mysql_num_rows($sqlCheck); if($plantCountCheck > 0) { $errorMsg = "<h1>This Plant and Landscape Use is already in Your Book! Please select another Plant or Landscape Use.</h1>"; } else { mysql_query("INSERT INTO book_temp (memberID, PlantID, BotanicalName, CommonName, Use) VALUES ('$memberIDtemp', '$PlantID', '$BotanicalName', '$CommonName', '$Use')"); header("location: book.php"); exit(); } } ?> The particular errors are on the following lines: Code: [Select] <input name="Use" type="hidden" value="' . $Use1 . '" /> <input name="add_to_book_btn" type="submit" value="Add ' . $BotanicalName . ' (' . $Use1 . ') to Your Book!" /> $plantCountCheck = mysql_num_rows($sqlCheck); Any help would be superb, Thanks in advance! Bill C. I am having trouble calling mysql_num_rows in my script. I get the error message "PHP Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\scripts\update.php on line 41". I have looked around online but can only find the my sql is not right. I am able to copy and past the sql into mysql workbench and it runs fine. Any help would be much appreciated. Code: [Select] $game_id = $row['id']; $game_name = $row['game_name']; print("updating game {$game_id}: {$game_name}" . PHP_EOL); $sql = "SELECT users.id, users.username, users.number_attended_games FROM users join game_sessions_users on game_sessions_users.user_id = users.id where game_sessions_users.game_session_id = " . $game_id; print($sql . PHP_EOL); $players = mysql_query($sql, $connection) or die(mysql_error($connection));; if(mysql_num_rows($players >= 1)){ while($player = mysql_fetch_array($players, MYSQL_ASSOC)){ print('updating player: ' . $player['username']); $number_attended_games = $player['number_attended_games']; $user_id = $player['id']; $sql = "UPDATE users SET number_attended_games = " . ($number_attended_games + 1) . ", user_level = " . ($number_attended_games / 10) . " WHERE id = " . $user_id; $update_result = mysql_query($sql, $connection); } } $sql = "UPDATE game_sessions SET completed = 1 WHERE id = " . $game_id; $update_result = mysql_query($sql, $connection); ok guys, im going out the door so forgive me for not showing any code..but I have a while loop whithin a while loop to display rows for two different id's. I have a mysql_num_rows vaiable set that displays 0 because there are 0 rows with the userid of 1...then it displays 2 because there are 2 rows with the userid of 3..now what i want to do is find a way to omit the return of 0 so that i can use the return of 2 alone. sorry if im not clear enough. Hi people, My script is for my game, it is to see whether a player has already started a challenge (it is recorded to a table in my database). If the challenge has already been started, then the button to accept the challenge is disabled. But if the challenge hasn't been accepted yet, then the button is clickable. The problem though is the mysql_num_rows function isn't returning 0 like it should (meaning the challenge has not been started) and instead the button is being disabled. Code: [Select] <?php session_start(); include('functions.php'); connect(); ?> <!DOCTYPE html> <html> <head> <title>University Crusade</title> <link rel="stylesheet" href="css/style.css" type="text/css" media="screen"> <meta name="viewport" content="width=device-width, minimum-scale=1,maximum-scale=1, user-scalable=no"> </head> <body> <?php if (isset($_SESSION['userid'])) { include('safe.php'); ?> <ul id="tab-nav"> <li><a href="stats.php" id="tab-character">Character</a></li> <li><a href="games.php" class="active" id="tab-games">Games</a></li> <li><a href="account.php" id="tab-account">Account</a></li> </ul> <div id="wrapper"> <h2 id="name">Select a Challenge</h2> <ul id="table-view"> <li> <fieldset> <legend>"Easter Egg" Challenge</legend> <div id="rewards"> <p class="instructions"> Find each of the 4 "Easter Egg" barcodes around the university campus. </p> <p> Rewards: Every egg is worth <span class="stat">50</span> XP and <span class="stat">35</span> gold coins. </p> </div> <form action="games.php" method="POST"> <?php $check = mysql_query(" SELECT start1 FROM chal1 WHERE userid='".$_SESSION['userid']."' ") or die ("Could not select database from chal1"); if (mysql_num_rows($check)>0) { echo " <button class=\"buttons\" type=\"submit\" name=\"start1\" disabled=\"disabled\"> Accept challenge </button> "; } else { echo " <button class=\"buttons\" type=\"submit\" name=\"start1\"> Accept challenge </button> "; } ?> </form> </fieldset> </li> <li class="even"> <fieldset> <legend>Increase Strength</legend> <div id="rewards"> <p class="instructions"> You'll find the barcode in the Sports Centre. </p> <p> Rewards: Every time you visit the Sports Centre you'll receive <span class="stat">2</span> Str. points. </p> </div> <form action="games.php" method="POST"> <button class="buttons" type="submit" name="start2"> Accept challenge </button> </form> </fieldset> </li> <li> <fieldset> <legend>Increase Intelligence</legend> <div id="rewards"> <p class="instructions"> You'll find the barcode in the university Library. </p> <p> Rewards: Every time you visit the Library you'll receive <span class="stat">2</span> Int. points. </p> </div> <form action="games.php" method="POST"> <button class="buttons" type="submit" name="start2"> Accept challenge </button> </form> </fieldset> </li> </ul> </div> <div id="footer"> <a class="buttons" href="logout.php">log me out</a> </div> <?php } else { die (" <div id=\"wrapper\"> <p>Opps! You don't seem to be logged in...</p> <a class=\"buttons\" href=\"index.php\">login now</a><br /> <p>Don't have an account? No worries, just <a class=\"buttons\" href=\"register.php\">register for one.</a></p> </div> "); } ?> Anyone know what could be wrong? Thanks Here is my CODE which is showing some error: Code: [Select] <?php include('dbcon.php'); session_start(); $usname=$_POST['usname']; $password=$_POST['password']; $usname = stripslashes($usname); $password = stripslashes($password); $usname = mysql_real_escape_string($usname); $password = mysql_real_escape_string($password); $check="y"; $sql = "select * from usname where usname='$usname' and password='$password'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1) { $sqlq = "select * from usname where usname='$usname' and password='$password' and check='$check'"; $resultq=mysql_query($sqlq); $countq=mysql_num_rows($resultq); if($countq==1) {$_SESSION['usname']=$usname; header('location:fire.php');} else {$_SESSION['status']="Admin Didnt grant you the permission to access the things"; header('location:index.php');} } else {$_SESSION['status']="Wrong username and password"; header('location:index.php');} die(" "); ?> And here are the ERRORS when the username and passwords are correct but the check is not equal to 'y'.... Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\money\verify.php on line 18 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\money\verify.php:18) in C:\xampp\htdocs\money\verify.php on line 24 Please tell me where is the mistake........... I am getting the following warning with the code below.Can someone please point me why the warning ? Warning Code: [Select] Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/r.php on line 500 Code Code: [Select] $sql = "SELECT * FROM funeral WHERE name='".$name."' AND phone='".$phone."'"; $result = mysql_query($sql); if((mysql_num_rows($result)) == 0) { $insertsql = "INSERT INTO funeral (name, street, city, shrtstate, state, pincode, phone) VALUES ('".$name."', '".$street."', '".$city."', '".$shrtstate."', '".$state."', '".$pincode."', '".$phone."')"; $insert = mysql_query($insertsql); } Get an error saying: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\admin\delete.php on line 16 Sorry, but we can not find an entry to match your query Surely this means the code has worked because of the last line, but i can't get rid of the error! $q = "DELETE FROM `stocklist` WHERE `Stock Number`='".$rec."'"; $res = mysql_query($q, $link) or die (mysql_error()); $anymatches = mysql_num_rows($res); if ($anymatches == 0) die ('Sorry, but we can not find an entry to match your query<br><br>'); echo '<h1>Entry has been deleted!</h1><br><br>'; mysql_close($link); Hi guys, one of the last questions from me for a while i hope lol i am trying to check if a phone number allready exists in a database using mysql_num_rows The code is this: Code: [Select] $SQL = "SELECT * FROM postcode WHERE phone = $phone"; $result = mysql_query($SQL); $num_rows = mysql_num_rows($result); if ($num_rows > 0) { $errorMessage = "It seems that this phone number is already been entered into our database"; } else {bla bla bla But i keep getting a warning message: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in I am at my wits end with this and done a lot of searching to try and find out why this is, but obviously i am dim and need it to be explained in english. Cheers! Hi, This is my first post on phpfreaks[dot]com. I want some help on a php code i was writing this night watching a tutorial. Here's the code: if($cat && $name && $desc){ $sql7 = "SELECT * FROM `forum_cats` WHERE `id`='".$cat."'"; $res7 = mysql_query($sql7) or die(mysql_error()); if(mysql_num_rows($res7) == 0){ echo "The forum category you supplied does not exist!\n"; }else{ blablabla... } in cases like this, the mysql_num_rows() function was always working nicely. here for example: $sql3 = "SELECT admin FROM `users` WHERE `id`='".$_SESSION['uid']."'"; $res3 = mysql_query($sql3) or die(mysql_error()); if(mysql_num_rows($res3)==0){ echo "Please login to your account!\n"; }else{ blablabla... } it's working here just fine. i canged my first code into this: if($cat && $name && $desc){ $sql7 = "SELECT * FROM `forum_cats` WHERE `id`='".$cat."'"; $res7 = mysql_query($sql7) or die(mysql_error()); $aaa = mysql_fetch_assoc($res7); if(sizeof($aaa) == 0){ echo "The forum category you supplied does not exist!\n"; }else{ blablabla... } now it works like it is supposed to, but i really want to know what i am doing wrong in the first part. EDIT: I can post a longer code so that maybe there is an error before the script gets to this part. I hope you will understand what i'm talking about Sorry for my English. thanks in advance, Resul. |