PHP SQL query to print results in webpage -


i trying php script print rows have in database in neat order. im not getting anything. table has 4 columns, name, address, long , lat, , 2 rows data. table called locations. using following code im not getting to work:

<?php  $con=mysqli_connect("localhost","user","pass","db");  if (mysqli_connect_errno()) {   echo "failed connect mysql: " . mysqli_connect_error(); }  $sql = "select * `locations` ";  if ($result = mysqli_query($con, $sql)) {  $resultarray = array();  $temparray = array();   while($row = $result->fetch_object())  {   $temparray = $row;      array_push($resultarray, $temparray);  }   echo json_encode($resultarray); }  // close connections mysqli_close($con); ?> 

here simple example using pdo instead of mysqli

    $dbhost = 'localhost';     $dbname = 'nilssoderstrom_';     $dbuser = 'nilssoderstrom_';     $dbpass = 'durandal82!';     $pdo = new pdo('mysql:host=' . $dbhost . ';dbname=' . $dbname, $dbuser, $dbpass); // create connection      $stmt = $pdo->prepare("select name, address, long, lat locations");     //you should never use *, call each field name going use      $stmt->execute(); // run statement     $arr = $stmt->fetchall(pdo::fetch_assoc); // fetch rows , put associative array      print_r($arr); // print array items, unformatted 

and can echo out data , format using loop so

    for($i=0; $i<sizeof($arr); $i++) { // loop through each row in database. prefer method on while loops testing has shown faster large scale tables         echo 'name: ' . $arr[$i]['name'] . '<br />'; // $arr array name, $i number of array item, or iterator, ['name'] field name         echo 'address: ' . $arr[$i]['address'] . '<br>';         echo 'long: ' . $arr[$i]['long'] . '<br>';         echo 'lat: ' . $arr[$i]['lat'] . '<br>';     } 

if names correct, echo out row id , row city. change names field names. if want further assistance, feel free ask.

however, if want stick mysqli, give following code wirl.

    $dbhost = 'localhost';     $dbname = 'nilssoderstrom_';     $dbuser = 'nilssoderstrom_';     $dbpass = 'durandal82!';     $mysqli = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);     $query = "select name, address, long, lat locations";     $result = mysqli_query($mysqli, $query);      if($result) {         while($row = mysqli_fetch_assoc($result)) {             echo 'name: ' . $row['name'] . '<br />';             echo 'address: ' . $row['address'] . '<br>';             echo 'long: ' . $row['long'] . '<br>';             echo 'lat: ' . $row['lat'] . '<br>';             }     } 

change fieldname field want display

edit: paste following code. echo out number of rows. tell if query statement correct.

    $dbhost = 'localhost';     $dbname = 'nilssoderstrom_';     $dbuser = 'nilssoderstrom_';     $dbpass = 'durandal82!';     $pdo = new pdo('mysql:host=' . $dbhost . ';dbname=' . $dbname, $dbuser, $dbpass);      $stmt = $pdo->query("select name, address, long, lat locations");     echo $stmt->rowcount(); 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -