Jump to content

Recommended Posts

Hi  i have a while loop which takes values out of an array. but my else statement wont work. please tell me why

while($row=mysql_fetch_array($result)){
          $Solutions  =$row['solutions'];
          $Issue=$row['Issue'];
          $Category=$row['Category'];

  //-display the result of the array
  echo "<p>"   .$Solutions . " " . $Category .  "</p>\n";

  }
  }
	if($row['solutions'] = ''){
  require("google.php");
  }
  }
  }

 

I just wanna know why it wont even break into an else. or an if. I'm new to php btw.

 

Link to comment
https://forums.phpfreaks.com/topic/191368-while-loop-help/
Share on other sites

<?php
  if(isset($_POST['submit'])){
  if(isset($_GET['go'])){
  if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){
  $name=$_POST['name'];
  //connect  to the database
  $db=mysql_connect  ("localhost", "root",  "blabla") or die ('I cannot connect to the database  because: ' . mysql_error());
  //-select  the database to use
  $mydb=mysql_select_db("final_project");
  //-query  the database table
  $sql="SELECT  Issue, Category,solutions FROM Persons WHERE Issue LIKE '%" . $name .  "%' OR Category LIKE '%" . $name ."%'";
  //-run  the query against the mysql query function
  $result=mysql_query($sql);
  //-create  while loop and loop through result set
  while($row=mysql_fetch_array($result)){
          $Solutions  =$row['solutions'];
          $Issue=$row['Issue'];
          $Category=$row['Category'];

  //-display the result of the array
  echo "<p>"   .$Solutions . " " . $Category .  "</p>\n";

  }
  }
	else if($row['solutions'] = ''){
  require("google.php");
  }
  }
  }
?>

 

 

Thats the whole bit of code.

Link to comment
https://forums.phpfreaks.com/topic/191368-while-loop-help/#findComment-1008902
Share on other sites

I do not even see an else statement in that code. You have way too many braces } which could be because you have not posted the whole code.

 

Before you venture further into programming I would highly suggest you read up on Ident Styles for programming, pick one and stick to it. As well PHP has it's own standards set by PEAR which you can find Pear PHP Code Standards

 

It will help you throughout programming doing proper indentation as it helps with debugging. If you are just using notepad, I would suggest getting an editor like Notepad++ or Netbeans by Java with PHP Syntax Highlighting.

Link to comment
https://forums.phpfreaks.com/topic/191368-while-loop-help/#findComment-1008904
Share on other sites

I think they're referring to the else if, which was written as just an if in the OP.

 

If that's the case, your logic is messed up.  If the first case (the regex) is true, then you access the db and do a bunch of stuff.  If it's not true (else if), then there's no $row['solution'] because you haven't accessed the db.  The db is accessed only if the first case is true.

Link to comment
https://forums.phpfreaks.com/topic/191368-while-loop-help/#findComment-1008906
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.