Jump to content

What am I doing wrong? This looks to be so simple.


fizzzgigg

Recommended Posts

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.

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}

//

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.