php - 使用 php 检索 mysql 行

标签 php mysql

我一直在尝试从 mysql 数据库检索一行,但没有成功。我检索的行只有一行。下面是我的代码:

    <?php

$email = $_POST["email"];
$usrname = $_POST["username"];
$pass = $_POST["password"];
$code = $_POST["code"];
$status = $_POST["status"];
$userinfo = $_POST["user_info_id"];
$firstname = $_POST["fname"];
$middlename = $_POST["mname"];
$lastname = $_POST["lname"];
$img = $_POST["image"];

require "init.php";


$query = "select * from user where email like '".$email."';";
$result = mysqli_query($con,$query);

if (mysqli_num_rows($result)>0)
{
    $response = array();
    $code = "reg_false";
    $message = "User already exist";
    array_push($response,array("code"=>$code,"message"=>$message));
    echo json_encode(array("server_response"=>$response));
}
else
{
    $query = "insert into user (email,username,password,code,status,user_info_id) values('".$email."','".$usrname."','".$pass."','".$code."','".$status."','".$userinfo."');";
    //$query2 = "insert into userinfo (fname,mname,lname) values('".$firstname."','".$middlename."','".$lastname."');";
    $result = mysqli_query($con,$query);

    if(!$result)
    {
    $response = array();
    $code = "reg_false";
    $message = "Error try again";
    array_push($response,array("code"=>$code,"message"=>$message));
    echo json_encode(array("server_response"=>$response));
    }
    else
    {
    $query = "select user_info_id from user group by user_info_id DESC limit 1";
    $result = mysqli_query($con, $query);
    if ($mysqli_num_rows($result)>0){

        while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["user_info_id"]. " - Name: " . $row["fname"]. " " . $row["lname"]. $row["mname"] .  "<br>"; }


    }
    $query1 = "insert into userinfo (user_info_id,fname,mname,lname) values('".$result."','".$firstname."','".$middlename."','".$lastname."','".$img."');";
    $result = mysqli_query($con,$query1);

    $response = array();
    $code = "reg_false";
    $message = "Success";
    array_push($response,array("code"=>$code,"message"=>$message));
    echo json_encode(array("server_response"=>$response));
    }
}

mysqli_close($con);





?>

我遇到的错误是:

     Notice: Undefined variable: mysqli_num_rows in     C:\xampp\htdocs\loginapp\register.php on line 46

Fatal error: Uncaught Error: Function name must be a string in C:\xampp\htdocs\loginapp\register.php:46 Stack trace: #0 {main} thrown in C:\xampp\htdocs\loginapp\register.php on line 46

我对 php 和 mysql 相当陌生。

最佳答案

请使用此查询获取 num 行。

$query = "select * from user where email like '".$email."';";
$result = mysqli_query($con,$query);
$row_count = mysqli_num_rows($result);

然后使用这样的 if 条件

if ($row_count>0)
{
    $response = array();
    $code = "reg_false";
    $message = "User already exist";
    array_push($response,array("code"=>$code,"message"=>$message));
    echo json_encode(array("server_response"=>$response));
}
else
{
.......

关于php - 使用 php 检索 mysql 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40463035/

相关文章:

php - Symfony 2 安全总是返回 Bad Credentials 错误

php - 处理 : libapache2-mod-php5 php5 php5-cli 时遇到错误

php - MySQL价格字段插入$和逗号

mysql - 以下删除查询我做错了什么?

mysql - 约束以防止在第三个表中违反 FK 约束

php - LIKE MySQL 查询的问题

php - AsyncTask Android 中的 Http 请求

php - magento 设置属性的值

php - 如何从txt文件导入mysql空值

sql - 问题 : mysql Subqueries