PHP - Num_rows
ok really simple question. I'm going blind staring at this code so can someone please help. All I want to do is echo the number of rows in a table. Here is the code
<?php $qCheckFirstLog = "SELECT * FROM logindetails WHERE email = '".$_SESSION['email']."'"; $rCheckFirstLog = mysql_query($qCheckFirstLog); $num_rows = mysql_num_rows($rCheckFirstLog); ?> Now there are 15 records with the session emails name in the table. I know the session email is there as it echos an email if I ask it too so thats not missing but when I put <?php echo $num_rows; ?> I get nothing!!!!!! Can someone please tell me what I'm doing wrong Thank you Similar TutorialsHey guys I cant seem to find this error guess ive been looking at this screen to long to try and get this project done in time would appriciate if someone could take a fresh look at it and tell me where I went wrong. Basically im trying to get the number of entries returned by my query. I echoed out the query and manually entered it into phpmyadmin there is a 1 row result however whenever i kill $num_result it doesnt give me any output. Code: [Select] <?php public function getNewMessages() { global $db,$db_table_prefix; $sql = "SELECT * FROM messages WHERE `to_id` = '".$db->sql_escape($this->user_id)."' AND `read` = 0"; $res = mysql_query($sql) or die (mysql_error()); $num_rows = mysql_num_rows($res); die ($num_rows); // does not return a value return $num_rows; } ?> I have a table and the following code. Im not sure if im using rownum in the correct way as im not getting the result expected. $result = mysql_query("SELECT * FROM totem_ads"); $num_rows = mysql_num_rows($result); $random_row = rand(1, $num_rows); $ad_sql = mysql_query("SELECT * FROM totem_ads WHERE rownum='$random_row'"); while($row_advert = mysql_fetch_array($ad_sql)) { echo "<a href='" . $row_advert['link'] . "'><img src=$img_loc'" . $row_advert['image'] . "' alt='" . $alt_text['image'] . "'></a>"; } THe ad_sql displays as expected SELECT * FROM totem_ads WHERE rownum='1' however the while loop displays nothing which makes me wonder if im using rownum incorrectly. When i search for information on rownum I find all kinds of confusin statements from people posting in forums with problems and not the correct answer im looking for. I'm trying to write a plugin and when trying to see if a user ID is in an approved users table it does not want to cooperate. It doesn't error on any other $wpdb just this one. I'm not sure whats wrong, then again, I'm fairly new to OOP and WP Code: [Select] $wpdb->get_row("SELECT * FROM $wpdb->re_approvedusers WHERE user_id=$userid"); Full function code Code: [Select] function re_approved_user_verify($userid) { global $user_ID, $wpdb; //$userid = $this->re_secure($userid); $re_check = $wpdb->get_row("SELECT * FROM $wpdb->re_approvedusers WHERE user_id=$userid"); if($re_check == 0 || $re_check == NULL) { $success = 0; } else { $success = 1; } return $success; } Can anyone tell me what the problem is? $userid is obtained via the function if you're wondering. ERROR: Fatal error: Call to a member function get_row() on a non-object in /home/.../wp-content/plugins/re/functions.php on line 64 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! 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(); } }
|