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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

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.