0

Trying to show the table but isn't working, what am I doing wrong?

<?php

$query = "SELECT id menu_id menu_title FROM tbl_menu";
$result = mysql_query($query);

if(mysql_num_rows($result) > 0){
    while ($row = mysql_fetch_array($result)){
        echo $row['menu_title'];echo 'test';
    }
}

?>
2

5 Answers 5

1
$query = "SELECT id, menu_id, menu_title FROM tbl_menu";
Sign up to request clarification or add additional context in comments.

Comments

0
$result = mysql_query($query) or trigger_error(mysql_error());

Comments

0

It doesn't look like you are connecting to anything. You also need to separate column names by commas:

SELECT id, menu_id, menu_title FROM tbl_menu

Here's a mysqli_ example from the documentation:

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT id, menu_id, menu_title FROM tbl_menu";

if ($result = mysqli_query($link, $query)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        echo $row[menu_id];
    }

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

/* close connection */
mysqli_close($link);
?>

Comments

0
$query = "SELECT id menu_id menu_title FROM tbl_menu";

Must Be

$query = "SELECT id, menu_id, menu_title FROM tbl_menu";

Your SQL have a typo

Comments

0
$query = "SELECT id, menu_id, menu_title FROM tbl_menu";
$result = mysql_query($query);

if($result && mysql_num_rows($result) > 0){
    while ($row = mysql_fetch_assoc($result)){
        echo $row['menu_title'];
    }
}

To note:

mysql_fetch_array() returns a number-indexed array

mysql_fetch_assoc() returns a string-indexed array (indices are names of your fields)

And please, stop using mysql, it is deprecated. Use mysqli instead.

Comments

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.