1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Retrieve data SQL syntax error

Discussion in 'Databases' started by mirosoft1, Mar 18, 2018.

  1. #1
    Hello,
    Can you please correct the syntax error in this code
    
    if(mysqli_num_rows($sql) > 0)
        {
            while($row = mysqli_fetch_array($sql))
            {
                $result .='
                <tr>
                <td>'.$row["title"].'</td>
                <td>'.$row["name"].'</td>
                if($row["type"]==2)
                { echo "<td>type2</td>";}
                  else if($row["type"]==1)
                 {echo"<td>type1</td>";}
                 else if($row["type"]==3)
                 {echo"<td>type3</td>";}
               else if($row["type"]==4)
               {echo"<td>type4</td>";}
                <td>'.$row["add_date"].'</td>
                </tr>';
            }
        }
        else
    
    Code (markup):

    SEMrush
     
    mirosoft1, Mar 18, 2018 IP
    SEMrush
  2. sarahk

    sarahk iTamer Staff

    Messages:
    24,129
    Likes Received:
    3,252
    Best Answers:
    92
    Trophy Points:
    615
    #2
    this should do the trick

    if(mysqli_num_rows($sql) > 0)
        {
            while($row = mysqli_fetch_array($sql))  {
                $result .= "<tr>
                <td>{$row['title']}</td>
                <td>{$row['name']}</td>
                <td>type{$row['type']}</td>
                <td>{$row['add_date']}</td>
                </tr>";
            }
        }
        else
    PHP:
    If you need to add more logic around $row['type'] then use switch.
     
    sarahk, Mar 18, 2018 IP
  3. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Thank you for your reply but if i want to change the echo "message" and type anything else like echo"Hello world" instead of "type 1"
    this solution didn't work for me.
     
    mirosoft1, Mar 19, 2018 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    24,129
    Likes Received:
    3,252
    Best Answers:
    92
    Trophy Points:
    615
    #4
    I'm not following you.

    I suspect you need to find some basic php tutorials and work through them.
     
    sarahk, Mar 19, 2018 IP
  5. Cameron Fillers

    Cameron Fillers Greenhorn

    Messages:
    18
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    23
    #5
    The response that sarahk provided is how you want to build the content. I think the part that the OP is confused about, is how to handle the IF statements for his switches.

    for instance, you should set the data to a variable, and add upon it based on the if statements, like such

    if(mysqli_num_rows($sql) > 0)
        {
            while($row = mysqli_fetch_array($sql)) {
                $output = '';
                $output .='<tr>';
                $output .= '<td>'. $row["title"]. '</td>';
                $output .= '<td>'. $row["name"]. '</td>';
               
                if ($row["type"]==2) {
                    $output .= "<td>type2</td>";
                }
                else if ($row["type"]==1) {
                    $output .= "<td>type1</td>";
                }
                else if ($row["type"]==3) {
                    $output .= "<td>type3</td>";
                }
                else if ($row["type"]==4){
                    $output .= "<td>type4</td>";
                }
                $output .= '<td>'. $row["add_date"]. '</td>';
                $output .= '</tr>';
                echo $output;
            }
        }
        else
    PHP:
    You could also convert the multiple if/else if into a case statement, as that would be a fairly simple switch, and if you want to build the entire page, you can remove the clearing of $ouput at the beginning of the while loop, and move the echo to outside the while, but I believe that this configuration is more to what the OP is looking for.
     
    Cameron Fillers, Jun 18, 2018 IP