fizzzgigg Posted June 22, 2010 Share Posted June 22, 2010 I have the following code for a site I am working on. The goal here, is if there is nothing in the database it will a message, "No information submitted." But what I get is only the first letter of what ever print I put into it. $conid = $_GET['conid']; $result = mysql_query("SELECT * FROM `content` WHERE `cid`='$conid' LIMIT 1"); $row = mysql_fetch_array( $result ); $row = (strlen($row)==0)?'No files submitted.':$row; Please help. Quote Link to comment Share on other sites More sharing options...
Cless Posted June 22, 2010 Share Posted June 22, 2010 $conid = $_GET['conid']; $result = mysql_query("SELECT * FROM `content` WHERE `cid`='$conid' LIMIT 1"); $row = mysql_fetch_array( $result ); if(empty($row['content'])) { echo "No files submitted."; } Edit: I see you're trying to change the value of $row. I'm pretty sure it only takes the first character because $row is an array. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 22, 2010 Share Posted June 22, 2010 If all you want is the count of rows, it would be a lot more efficient to simply query for that ie. SELECT COUNT(*) as countof FROM content WHERE cid = '$conid' ... Notice no need for LIMIT etc. If you might actually want to fetch the rows, then you can use the mysql_num_rows() function to get the number of rows, and check to see what that is. Quote Link to comment Share on other sites More sharing options...
fizzzgigg Posted June 22, 2010 Author Share Posted June 22, 2010 Sorry I should have explained more. I dont need a count of rows. I need it to display a message if there's nothing in the database. Like a message that says, "You need to add information." Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 22, 2010 Share Posted June 22, 2010 Yes we get that, but the point is, that the count query always returns a result set. That result set will have one row, and that row has the column which contains the count -- either 0 or > 0. The question is -- if you only need the answer of whether or not there is at least one row that matches the criteria then you can do that with the COUNT(*) query. If you're not going to do anything with the result set, it doesn't make sense to do the query you're doing where you select some number of rows, but never fetch them. If the count of rows > 0 AND you then will fetch those rows, which is doubtful due to the fact that you were using a LIMIT 1, then you can use the mysql_num_rows() function which will also get you the count of rows in the result set, but will also allow you to fetch them. Quote Link to comment Share on other sites More sharing options...
fizzzgigg Posted June 22, 2010 Author Share Posted June 22, 2010 WoW. Let me try it like this. Hey guys, I have a page that will contain information pulled from a database via an mysql_query. What would be the best way to have a message come across if there is no data to be presented? Quote Link to comment Share on other sites More sharing options...
Ruzzas Posted June 22, 2010 Share Posted June 22, 2010 if(mysql_query("COUNT * FROM table") == 0){ die("No info to present to you..."); } Something more cleaner would be good but yes this is basically what you'll need. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 22, 2010 Share Posted June 22, 2010 Did you read what I wrote 2x now about calling mysql_num_rows()? // $result = mysql_query(.... $rowCount = mysql_num_rows(); if ($rowCount echo "There were no rows found."; } else { // fetch the rows in a while loop and output } // Quote Link to comment Share on other sites More sharing options...
Ruzzas Posted June 22, 2010 Share Posted June 22, 2010 Did you read what I wrote 2x now about calling mysql_num_rows()? // $result = mysql_query(.... $rowCount = mysql_num_rows(); if ($rowCount < 1) { echo "There were no rows found."; } else { // fetch the rows in a while loop and output } // That would work better. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 22, 2010 Share Posted June 22, 2010 if(mysql_query("COUNT * FROM table") == 0){ die("No info to present to you..."); } Something more cleaner would be good but yes this is basically what you'll need. That code isn't even remotely close to right. Quote Link to comment 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.