使用json从数据库中获取数据而不使用数组

时间:2017-10-06 04:27:14

标签: php mysql arrays json

我想使用PHP和MySql从JSON中检索数据库中的对象。我想知道如何在不创建数组的情况下执行此操作?可以吗?

如果是这样,我可以举一个例子说明如何使用这段代码草案吗?

  public static class ImageEncryption
    {
        static string FILENAME = @"D:\Documents\Watermark\EBCDocument\EBC021700725665\test.pdf\Page1.jpg";
        static string ENCFILENAME =  @"D:\Documents\Watermark\EBCDocument\EBC021700725665\test.pdf\Page1.jpg";
       public static void ImageTripleDESCrypto()
        {
            //Create instance of DES
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            //Generate IV and Key
            des.GenerateIV();
            des.GenerateKey();
            //Set Encryption mode
            des.Mode = CipherMode.ECB;
            //Read
            FileStream fileStream = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
            MemoryStream ms = new MemoryStream();
            fileStream.CopyTo(ms);
            //Store header in byte array (we will used this after encryption)
            var header = ms.ToArray().Take(54).ToArray();
            //Take rest from stream
            var imageArray = ms.ToArray().Skip(54).ToArray();
            //Create encryptor
            var enc = des.CreateEncryptor();
            //Encrypt image
            var encimg = enc.TransformFinalBlock(imageArray, 0, imageArray.Length);
            //Combine header and encrypted image
            var image = Combine(header, encimg);
            //Write encrypted image to disk
            fileStream.Close();
            File.WriteAllBytes(ENCFILENAME, image);


        }
    public static byte[] Combine(byte[] first, byte[] second)
    {
        byte[] ret = new byte[first.Length + second.Length];
        Buffer.BlockCopy(first, 0, ret, 0, first.Length);
        Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
        return ret;
    }
}

1 个答案:

答案 0 :(得分:0)

$sql = "select name,email from contact";

$res = mysqli_query($conn,$sql) or die(mysqli_error($conn));
$num = mysqli_num_rows($res);

$json = array();
if($num > 0)
{
    while ($obj=mysqli_fetch_object($res))
    {
        $json[] = $obj;
    }
}
echo json_encode($json);

输出:

 [{"name":"test","email":"test@gmail.cmom"},{"name":"test1","email":"test1@gmail.cmom"}]