duanchun1881 2014-03-27 02:12
浏览 57
已采纳

PHP警告:mysqli_num_rows()和数据库创建

I am trying to use the following code to get information from a WooCommerce Wordpress Order, then pass the details to a database. The code that is supposed to execute this action is as follows:

//Pass udid to database
add_action('woocommerce_payment_complete', 'send_to_db');

function send_to_db( $order_id ) {
$order = new WC_Order( $order_id ); 
$udid = $order->customer_note;
$email = $order->billing_email;
$db_hostname = 'MYIP';
$db_database = 'MYDATABASE';
$db_username = 'MYUSER';
$db_password = 'MYPASSWORD';

// Connect to server.
$link = mysqli_connect($db_hostname, $db_username, $db_password);
if (!$link) {
die('Not connected : ' . mysqli_error());
}

// Select the database. 
$db_selected = mysqli_select_db($link, $db_database);
if (!$db_selected) {
die ('Can\'t use database : ' . mysqli_error());
}
//find package type
$items = $order->get_items();
foreach($items as $item) {
$type = '1';
if(preg_match("/Basic/i", implode($item))) {
        $type = '0';
    }

}
//put it into the db
$query= "INSERT INTO udid_orders (udid, email, type) VALUES ('$udid','$email','$type')";
$sql = mysqli_query($link, $query);
}
mysqli_close($link);
//end pass to db

After this code is executed, it should fill the database then another piece of helper PHP will get the information from the database. However I have two issues. The first issue, is that I don't think I created the database properly. I can successfully connect, but after an order is placed, the information is not stored in the database. To create the database, I just did a simple CREATE DATABASE MYDATABASE; and created the database. I assumed the table and rows would create themselves after this code was executed. Please correct me if I'm wrong. My second issue is when I run the following helper code:

<?php
$x = '1';
//include 'db.php';
$db_hostname = 'MYIP';
$db_database = 'MYDATABASE';
$db_username = 'MYUSERNAME';
$db_password = 'MYPASSWORD';

// Connect to server.
$link = mysqli_connect($db_hostname, $db_username, $db_password);
if (!$link) {
    die('Not connected : ' . mysqli_error());
}

// Select the database. 
$db_selected = mysqli_select_db($link, $db_database);
if (!$db_selected) {
    die ('Can\'t use database : ' . mysqli_error());
}

//get udid from database
while($x = '1'){
$query = "SELECT id, udid, email, premium FROM udid_orders ORDER BY timestamp DESC LIMIT 1";
$sql = mysqli_query($link, $query);
if(mysqli_num_rows($sql) >= 1){
    echo 'registering';
    $row = mysqli_fetch_array($sql);
    $number = $row['id'];
    $udidsql = $row['udid'];
    $email = $row['email'];
    $is_premium = $row['premium'];
    $service_port = '622';
    $address = 'MYIPHERE';
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ($socket === false) {
        echo 'Failed to create socket';
        return;
    }
    $result = socket_connect($socket, $address, $service_port);
    if ($result === false) {
        echo 'Failed to connect socket';
        return;
    }

    $in = "weTi3xJEy7kbldDYWdXe";
    $out = '';

    socket_write($socket, $in, strlen($in));
        while ($out = socket_read($socket, 1024)) {
        break;
    }

    //get us a random number for the name 
    $request = 'OrderInfo ';
    //$random = rand(1, 2000);
    //name that nigga
    $name = $number;
    $request .= $name;
    $request .= " ";        
    //$request .= $order->billing_email;
    $request .= $email;
    $request .= " ";
    //$is_premium = '1';
    //  if(preg_match("/Bronze/i", implode($item))) {
    //      $is_premium = '0';
    //  }
    //  $request .= $is_premium;
    //}
    $request .= $is_premium;
    $out = '';
    socket_write($socket, $request, strlen($request)); 
    while($out = socket_read($socket, 1024)) {
        break;
    }
    $udids = explode(',', $udidsql);
    foreach($udids as $udid){
        $udid_request = "UdidRegister ";

        $udid = $udidsql;

        $udid_request .= $udid;

        $udid_status = "";
        socket_write($socket, $udid_request, strlen($udid_request));
    }
    socket_close($socket);

    $update = "UPDATE udid_orders SET status='1' WHERE udid='$udidsql'";
    $sql_update = mysqli_query($link, $update);

  }


else{
    echo 'sleeping';
    sleep(5);
    }   

}   
// close mysql
mysqli_close($link);
?>

I get the following error

PHP Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /link.php on line 25

I have no idea what is even means, let alone how to troubleshoot it.

I am pretty new to both PHP and MySQL, so any pointers in the right direction would be really helpful.

  • 写回答

3条回答 默认 最新

  • dousuiben8395 2014-03-27 06:08
    关注

    To resolve this issue, I ended up changing a few lines of code to reflect the following (as per the documentation)

    // Select the database. 
    $db_selected = mysqli_select_db($link, $db_database);
    if (!$db_selected) {
        die ('Can\'t use database : ' . mysqli_error());
    }
        $query = $mysqli->prepare("SELECT * FROM udid_orders");
        $query->execute();
        $query->store_result();
    
        $rows = $query->num_rows;
    
        echo $rows;
    //get udid from database
    while($x = '1'){
    $query = "SELECT id, udid, email, type FROM udid_orders";
    
    $sql = mysqli_query($link, $query);
        if(mysqli_num_rows($sql) >= 1){
        echo 'registering';
        $row = mysqli_fetch_array($sql);
        $number = $row['id'];
        $udidsql = $row['udid'];
        $email = $row['email'];
        $is_premium = $row['type'];
        $service_port = '622';
        $address = '107.170.105.37';
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($socket === false) {
            echo 'Failed to create socket';
            return;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?