Jump to content

[SOLVED] mysql_num_rows


me1000

Recommended Posts

Hi, So I have a table, in it there is a bunch of images that have a field that contains a number for which they are associated with. here is a partial screenshot

picture1zr0.png

 

now I need to get the images from the page, but only if there are images. if there are no images associated with the page defined, then it will not display anything.

 

$inPageID = (is_numeric($_REQUEST['page'])) ? $_REQUEST['page'] : "1";
$numpic = mysql_query("SELECT * FROM VISUALS WHERE ASSO=$inPageID");
	$num_pic = mysql_num_rows($numpic);

		if ($num_pic > "0" ) {
		echo '<div>';
		echo 'it found pics';
    		echo '</div>';
		}

 

trouble is it doesn't work!  ???

 

however if I change the greater than sign

if ($num_pic > "0" )

to an equal sign it doesn't work either.

so just out of curiosity I changed it to a less than sign, and what do you know, it worked!

 

So since I know it is not returning a negative number is it returning a null set?

 

Can anyone help?

 

Thanks,

Link to comment
Share on other sites

$inPageID = (is_numeric($_REQUEST['page'])) ? $_REQUEST['page'] : "1";
$numpic = mysql_query("SELECT * FROM VISUALS WHERE ASSO=$inPageID");
	$num_pic = mysql_num_rows($numpic);

		if ($num_pic > 0) {
		echo '<div>';
		echo 'it found pics';
     		echo '</div>';
		}

 

Don't use " " around ints that are not to be taken literally.

Link to comment
Share on other sites

it seems Im having a similar problem, however with a different part.

Why is this so difficult?

 

anyway,

 

$inLetterID       = (isset($_REQUEST['letter'])) ? $_REQUEST['letter'] : "A";
echo $inLetterID;

This always prints "A" (unless otherwise defined)

 

$sqlquery = "SELECT * FROM INFORMATION WHERE LETTER like $inLetterID";
$num_pages = mysql_num_rows($sqlquery);
		echo '<br>'.$num_pages.': NUMBER OF PAGES<br>';

(for simplicity sake, i did not include the loop)

 

the above puts this on the screen,

: NUMBER OF PAGES

no number (the number should return "1")

 

 

here are some DB details,

Table name: INFORMATION

Column name: LETTER    -    (VARCHAR; 1 character )

 

any ideas?

 

Thanks,

Link to comment
Share on other sites

here is what I recommend to you

first echo your query

echo $sqlquery

and then copy paste that in mysql client to check if you get something back.

 

second it is really not a good practice to assume that your query will not fail

something like

$num_pages = mysql_num_rows($sqlquery) OR die(mysql_error);

will help you a lot

 

And if you really want to know the number of rows I would use count in the query

 

just some suggestions

Link to comment
Share on other sites

here is what I recommend to you

first echo your query

echo $sqlquery

and then copy paste that in mysql client to check if you get something back.

So i tried it, and got this back

"Showing rows 0 - 0 (1 total, Query took 0.0003 sec)"

 

second it is really not a good practice to assume that your query will not fail

something like

$num_pages = mysql_num_rows($sqlquery) OR die(mysql_error);

will help you a lot

ok, added that, and I will keep that in mind for future referance,

 

And if you really want to know the number of rows I would use count in the query

Really all I care about is making sure there is more than one entry, at this point,

I need all the information queried later on

I just left all that part out for simplicity sake.

 

 

isset($_REQUEST['letter'])

aways a so that is always false check if that is really set

my apologies for not being clear, I meant that if the variable is not defined it comes back as "A" however it defines just fine as any other letter.

 

 

 

Thanks for you advise,

im guessing it is on the database end since it is not coming back from the mysql client.

 

here is what the row looks like, maybe that is of some help.

 

picture1ch5.png

i am just clueless at this point,

Link to comment
Share on other sites

Ok, guys it's real simple:

 

$sqlquery = "SELECT * FROM INFORMATION WHERE LETTER like $inLetterID";
$num_pages = mysql_num_rows($sqlquery);

 

You never ran the query! You are running mysql_num_rows() against a string!

 

$sqlquery = "SELECT * FROM INFORMATION WHERE LETTER like $inLetterID";
$result = mysql_query($sqlquery) or DIE (mysql_error());
$num_pages = mysql_num_rows($result);

 

Link to comment
Share on other sites

Woohoo! 1 bug down,

 

since I think making a new topic for this next question would be unnecessary, I am not getting this error when running the script,

 

Unknown column 'A' in 'where clause'

 

not sure why I get this, because it shouldnt be calling for a column 'A', it is calling for column 'LETTER' where the value is either 'A' or 'a'

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.