PHP数据库循环遍历数组以查找id,然后使用id显示该列的值

时间:2018-01-20 23:50:41

标签: php mysql arrays wordpress

目前,我已经能够找到正确的表格并print_r数组的内容。现在,我试图遍历该数组,并根据id显示该数组中的用户名和点。这就是数组的样子

Array
(
[0] => stdClass Object
(
[id] => 5a61d2f40394d
[first] => Test
[last] => Guy3
[company] => 123 Company
[points] => 50
[url] => https://example.com/points/?user=5a61d2f40394d
[created] => 0000-00-00 00:00:00
[updated] => 2018-01-19 06:22:42
)

[1] => stdClass Object
(
[id] => 5a61cdddc359d
[first] => Test
[last] => Guy1
[company] => 123 Company
[points] => 0
[url] => https://example.com/points/?user=5a61cdddc359d
[created] => 0000-00-00 00:00:00
[updated] => 2018-01-19 05:52:13
)

[2] => stdClass Object
(
[id] => 5a61d054a0915
[first] => Test
[last] => Guy2
[company] => 123 Company
[points] => 0
[url] => https://example.com/points/?user=5a61d054a0915
[created] => 0000-00-00 00:00:00
[updated] => 2018-01-19 06:02:44
)

)

这是我到目前为止所写的内容,而且我已经能够找到该ID,但现在我想访问这些点并在之后更新它们。

global $wpdb; //this is wordpress so I'm accessing the database
//here I'm reading the variable ?user= in the url
$queryURL = parse_url( html_entity_decode( esc_url( add_query_arg( $arr_params ) ) ) );
parse_str( $queryURL['query'], $getVar );
$user = $getVar['user'];

$customers = $wpdb->get_results("SELECT * FROM `wpjw_skmember`;");
//I've found the users id but now I need to update their points but I don't know how.
$first = array_column($customers, 'id');

foreach($first as $value) {
   if  ($value == $user) {
       echo 'user found'
   }    
}

1 个答案:

答案 0 :(得分:1)

如果您的$ customers数组具有上面发布的格式,那么访问其中的值将如下所示:

Internal Server Error: /accounts/signup/
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 53, in inner
    return func(*args, **kwds)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
    return view(request, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/views.py", line 210, in dispatch
    return super(SignupView, self).dispatch(request, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/views.py", line 79, in dispatch
    **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/views.py", line 188, in dispatch
    **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/views.py", line 102, in post
    response = self.form_valid(form)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/views.py", line 231, in form_valid
    self.get_success_url())
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/utils.py", line 188, in complete_signup
    signal_kwargs=signal_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/utils.py", line 151, in perform_login
    send_email_confirmation(request, user, signup=signup)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/utils.py", line 319, in send_email_confirmation
    signup=signup)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/models.py", line 60, in send_confirmation
    confirmation.send(request, signup=signup)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/models.py", line 166, in send
    get_adapter(request).send_confirmation_mail(request, self, signup)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/adapter.py", line 447, in send_confirmation_mail
    ctx)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/allauth/account/adapter.py", line 140, in send_mail
    msg.send()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/mail/message.py", line 348, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 104, in send_messages
    new_conn_created = self.open()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 71, in open
    self.connection.login(force_str(self.username), force_str(self.password))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/smtplib.py", line 696, in login
    "SMTP AUTH extension not supported by server.")
smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server.

因此,您可以遍历$ customers并测试该数组中的每个客户,以查看ID值是否匹配,然后访问其他值:

echo $customers[0]->last; // returns "Guy3"

在识别匹配用户后使用break将退出foreach循环,防止其他不必要的迭代。