PHP - Help With Mysql_num_rows Error
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"; Similar TutorialsGet 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); 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........... 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! Hello there, I'm having a issue where when I run the following statement: $Session = $Module['MySQL']->RowCount("SELECT * FROM xhost_accounts WHERE (account_username = '{$Module['MySQL']->Escape($AccountUsername)}' OR account_email = '{$Module['MySQL']->Escape($AccountUsername)}') AND account_password = MD5('{$AccountPassword}')") == 1 ? true:false; I am proceeded by the following error: Code: [Select] Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\Backend\MySQL-Module.php on line 49 Yet when I do the following statement: echo $Module['MySQL']->RowCount("SELECT * FROM xhost_accounts WHERE (account_username = '{$Module['MySQL']->Escape($AccountUsername)}' OR account_email = '{$Module['MySQL']->Escape($AccountUsername)}') AND account_password = MD5('{$AccountPassword}')") It gives me the output of 2 (That's the correct table row count)? Does anybody have any insight into why this could be happening thanks. I have tried to create a basic login form. When I enter the username and password I get this error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in login.php on line 10 Code: [Select] <?php require("connection.php"); if($_SERVER["REQUEST_METHOD"] == "POST") { $username=mysql_real_escape_string($_POST['username']); $password=mysql_real_escape_string($_POST['password']); $password=md5($password); $sql="SELECT id FROM user WHERE username='$username' and password='$password'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count==1) { header("location: dashboard.php"); } else { $error="Invalid username or password"; } } ?> <form action="login.php" method="POST"> <table class="ltext"> <tr> <td> Username </td> <td> <input type="text" name="usename" class="input" /> </td> </tr> <tr> <td> Password </td> <td> <input type="password" name="password" class="input" /> </td> </tr> </tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> <input type="submit" value="Login" name="submit" class="input" /> <input type="reset" value="Clear" name="clear" class="input" /> </td> </tr> </table> </form> I can't figure out why I am getting that error message does anyone have any ideas? Trying to get this to work but I know I'm missing something here. It's a form that is meant to update a record in DB. I am getting errore reference to first if (mysql_num_rows($result) == 1) in code and error message: Error has occurred. $connection = mysql_connect("xxxxxxxx","xxxxxx","xxxxxx"); if (!$connection) { die("Error connecting to database " . mysql_error()); } if (isset($_POST['submitted'])) $name = $_POST['name']; $email = $_POST['email']; $PHOTOGRAPHERID = $_POST['PHOTOGRAPHERID']; $query = "SELECT * FROM Photographers WHERE PHOTOGRAPHERID = $PHOTOGRAPHERID"; $result = mysql_query($query); if (mysql_num_rows($result) == 1){ // Make the query. $query = "UPDATE Photographers SET name='$name',email='$email' WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID"; $result = @mysql_query ($query); // Run the query. if (mysql_affected_rows() == 1) { // If it ran OK. header("Location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/"."search.php"); } else { // If it did not run OK. echo '<h1 id="mainhead">System Error</h1> <p class="error">The name could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message. echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message. exit(); } } else { echo '<h1 id="mainhead">Error!</h1> <p class="error">Error has occurred.</p>'; } $query = "SELECT name, email FROM Photographer WHERE PHOTOGRAPHERID=$PHOTOGRAPHERID"; $result = @mysql_query ($query); // Run the query. // Create the form. echo '<h2>Edit a User</h2> <form action="update.php" method="post"> <p>Name: <input type="text" name="name" size="60" maxlength="60" value="' . $row[0] . '" /></p> <p>Email: <input type="text" name="email" size="60" maxlength="60" value="' . $row[1] . '" /></p> <p><input type="submit" name="submit" value="Submit" /></p> <input type="hidden" name="submitted" value="TRUE" /> <input type="hidden" name="PHOTOGRAPHERID" value="' . $id . '" /> </form>'; Hi I don't want errors on my page been reported on the web browser for all to see so I use this. $total2 = mysql_num_rows($qCheckUser) or die ("Error1"); however if the query returns zero rows I get Error1 on the web page. This is not an error and I don't want to taje out the or die() Any sugestions. 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++; } 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 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 When fetching results from a query I have always used the following method (the one i was taught) 1. get the query 2. if i know there might be more than one row returned, count the rows 3. use a for loop to get all the mysql_fetch_row results. Is there a more efficient way? eg, i am now writing a section that i know will either return one row or two, but never anymore, can i run a foreach on a mysql fetching function to get all the rows instead of counting first? (ie cutting out the middle man) 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. 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!! 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); ?> |