从mysql中检索一条记录,并将其存储在PHP中的数组中


Retrieving one record from mysql and storing in an array in PHP

我两天前刚开始使用php,因为我需要用php进行编码。请耐心等待,因为我还在通读手册。我想从mysql中检索一个特定的列,并将字符串存储到一个数组中。

mySQL col_1 record: 1, 2, 3
Desired array: ['1', '2', '3']
//mySQL connection
$connect = mysqli_connect("localhost", "root", "","my_db");
//Sample job_id 
$user_id  = 40398;
$sql_statement = "SELECT col_1 FROM table1 WHERE name = " . $user_id . " LIMIT 1";
$result_set = mysqli_query($connect, $sql_statement);
$row = mysqli_fetch_row($result_set);
$terms = explode(',', $row);
print_r $terms;
mysqli_close($connect);

尝试打印以查看数组时出错。我在这里做错了什么?

您需要将数组括在括号中才能使用PHP print_r函数。

更改

print_r $terms;

print_r($terms);

更多信息请参阅手册。