”废话,但我得到的只是这个错误 Notice: Undefined variable: data in C:\xampp\htdocs\-6ren">
gpt4 book ai didi

php - "Undefined Variable"尝试读取行时

转载 作者:行者123 更新时间:2023-11-29 12:22:35 25 4
gpt4 key购买 nike

我正在尝试读取一堆行并显示它们,而不使用通常的“ARRAY =>”废话,但我得到的只是这个错误

Notice: Undefined variable: data in C:\xampp\htdocs\sql.php on line 0

这是我的代码:

<?php virtual('/Connections/TDBS_local.php');
$query = "SELECT sub_category FROM sub_category WHERE main_category_id = 2 ";
$result = mysqli_query($TDBS_local, $query);
if ($result = mysqli_query($TDBS_local, $query)) {

/* fetch associative array */
if($row = mysqli_fetch_assoc($result)) {
$data = unserialize($row['sub_category']);
}

/* free result set */
mysqli_free_result($result);
}

print "<pre>";
print_r($data);
print "</pre>";
?>

我哪里出错了?

最佳答案

以下是如何使用 pdo、mysqli 和 mysql 来完成此操作

MYSQL

    $con=mysqli_connect("localhost","username","password","dbname");

$sql="SELECT sub_category FROM sub_category WHERE main_category_id = 2";

$result=mysqli_query($con,$sql);

$new_array = array();

while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
$new_array[] = $row;
}
print_r($new_array);

mysqli_free_result($result);

mysqli_close($con);

MYSQL

mysql_connect("localhost","username","password");
mysql_select_db("dbname");

$result = mysql_query("SELECT sub_category FROM sub_category WHERE main_category_id = 2");
$new_array = array();
while ($row = mysql_fetch_array($result)) {
$new_array[] = $row;
}
print_r($new_array);

PDO

$pdo = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');

$statement = $pdo->query("SELECT sub_category FROM sub_category WHERE main_category_id = 2");
$new_array = array();
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$new_array[] = $row;
}
print_r($new_array);

关于php - "Undefined Variable"尝试读取行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28799660/

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