$cg= $mysqli->query("SELECT * FROM news WHERE id='$postid'
and status='1' ORDER BY RAND()");
while($sd= $cg->fetch_assoc()){
$title= $sd['title'];
}
我想要做的是将 var $title 转换为类似
$mytitle[1] = 'first title';
$mytitle[2] = 'second title';
$mytitle[3] = 'third title';
$randnews= rand(1, 3);
echo $mytitle[$randnews];
最佳答案
试试这个,我已经根据我的数据库表查询进行了尝试, 它正在按您的预期工作。
代码:-
<?php
require_once("connect.php");
$sql=("SELECT * FROM news WHERE id='$postid' and status='1' ORDER BY RAND()");
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
$array[] = $row['title'];
}
echo "After Sorting".'<pre>';
print_r($array);
echo '</pre>';
?>
$con是连接类的变量。
$con=mysqli_connect($servername,$username,$password,$my_db);
输出:-
[xyz] => "test" [abc] => "tester" [pqr] => "user" [res] => "test"
希望这有帮助
关于php - 如何将mysql数组转为php数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29559211/