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.

if to Show records

Discussion in 'PHP' started by piropeator, May 1, 2017.

  1. #1
    Hi. I try this code:
    $Data = new Conection();
    
    $sql = "SELECT fiel1, field2 FROM mytable WHERE condicion";
    if ($stm = $Data->query($sql)) {
      echo "There are records";
    } else {
      echo "No Records";
    }
    PHP:
    I want to show "There are records" if mytable are records.
    I want to show "No Records" if mytable is empty.
    But always show "There are records" Why?
     
    piropeator, May 1, 2017 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Because your if is checking to see if the query is run - which, as long as there are no errors, it is. Hence, it will always return "There are records", regardless of how many records is found.
     
    PoPSiCLe, May 4, 2017 IP
    NetStar likes this.
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #3
    Also your if statement should be == instead of =

    if ($stm = $Data->query($sql)) {
    Code (markup):
     
    sarahk, May 4, 2017 IP
  4. matt_62

    matt_62 Prominent Member

    Messages:
    1,827
    Likes Received:
    515
    Best Answers:
    14
    Trophy Points:
    350
    #4
    I would also check if its:
    "fiel1" or "field1"
     
    matt_62, May 5, 2017 IP
    NetStar likes this.
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    The correct way to do all this would first be to fix all the spelling errors, then use whatever method of rowCount() you have available. In PDO this would be something like this:
    
    $data = new Connection(); //you never START a variable with a capital letter
    $stmt = $data->query("SELECT field1, field2 FROM mytable WHERE condition");
    if ($stmt->rowCount() > 0) {
      echo "There are records";
    } else {
      echo "There are no Records";
    }
    
    PHP:
     
    PoPSiCLe, May 5, 2017 IP
  6. piropeator

    piropeator Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    121
    #6
    But In PHP manual, says:
    http://php.net/manual/en/pdostatement.rowcount.php (in Example #2 Counting rows returned by a SELECT statement).
     
    piropeator, May 5, 2017 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Well... depends on the database. MySQL supports rowCount() on select.

    Ref:
    "If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications." (same page)
     
    PoPSiCLe, May 7, 2017 IP