Jump to content

[SOLVED] PHP + MYSQL does not reder any fields


14pulsars

Recommended Posts

Hi folks, I need some help:

 

I'm retrieving data from a DB based on two variables in the SQL query string (a month number and a week number).

 

All records pull up fine for (monthNumber=1 AND weekNumber=1), but any other weekNumber, monthNumber, or any combination thereof pulls a blank.

 

Also, everything shows up with if the query string just includes (weekNumber=n), but the first record of every month is missing is the query string just includes (monthNumber=n)

 

Here is what I have tried:

  • I though that the weekNumber was not an integer, but a string. is_int() told me $weekNumber is not an integer; is_numeric() returned false. Type Casting or intval() did nothing.
  • Placing single quotes around the variables in the query string in every single combination I could think of
  • I ran the exact same queries through PHPMyAdmin and the right records show up

I'm at my whit's end here - any help would be greatly appreciated.

 

Here is my code: (The month variable goes through the array for a reason, by the way)

$dbh=mysql_connect ("localhost", "DB_NAME", "PASSWORD) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("TABLE_NAME");

$monthArray = array(
'udhYdh' => 1,
'IUshd8' => 2,
'Cdne92' => 3,
'Cmdiuj' => 4,
'jd83wh' => 5,
'Vmd93f' => 6,
'd9dfOv' => 7,
'8dFvhj' => 8,
'lCjuew' => 9,
'mDUfn4' => 10,
'9dnjCu' => 11,
'KIDc2m' => 12
);


$errorMsg = 'Sorry, the following error(s) occured:<br />';

if (isset($_GET['month']))
$monthNumber = $monthArray[$_GET['month']];
else
 $errorMsg = $errorMsg.'- Invalid Month<br />';

if ($_GET['week'] == 1 || $_GET['week'] == 2 || $_GET['week'] == 3 || $_GET['week'] == 4)
$weekNumber = $_GET['week'];
else
$errorMsg = $errorMsg.'- Invalid Week<br />';

if (isset($monthNumber) && isset($weekNumber)){
$data = mysql_query("SELECT * FROM amember_weeks WHERE monthNumber=$monthNumber AND weekNumber=$weekNumber") or die(mysql_error());
$info = mysql_fetch_array($data);
}

... page contents were here ....

if (isset($monthNumber) && isset($weekNumber))
	  {
		  while($info = mysql_fetch_array( $data ))
			{
				print $info['id']." - ".$info['title'].'<br />'.$info['weekDesc'].'<br /><br />';
			}
	  } else
	  { print $errorMsg; }

If your query returns only 1 row then nothing will print. You only print from the second row onwards.

 

<?php
$data = mysql_query("SELECT * FROM amember_weeks WHERE monthNumber=$monthNumber AND weekNumber=$weekNumber") or die(mysql_error());
$info = mysql_fetch_array($data);                          // reads first row and ignores it.  REMOVE

If your query returns only 1 row then nothing will print. You only print from the second row onwards.

 

<?php
$data = mysql_query("SELECT * FROM amember_weeks WHERE monthNumber=$monthNumber AND weekNumber=$weekNumber") or die(mysql_error());
$info = mysql_fetch_array($data);                          // reads first row and ignores it.  REMOVE

 

That Worked, thank you!

 

I guess the Resource ID#2 was referring to a second row that did not exist, meaning it did not render anything.

 

Is my logic flawed?

 

Anyway, thanks alot Barand.

I guess the Resource ID#2 was referring to a second row that did not exist, meaning it did not render anything.

 

Is my logic flawed?

 

Yes.

 

If a query fails due to an error it returns false. If it works it returns a "Resource". The resource number is just its internal address of the recordset returned by the query.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.