Can not get MySql output with PHP [duplicate]

2019-08-13 01:22发布

I'm new to PHP and MySql. I am trying to connect to MySql database and display its contents on screen. But nothing is displaying out. Though I am able to get "congrats" message for connection, I am not able to get anything else. Please help me out to figure out what is the problem with my code.

<?php
    $dbhost= "localhost";
    $dbuser= "root";
    $dbpass= "";
    $dbconnect= mysqli_connect('$dbhost', '$dbuser', '$dbpass','$mydb');
    /*mysqli_select_db($mydb);*/
        if(!mysqli_select_db){
            echo "database not found";
            die(mysqli_error());
        }
        else{
            echo "congrats";
        }


    $query = "SELECT * FROM userinfo";  
    $result = mysqli_query($dbconnect, $query);

    while($record = mysqli_fetch_array($result)){
        echo $record['Name'], $record['Email'], $record['Contact'];
    }

?>

标签: php mysql mysqli
1条回答
你好瞎i
2楼-- · 2019-08-13 01:26

you have not defined the value of $mydb variable. Also made few other corrections. Try to execute below code.

<?php
    $dbhost= "localhost";
    $dbuser= "root";
    $dbpass= "";
    $mydb = "your_dbname";
    $dbconnect= mysqli_connect($dbhost, $dbuser, $dbpass,$mydb);
    /*mysqli_select_db($mydb);*/
        if(!mysqli_select_db){
            echo "database not found";
            die(mysqli_error());
        }
        else{
            echo "congrats";
        }


    $query = "SELECT * FROM userinfo";  
    $result = mysqli_query($dbconnect, $query);

    while($record = mysqli_fetch_array($result)){
        echo $record['Name']." ".$record['Email']." ".$record['Contact'];
    }

?>
查看更多
登录 后发表回答