Jixy Jixy - 3 years ago 147
MySQL Question

Issue when displaying results from randomization with Mysql row

So first off, I know this isn't the best method to try and randomize Mysql Rows, but it works for now.

My only issue is that it displays like this:
{"0":"random1","quote":"random1"}
when I want it to display like this
random1

My Code:
`

<?php
$quotes = $DBcon->query(
'SELECT quote FROM quotes ORDER BY RAND() LIMIT 1;');
$result = $quotes->fetch_array();
?> <strong>Daily Quote</strong> - <?php echo json_encode($result); ?>
<input type="submit" class="btn btn-primary" value="Get Quote" />
</div>`


*No that is not my only PHP code, but that's the only section for this issue. Also, if I did something wrong, please correct me as I'm still learning.

*

Answer Source

change the fetch array return two array one with numeric index and other with string index so use fetch_assoc()

$result   = $quotes->fetch_array();

to

$result   = $quotes->fetch_assoc();

to display result

$result['quote']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download