PHP - I've Got A Problem With Mysql_num_rows()
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. Similar Tutorials MYSQL Client server version 5.0.45 Hi All! Got a little irritating problem going on when trying to allow a user to log on with their credentials. I've created the relevant tables, defined connection correctly etc but every time I enter in the correct user account details , it always displays the message I created for when the user account details are wrong. Here is the code: <?php ... ... $find_user_sql = "SELECT `ad_aid`, `ad_aus` FROM `ad_users` WHERE `ad_aus` = '" . $find_user_usr . "' AND `ad_apa` = '" . $find_user_pas . "'"; $find_user_res = mysql_query($find_user_sql); if(mysql_num_rows($find_user_res) > 1|| mysql_num_rows($find_user_res) < 1){ ?> <div id="loginbox"> <div id="loginbox-logo"> </div> <div id="loginbox-content"> <div id="content-error"> <p>The details you entered were incorrect.</p> </div> <form action="login.php" method="post"> <p>Admin Username:<br /> <input type="text" name="ad_user" autocomplete="off" /> </p> <p>Admin Password:<br /> <input type="password" name="ad_pass" autocomplete="off" /> </p> <p> <br /> <input type="submit" name="ad_submit" value="Log me in »" id="login_button" /> </p> </form> </div> <div id="loginbox-bottom"> </div> </div> <?php include_once('includes/footer.php'); $insert_fail_attempt_sql = "INSERT INTO `ad_fail_log` VALUES (NULL, NULL, '" . visitorIP() . "')"; mysql_query($insert_fail_attempt_sql); } else{ include_once('includes/header.php'); $find_user = mysql_fetch_assoc($find_user_res); session_start(); $_SESSION['ad_user'] = $find_user['ad_aus']; $_SESSION['ad_aid'] = $find_user['ad_aid']; ?> <meta http-equiv="refresh" content="3;url=../ad/index.php"> <div id="loginbox"> <div id="loginbox-logo"> </div> <div id="loginbox-content"> <p>Logging In...<br /><br /><br /><br /> <center><img src="images/login-loader.gif" /></center> </p> </div> <div id="loginbox-bottom"> </div> </div> <?php include_once('includes/footer.php'); } } }else{ include_once('includes/header.php'); ?> What the aim of the script is, is to allow users to log in then be taken to a page called index.php . I also have a include php script called check.php (below) which checks users accounts: <?php session_start(); if(!empty($_SESSION['adm_user']) && !empty($_SESSION['adm_aid'])){ $check_user_sql = "SELECT `adm_ana`, `adm_per` FROM `pb_admin_users` WHERE `adm_aus` = '" . $_SESSION['adm_user'] . "' AND `adm_aid` = '" . $_SESSION['adm_aid'] . "'"; $check_user_res = mysql_query($check_user_sql); if(mysql_num_rows($check_user_res) > 1 || mysql_num_rows($check_user_res) < 1)){ $check_user = mysql_fetch_assoc($check_user_res); $_SESSION['adm_name'] = $check_user['adm_ana']; $_SESSION['adm_perm'] = $check_user['adm_per']; }else{ header('Location: https://*website*/' . DIR_ADM . '/login.php'); die($check_user_sql); } }else{ header('Location: https://*website*/' . DIR_ADM . '/login.php'); die($check_user_sql); } ?> I know it's got something to do with the if(mysql_num_rows($find_user_res) > 1|| mysql_num_rows($find_user_res) < 1){ , but I don't know why its rejecting correct username and password . I've tried fiddling around with the mysql_num_rows value to 1 or 0 or ==1 etc but it will only do one of the following: - State that my username and password are incorrect(when they're not) OR - Seem as if it is logging in by displaying "logging in" then for it to only display the login.php page again I would be very grateful if anyone could give me some pointers!! 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 I am having real problems getting mysql_num_rows function working with PHP 5 This is the split up script: index.php <?php ini_set('display_errors', 1); require_once 'config.php'; require_once 'func/func.db.php'; $connect = databaseConnect($host, $user, $password, $database); if($connect == true) { // printf("<table>"); // get it querying results for showing users entries as of now: $result = query("SELECT * FROM entries"); if($result == true) { printf("<table> <tr> <td></td> </tr>\n"); while($row = mysql_fetch_array($result)) { printf(" <tr> <td>Result row</td> </tr>\n"); } printf("</table>\n\n"); } // show form for users logged in so they can post entries, ie just one user account for now: require_once 'auth.php'; } ?> func/func.db.php <?php function databaseConnect($host, $user, $password, $database) { $connect = mysql_connect($host, $user, $password); if($connect) { $select_db = mysql_select_db($database, $connect); if($select_db) { return true; } else { return false; } } else { return false; } } function query($sql) { $sql = stripslashes($sql); $query = mysql_real_escape_string($sql); $result = mysql_query($query); if($result){ return $result; } } Then the auth.php, looks like: <?php if(isset($_POST)) { $message = 'Only registered users can post'; if(array_key_exists('submit',$_POST)) { if($_POST['username'] != '' && $_POST['password'] != '') { $username = $_POST['username']; $password = sha1($_POST['password']); // try to log user in: $result = query("SELECT * FROM users WHERE username = '$username' AND '$password'"); echo mysql_num_rows($result); } else { $message = 'You did not enter all required fields, please retry'; } } } else { $message = 'Only registered users can post'; } ?> <p><?=$message;?></p> <table> <form id="login" name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <tr> <td><label for="username">User: </label></td> <td><input type="text" id="username" name="username" value="" /></td> </tr> <tr> <td><label for="password">Password: </label></td> <td><input type="password" id="password" name="password" value="" /></td> </tr> <tr> <td><input type="submit" id="submit" name="submit" value="Login" /></td> </tr> </form> <table> You dont need to know whats in config.php since thats just full of my database connection variables, like the hostname, user, password and database, but its saying mysql_num_rows is an invalid function, since maybe its because its lost connection to the database, but how would I get around this? I thought about maybe making it connect to the database again, but is there any point in doing this? This is the actual error I get: Quote Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/jeremysmith.me.uk/html/blog-dev/auth.php on line 17 Any help is much appreciated, Jez. I have a mysql database table that lists baseball and softball umpires for the season. The table has columns for: date, sport, umpire1, umpire2, umpire3, umpire 4, umpire5, visitor team, and home team. I'm trying to write a bit of code that will tell me how many times I have schedule (manually) an umpire to see a particular school. Here's what I've come up with. Code: [Select] <?php require_once "/config.php"; $dbc = mysql_pconnect($host, $username, $password); mysql_select_db($db,$dbc); $start_date = '2011-04-01'; $end_date = '2011-06-01'; $umpires = Array('umpire 1 name', 'umpire 1 name', 'umpire 2 name', 'umpire 3 name', 'umpire 4 name', 'umpire 5 name', 'umpire 6 name'); $schools = Array('Bagley', 'Bemidji', 'Blackduck', 'Fosston', 'International Falls', 'Kelliher', 'Laporte', 'Lake of the Woods', 'Remer', 'Walker'); ?> <?php foreach($umpires as $umps) { //selects the first umpire in the array echo "<table width='20%' border ='1'>"; echo "<tr><td>$umps</td><td>Baseball</td><td>Softball</td></tr>"; foreach($schools as $town) { //selects the first town in the array $sql_baseball = "SELECT * FROM $table WHERE (ump1 = $umps OR ump2 = $umps OR ump3 = $umps OR ump4 = $umps OR ump5 = $umps) AND (visitor = $town OR home = $town) AND sport = 'Baseball' AND date >= $start_date AND DATE <= $end_date"; $result_baseball = mysql_query($sql_baseball); $count_baseball=mysql_num_rows($result_baseball); $sql_softball = "SELECT * FROM $table WHERE (ump1 = $umps OR ump2 = $umps OR ump3 = $umps OR ump4 = $umps OR ump5 = $umps) AND (visitor = $town OR home = $town) AND sport = 'Softball' AND date >= $start_date AND DATE <= $end_date"; $result_softball = mysql_query($sql_softball); $count_softball=mysql_num_rows($result_softball); echo "<td>$town</td><td>$count_baseball</td><td>$count_softball</td></tr>"; } //loops the towns echo "</table>"; } //ends each umpire ?> As you can see, I've put all the umpire names in an array...and each school in a separate array. The html tables are being displayed (with the correct names and schools listed...but I'm getting this error: Quote Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /by_school.php on line 33 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /by_school.php on line 37 Line 33 is this - - - $count_baseball=mysql_num_rows($result_baseball); Line 37 is this - - - $count_softball=mysql_num_rows($result_softball); I'm guessing that the problem is that I have multiple selects (one for each town) while using the same variable names for each select...but I can't figure out how to concacate the variable names properly...or even if that's the problem. Any ideas? Thanks! 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? $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 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. 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();\">"; } 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++; } 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); } I'm trying to check the number of results returned in a query. Currently there is one result being returned but i'm getting this error when I try to run line 38: Code: [Select] $num_rows = mysql_num_rows($ratings); Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/haas12/public_html/login/rateVideo.php on line 38 Rows Code: [Select] $ratingsQuery = "SELECT * FROM haas12_test.ratings "; $ratings = mysqli_query($conn, $ratingsQuery) or die ("Couldn't execute query."); $num_rows = mysql_num_rows($ratings); echo "$num_rows Rows\n"; I know im being blind today but can someone point out why: Code: [Select] $query = "SELECT peopleID FROM numpeople WHERE ID = '3';"; $result = mysql_query($query) or die("failed."); $row = mysql_fetch_row($result); $numpeople = mysql_num_rows($result); echo "<br /> row $row[1]"; echo "<br />numpeople $numpeople"; $numpeople returns 2, but $row[1] is undefined? $row[0] is fine. (The value of $row[0] is 7 and $row[1] should be . This is the second error that I really cant see.... 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 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); 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. 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! 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); ?> |