2

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.

*

1
  • Don't echo json_encode unless you want json formatted text inside your html tags. Commented Sep 30, 2017 at 4:49

1 Answer 1

1

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']
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, That fixed the problem of it displaying twice, but why does it appear like this: {"quote":"quote5"} instead of quote5.
Now I keep getting: Notice: Undefined variable: result in C:\xampp\htdocs\quotes\quote.php on line 87 Account Info - Notice: Undefined variable: result in C:\xampp\htdocs\quotes\quote.php on line 88 null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.