Jump to content

Switch Statements: Why Wont My Query Print?


SelfObscurity

Recommended Posts

I'm using a switch with multiple nested switches.  I tested the switch and nested switches by setting up the structure, and using basic text for the cases.  For example, instead of one of the nested switch cases needing to = $_GET['id'], I just entered two cases, each equaling 1 and 2, then the default case.

 

Once I begin to enter my SQL coding to connect to the databases, only certain information prints.  For example; on my switch that is $_GET['page'], I have a case for 'news'.  Inside this case, I have a nested switch called $_GET['id'], which will display information if the page=nes&id=whatever.  That works, but when I go do the page=news, nothing displays.  My coding is omitting displaying the default information if there is no $_GET['id'].

 

Attached, I am going to have my .php file.  The reason for not posting it here is because my coding is rather messy.  Please help!

 

[attachment deleted by admin]

If I am not mistaken it is because you in the case that you are checking for an id (under news) your query is still adding the id=$id...you need to remove that so that it can get the results. Also just a note...why dont you just use an if stament inside of the switch since there is only one case.

Well, the case that calls for the ID in the news section, is the part that actually works.  It's the default case that isn't showing any information.  The reason I'm using switches instead of an if statement is because I was told it was easier.  I'm doubting this.

switches are not "easier" lol. they are used when you have several things that something can match. Any statement that uses a switch statement can easily use an if statement and look even cleaner. However, my wording on the last one got screwed up I think. this is the part of code I am talking about:

 

switch ($_GET['page']) {
case 'news':
		$id = $_GET['id'];
		switch ($id) {
			case $id:
				mysql_select_db ($db, $link);
				$query = "SELECT * from news WHERE id = '$id'";
				$result = mysql_db_query ($db, $query, $link);
				while ($row = mysql_fetch_array ($result)) {
					echo ("ID $row[id] title is $row[title]<br />");
				}
				break;
              	default:
				mysql_select_db ($db, $link);
				$query = "SELECT * from news WHERE id = '$id'";
				$result = mysql_db_query ($db, $query, $link);
				while ($row = mysql_fetch_array ($result)) {
					echo ("ID $row[id] title is $row[title]<br />");
				}
                  	break;
           }
           break;

 

If you notice in your default cause you still have the WHERE id='$id'. That needs to be removed and only needs to be in the case where you are checking for an id. However I would rewrite this section to look like this:

 

switch ($_GET['page']) {
case 'news':
		if(is_numeric($id)){
				mysql_select_db ($db, $link);
				$query = "SELECT * from news WHERE id = '$id'";
				$result = mysql_db_query ($db, $query, $link);
				while ($row = mysql_fetch_array ($result)) {
					echo ("ID $row[id] title is $row[title]<br />");
				}
		} else {
				mysql_select_db ($db, $link);
				$query = "SELECT * from news";
				$result = mysql_db_query ($db, $query, $link);
				while ($row = mysql_fetch_array ($result)) {
					echo ("ID $row[id] title is $row[title]<br />");
				}
		}
           }
      break;

 

Also I noticed in your code that you are doing mysql_select_db a bunch of times and it seems as if you are using the same database. You only need to call this function once to connect to a database and it will keep that connection. Which is the same for mysql_connect (unless you close the connection). Whenever you need to change databases you can then call it again but it is unnecessary to keep calling this.

ngreenwood6,

 

Thank you for your response.  I've re-written the code you posted, and still, it will not show the default news information, but if I put &id=1 after the URL, it shows up.  This isn't making sense to me!

This should automatically pull up the id equivalent to 1.

 

    switch ($_GET['page']) {
        case 'news':
            if(is_numeric($id)){
                mysql_select_db ($db, $link);
                $query = "SELECT * from news WHERE id = '$id'";
                $result = mysql_db_query ($db, $query, $link);
                    while ($row = mysql_fetch_array ($result)) {
                        echo ("ID $row[id] title is $row[title]<br />");
                    }
            } else {
                $id = "1";
                mysql_select_db ($db, $link);
                $query = "SELECT * from news WHERE id='$id'";
                $result = mysql_db_query ($db, $query, $link);
                    while ($row = mysql_fetch_array ($result)) {
                        echo ("ID $row[id] title is $row[title]<br />");
                    }
            }
    }
    break;

 

Hope I could be of some help. ngreenwood6 made it so that it would display all the data in the database. I hope that could be some help.

 

Thanks,

Colton Wagner

Colton,

 

That's what I'm going for.  The first half of the if statement, is to call information based on if id equals whatever the id field is.  The second half of the if statement, calls everything.  It's the second half that won't display anything, not even my table, just blank space.  I've been literally working at it for 3 days!

I'm unsure what changing what I use as a value for ID will do.  When I go to my index.php, I'm displaying all news posts with no problem.  I copied the exact PHP/SQL coding for the default news case, so it should be working.  I believe I am going to change to if statements, see what I can accomplish.

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.