0

i am new at php, how to connect and get table value when i open http://localhost/x/x.php?push=1 i am trying to echoing the result but when i try it, it shows nothing

right now i am doing like this and it's not working

<?php
    //this block is to post message to GCM on-click
    $pushStatus = "";   
    if(!empty($_GET["push"])) {
        $mysqli = new mysqli("localhost", "root", "", "bimbinganpa");
        $list_dosen = array();
        $test ="test";
        $list_dosen = $mysqli->query("SELECT no FROM user_id WHERE tipe_user = mahasiswa");
        echo $list_dosen;

        $lines=array();
        $fp=fopen('GCMRegId.txt', 'r');
        while (!feof($fp)){
            $line=fgets($fp);
            //process line however you like
            $line=trim($line);
            //add to array
            $lines[]=$line;
        }

        $gcmRegID  = file_get_contents("GCMRegId.txt");
        $pushMessage = $_POST["message"];   
        if (isset($gcmRegID) && isset($pushMessage)) {      
            $gcmRegIds = array($gcmRegID);
            $pushStatus = sendPushNotificationToGCM($lines, $pushMessage);
        }       
    }
?>

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Togan J.R
  • 99
  • 1
  • 13
  • check your web server error log. – danblack Mar 06 '20 at 06:41
  • First off, strings in SQL need to be quoted: `column = 'value'`. Also, on a successful SELECT query using `$mysqli->query()`, it will return a mysqli result objects which you can't echo. – M. Eriksson Mar 06 '20 at 06:48

3 Answers3

3

String needs a quotation in query...

can you try like following

SELECT no FROM user_id WHERE tipe_user = 'mahasiswa'
Antony Jack
  • 480
  • 2
  • 16
2

Your string is not having quotation. If you are trying to compare string in QUERY then you have to add quotation

Please below query for reference

SELECT no FROM user_id WHERE tipe_user = 'mahasiswa'
Rahul Koshti
  • 186
  • 1
  • 10
1

You are echoing the result that database return, Indeed it never works. So you have to write loop to loop through and fetch data in every row like below and add single quote in query to make it string to compare.

    $list_dosen = $mysqli->query("SELECT no FROM user_id WHERE tipe_user = 'mahasiswa'");
    if ($list_dosen->num_rows > 0) {
       // output data of each row
       while($row = $list_dosen->fetch_assoc()) {
        echo $row["columnName"];
       }
    } else {
    echo "0 results";
    }

First check that result have some rows by num_rows and then fetch rows by method fetch_assoc(). In while loop you can get value of every row's column

Ahmed Ali
  • 1,908
  • 14
  • 28