我正在尝试创建一个脚本,用mysql从php中提取数据,并将所有内容放入数组中。这是成功的。
我想弄清楚的是,如何将值与整个数组匹配,然后拉出相应的值。
例如我有:
$q = "SELECT * FROM `addrbook`";
$res = $mysqli->query($q) or trigger_error($mysqli->error."[$q]");
$array = array(); // initialize
while($row = $res->fetch_array(MYSQLI_BOTH)) {
$array[] = array(
'key' => $row[0],
'username' => $row[1],
'password' => $row[2],
'dogeaddr' => $row[3],
'google' => $row[4],
'facebook' => $row[5],
'twitter' => $row[6],
'linkedin' => $row[7],
'youtube' => $row[8]
);
$match = array_search("facebookuser", $array);
if ($match !== FALSE) {
header('Content-Type: application/json');
echo json_encode($match);
}
$res->free();
$mysqli->close();
?>
当前代码:
<?php
error_reporting(E_ALL); ini_set("display_errors", 1);
include '../db/dbcon.php';
$thekey = $_GET['facebook'];
$facebook = $mysqli->real_escape_string($thekey);
if($_GET) {
$q = "SELECT * FROM `addrbook` WHERE `facebook` = " . $facebook;
} else {
//$q = "SELECT * FROM `addrbook`";
echo 'The key to the kingdom, is incorrect.';
}
//Start connection with SQL
$res = $mysqli->query($q) or trigger_error($mysqli->error."[$q]");
//$array = array(); // initialize
while($row = $res->fetch_array(MYSQLI_BOTH)) {
header('Content-Type: application/json');
echo json_encode($row['dogeaddr']);
}
$res->free();
$mysqli->close();
?>
当前代码 - 7:26 MST,USA
<?php
$con = mysqli_connect("localhost", "shooleyb_user", "access2012", "shooleyb_altcend");
$result = mysqli_query($con,"SELECT *FROM `addrbook`");
while ($row = mysqli_fetch_array($result)) {
echo $row['google'];
}
?>
注意:放入facebook ===部分给它一个白色屏幕,谷歌没有索引问题
我的数据库:
addrbook:
key
user
password
addr
google
facebook
twitter
linkedin
youtube
测试的示例行:
201402041741 user1密码123456789 google fbboy twitboy linkboy youtubeboy
我想在数组中找到任何facebook用户(这稍后将是表单中的变量),然后能够在相同的sql行中提取该facebook用户的google。最后,我想将结果(相应的谷歌数据)编码为JSON。
有人知道如何做到这一点,或者我的代码是否错误? 感谢您提前提供的任何帮助。非常感谢。
答案 0 :(得分:2)
现在好了,
将您的查询$ q更改为:
$q = "SELECT * FROM addrbook WHERE facebook='" . $FACEBOOKNAME . "'";
然后你只会获得facebook等于$ FACEBOOK名称的MySQL行,无论它是什么。既然MySQL已经确定了facebook名称是相同的,那么你只需获得你想要的值,在这种情况下就是google。
while($row = $res->fetch_array(MYSQLI_BOTH)) {
header('Content-Type: application/json');
echo json_encode($row['google']);
}
不确定错误发生了什么,但这完全是我格式化代码的方式:
$con = mysqli_connect("example.com", "user", "password", "database");
$result = mysqli_query($con,"SELECT * FROM addrbook WHERE facebook='" . $_GET['facebook']. "'");
while ($row = mysqli_fetch_array($result)) {
echo $row['google'];
}
如果我是你,我会尝试使用您的连接信息这个完整的代码,然后再尝试更改以满足您的需求
SLOPPY CODE
$con = mysqli_connect("example.com", "user", "password", "database");
$result = mysqli_query($con,"SELECT * FROM addrbook");
while ($row = mysqli_fetch_array($result)) {
if ($row['facebook'] === $_GET['facebook']) {
echo $row['google'];
}
}
如果这不起作用我真的不知道