我使用ajax将数据发送到查询mysql并返回结果的php页面。我在我的页面中有一个包含dept值的变量。但是,如果我var_dump($ _ POST),我发现var是一个数组。
如果我在查询中手动输入值,则返回数据。但是,只是没有使用我的$ dept var。
如何解码此数组以使变量可用于我的查询。感谢
ajax代码
dept=DEMOBILL
在firebug标签中发帖
<?php
$dept = $_POST['dept']; <--- array?
//open connection to mysql db
$connection = mysqli_connect("localhost","root","","sample") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select custref from boxes where department = '".$dept."' and status = 1";
$result = mysqli_query($connection, $sql) 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);
?>
deptdata.php
doctrine:
orm:
entity_managers:
default:
mappings:
ApplicationSonataMediaBundle: ~
SonataMediaBundle: ~
dbal:
types:
json: Sonata\Doctrine\Types\JsonType
sonata_media:
# if you don't use default namespace configuration
#class:
# media: MyVendor\MediaBundle\Entity\Media
# gallery: MyVendor\MediaBundle\Entity\Gallery
# gallery_has_media: MyVendor\MediaBundle\Entity\GalleryHasMedia
db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
default_context: default # you need to set a context
contexts:
default: # the default context is mandatory
providers:
- sonata.media.provider.dailymotion
- sonata.media.provider.youtube
- sonata.media.provider.image
- sonata.media.provider.file
- sonata.media.provider.vimeo
formats:
small: { width: 100 , quality: 70}
big: { width: 500 , quality: 70}
cdn:
server:
path: /uploads/media # http://media.sonata-project.org/
filesystem:
local:
directory: "%kernel.root_dir%/../web/uploads/media"
create: false
答案 0 :(得分:1)
$myArray = json_decode($data, true);
echo $myArray[0]['id']; // Fetches the first ID
echo $myArray[0]['c_name']; // Fetches the first c_name
// ...
echo $myArray[2]['id']; // Fetches the third ID
// etc..
如果没有将true作为第二个参数传递给json_decode,它会将其作为对象返回:
echo $myArray[0]->id;