Reputation: 1580
I was tring to get values from mysql db and creating json from php. But am always getting "false" as reply.
My query is working correctly .
<?php
header("Content-type: text/html; charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$charset="UTF8";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sth = mysqli_query("select
a.id,
a.fname,
a.mname,a.lname,a.country,a.city,a.dob,a.role,
a.email,
b.mobile,b.skypeid,b.address,b.languages,
c.height,
c.width,
c.skin,
c.bust,
c.waist,
c.hips,
c.shoesize,
c.hair,
c.eye,
c.comments,
d.movie,
d.advertisement,
d.brandpromotional,
d.danceshow,
d.runway,
d.catalog,
d.editorial,
d.fit,
d.casual,
d.corporate,
d.swimwear,
d.fitness,
d.magazine,
d.lingerie,
d.glamour,
d.alternative,
d.hair,
d.legs,
d.hands,
d.webmodel,
d.social,
d.experience
from
basicinfo a
join contactdetails b
on a.email=b.email
join measurements c
on a.email = c.email
join areainterest d
on a.email=d.email
where a.role='Model'");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
?>
please help me
Upvotes: 1
Views: 69
Reputation: 1580
this one is working..
<?php
header("Content-type: text/html; charset=utf-8");
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$charset="UTF8";
// Create connection
$connection = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
$sth = "select
a.id,
a.fname,
a.mname,a.lname,a.country,a.city,a.dob,a.role,
a.email,
b.mobile,b.skypeid,b.address,b.languages,
c.height,
c.width,
c.skin,
c.bust,
c.waist,
c.hips,
c.shoesize,
c.hair,
c.eye,
c.comments,
d.movie,
d.advertisement,
d.brandpromotional,
d.danceshow,
d.runway,
d.catalog,
d.editorial,
d.fit,
d.casual,
d.corporate,
d.swimwear,
d.fitness,
d.magazine,
d.lingerie,
d.glamour,
d.alternative,
d.hair,
d.legs,
d.hands,
d.webmodel,
d.social,
d.experience
from
basicinfo a
join contactdetails b
on a.email=b.email
join measurements c
on a.email = c.email
join areainterest d
on a.email=d.email
where a.role='Model'";
$result = mysqli_query($connection, $sth) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
//close the db connection
mysqli_close($connection);
?>
Upvotes: 1
Reputation: 4760
mysqli_query is missing the connection parameter but since you are new mysqli
and not mysqli_connect
it looks like you need to query the data in a different way... the easiest way to update your code would be to do this:
Change this:
$sth = mysqli_query("select
To this:
$sth = $conn->query("select
Then change this:
while($r = mysqli_fetch_assoc($sth)) {
To this:
while($r = $sth->fetch_assoc()) {
PHP has three different ways you can connect to the database: object oriented style, procedural style or using PDO. The code was intermixing the object oriented and procedural style so it wasn't working as expected. mysqli_query was also missing the link variable which is the first parameter.
Read more here: http://php.net/mysqli_query
Upvotes: 0