malikah Posted August 10, 2009 Share Posted August 10, 2009 Hi, I'm having trouble returning a full row count from my database. The following code returns "1" column instead of "24" columns. $conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) $query = "SELECT * FROM table"; if($stmt = $this->conn->prepare($query)) { $stmt->execute(); if($stmt->fetch()) { if ($stmt->field_count) { $result = $stmt->store_result(); } $stmt->close(); return $result; } } Quote Link to comment https://forums.phpfreaks.com/topic/169628-solved-mysql-row-count-prepared-statement/ Share on other sites More sharing options...
perrij3 Posted August 10, 2009 Share Posted August 10, 2009 You may want to use mysql_num_rows() to count your rows with. http://us.php.net/manual/en/function.mysql-num-rows.php Quote Link to comment https://forums.phpfreaks.com/topic/169628-solved-mysql-row-count-prepared-statement/#findComment-894909 Share on other sites More sharing options...
malikah Posted August 10, 2009 Author Share Posted August 10, 2009 You may want to use mysql_num_rows() to count your rows with. http://us.php.net/manual/en/function.mysql-num-rows.php I tried that, but unfortunately get the error: "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource". Quote Link to comment https://forums.phpfreaks.com/topic/169628-solved-mysql-row-count-prepared-statement/#findComment-894913 Share on other sites More sharing options...
alexdemers Posted August 10, 2009 Share Posted August 10, 2009 You need to store results so you can count them PHP: mysqli::store_result. Edit: actually look at this if you are using prepared statements: PHP: mysqli_stmt::num_rows - Manual Read the Typer85 at gmail dot com's comment at the bottom of the page. Quote Link to comment https://forums.phpfreaks.com/topic/169628-solved-mysql-row-count-prepared-statement/#findComment-894935 Share on other sites More sharing options...
perrij3 Posted August 10, 2009 Share Posted August 10, 2009 Did you add the results from the sql query in between the () you are trying to count? $sql = "Select * FROM usertable'"; $result = mysql_query($sql) or die(mysql_error()); $NumberOfRows = mysql_num_rows($result); Quote Link to comment https://forums.phpfreaks.com/topic/169628-solved-mysql-row-count-prepared-statement/#findComment-894942 Share on other sites More sharing options...
alexdemers Posted August 10, 2009 Share Posted August 10, 2009 To add to my previous response, you need to do num_rows method and not field_count method. Quote Link to comment https://forums.phpfreaks.com/topic/169628-solved-mysql-row-count-prepared-statement/#findComment-894944 Share on other sites More sharing options...
malikah Posted August 10, 2009 Author Share Posted August 10, 2009 To add to my previous response, you need to do num_rows method and not field_count method. I'm actually trying to count columns, not rows - but thanks Quote Link to comment https://forums.phpfreaks.com/topic/169628-solved-mysql-row-count-prepared-statement/#findComment-894955 Share on other sites More sharing options...
malikah Posted August 10, 2009 Author Share Posted August 10, 2009 So far this works for ROWS but I need something for COLUMNS: $query = "SELECT * FROM table"; if($stmt = $this->conn->prepare($query)) { $stmt->execute(); $stmt->store_result(); echo $stmt->num_rows(); $stmt->close(); } Quote Link to comment https://forums.phpfreaks.com/topic/169628-solved-mysql-row-count-prepared-statement/#findComment-894960 Share on other sites More sharing options...
perrij3 Posted August 10, 2009 Share Posted August 10, 2009 Try mysql_num_fields instead of mysql_num_rows. http://us2.php.net/mysql-num-fields or you may want to try fbsql_num_fields http://www.php.net/manual/en/function.fbsql-num-fields.php Quote Link to comment https://forums.phpfreaks.com/topic/169628-solved-mysql-row-count-prepared-statement/#findComment-894962 Share on other sites More sharing options...
malikah Posted August 10, 2009 Author Share Posted August 10, 2009 OK, I used "DESCRIBE table" to get it working: $query = "DESCRIBE table"; if($stmt = $this->conn->prepare($query)) { $stmt->execute(); $stmt->store_result(); echo $stmt->num_rows(); $stmt->close(); } NOW.. All I need is to return the names of each field... Quote Link to comment https://forums.phpfreaks.com/topic/169628-solved-mysql-row-count-prepared-statement/#findComment-894972 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.