代码:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>TestProj</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>TestProj</PackageId>
<UserSecretsId>aspnet-TestProj-6af8ade3-87ff-4468-a9ce-8bb69c696ab8</UserSecretsId>
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
<PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
<ItemGroup>
<None Remove="Properties\PublishProfiles\TestProjP.pubxml" />
<None Remove="Properties\PublishProfiles\FolderProfile.pubxml" />
<None Remove="Properties\PublishProfiles\FolderProfile1.pubxml" />
</ItemGroup>
<ItemGroup>
<None Update="wwwroot\**\*;Views\**\*;Areas\**\Views">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="bootstrap" Version="2.3.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration.Tools" Version="1.1.0-preview4-final" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Session" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
<PackageReference Include="BundlerMinifier.Core" Version="2.3.327" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="1.1.2" />
<PackageReference Include="EPPlus.Core" Version="1.4.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
</ItemGroup>
</Project>
示例:http://localhost/Contable/autocompletecod.php?term=3269
输出如下
<?php
$datoBuscar = $_GET["term"];
$conexion = mysql_connect("localhost", "root", "");
mysql_select_db("ap_contable");
$ssql = "SELECT provincia_id, nombre, codigopostal FROM ciudades WHERE codigopostal LIKE '%" . $datoBuscar . "%' LIMIT 50";
$rs = mysql_query($ssql, $conexion);
$arrayElementos = array();
while ($fila = mysql_fetch_array($rs)){
echo $fila[provincia_id];
$queryp = "SELECT nombre FROM provincias WHERE id = '$fila[provincia_id]' LIMIT 1";
$resultp = mysql_query($queryp, $conexion);
$foundp = mysql_fetch_array($resultp);
echo $foundp['nombre'];
$row['prov'] = $foundp['nombre'];
$row_set[] = $row;
}
echo json_encode($row_set);
?>
我故意让ECHO看看是否从MySQL数据库中获取数据以及是否获得了数据。问题是,当我将它分配给数组时,$ row ['prov'] = $ foundp ['nombre'];问题出在哪儿?它说NULL应该说Entre Rios
答案 0 :(得分:1)
您的字符串需要采用UTF-8编码(这是json_encode()
要求)。试试这个:
$row['prov'] = utf8_encode($foundp['nombre']);
或者,您可以在MySQL连接中将CHARACTER SET
设置为utf8mb4
。理想情况下,您应该使用UTF-8。阅读this。
答案 1 :(得分:0)
未使用$ arrayElementos
您应该加入以下两个表:SELECT p.nombre FROM provincias AS p JOIN ciudades AS c ON p.id = c.provincia_id WHERE c.codigopostal LIKE '%" . $datoBuscar . "%'
https://dev.mysql.com/doc/refman/5.7/en/join.html
$ row_set未定义
$ row未定义
结束?&gt;不应该用于纯PHP文件Why would one omit the close tag?
所以,它应该看起来像这样(未经测试的代码):
$datoBuscar = $_GET["term"];
$db = new PDO('mysql:host=localhost;dbname=ap_contable', 'root', 'PASSWORD', [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"]);
$query = $db->prepare('SELECT p.nombre FROM provincias AS p JOIN ciudades AS c ON p.id = c.provincia_id WHERE c.codigopostal LIKE \'%?%\'');
$query->execute([$datoBuscar]);
$results = $query->fetchAll();
$json = [];
foreach($results AS $result)
{
$row = [];
$row['prov'] = $result['nombre'];
$json[] = $row;
}
echo json_encode($json);
PS:如果你想编写好的PHP代码,启用通知和警告错误报告,你会惊讶于你错过了什么和“这段代码如何在第一位工作?!”
答案 2 :(得分:0)
要将$ row数组分配到$ row_set数组,请使用此
$row_set = $row;
这样你就可以在$ row_set中获得$ row数组的所有内容。
每当你运行一个loog并且你在同一个索引处设置$ row_set的值时,你将只获得数组中的最后一个循环执行值。如果您想要所有结果,那么您可以使用array_push();
来自php manual int array_push ( array &$array , mixed $value1 [, mixed $... ] )
的描述。
在您的情况下,您可以使用
array_push($row_set, $row);
而不是
$row_set[] = $row;