gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:53:26 24 4
gpt4 key购买 nike

我正在尝试使用以下代码从WooCommerce Wordpress订单获取信息,然后将详细信息传递给数据库。应该执行此操作的代码如下:

//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


执行此代码后,它将填充数据库,然后另一个帮助PHP将从数据库中获取信息。但是我有两个问题。第一个问题是,我认为我没有正确创建数据库。我可以成功连接,但是下订单后,信息不会存储在数据库中。为了创建数据库,我只是做了一个简单的
     CREATE DATABASE MYDATABASE;并创建数据库。我假设执行此代码后,表和行将自行创建。如果我错了,请纠正我。我的第二个问题是当我运行以下帮助程序代码时:

<?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);
?>


我收到以下错误


  PHP警告:mysqli_num_rows()期望参数1为mysqli_result,在/link.php的第25行中给出布尔值


我什至不知道这意味着什么,更不用说如何解决它了。

我对PHP和MySQL都还很陌生,因此正确方向的任何指针都将非常有帮助。

最佳答案

您的mysqli_query()返回FALSE,因为查询失败。您应在调用mysqli_num_rows()之前检查查询是否成功执行。

关于php - PHP警告:mysqli_num_rows()和数据库创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22676989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com