我的php代码没有连接到mysql数据库表。我的代码有什么问题?

时间:2015-12-26 15:12:53

标签: php

我希望我的php文件名为getData.php,以便从mysql数据库表中获取数据并且它无法正常工作?

$mysql_host='localhost';
$mysql_user='username';
$mysql_password='password';

@mysqli_connect($mysql_host,$mysql_user,$mysql_password);
@mysqli_select_db("records");

if(!@mysqli_connect($mysql_host,$mysql_user,$mysql_password) )
{
die("CanNot connect to database");  
}
else
{
if(!@mysqli_select_db("student"))
{
echo 'connection successful';
}
else{
die ("CanNot connect to database"); 
}
}
echo "<br />";
$query="SELECT * FROM `info`";
if(@$is_query_run=mysql_query($query))
{
echo"query executed";
while($query_execute=mysql_fetch_assoc($is_query_run))
{
echo $query_execute["Name"];
}
}
else
{
echo "query not executed";
}

?>

我的数据库的名称是$ records,目标表名为$ info,代码连接到数据库,但无法连接到$ student数据库中的表名$ info。

3 个答案:

答案 0 :(得分:0)

小错误交换死亡和数据库选择中的回声。

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode)
    {
        case REQUEST_WRITE_STORAGE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                //reload my activity with permission granted or use the features what required the permission
            } else
            {
                Toast.makeText(parentActivity, "The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
            }
        }
    }    
}

答案 1 :(得分:0)

您应该使用MySQLi或PDO。

<?php

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}

if (!mysql_select_db("student")) {
    echo "Unable to select student: " . mysql_error();
    exit;
}

$sql = "SELECT * FROM info";

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

while ($row = mysql_fetch_assoc($result)) {
    echo $row["Name"];
}

mysql_free_result($result);

?>

更多信息和样本:http://php.net/manual/en/function.mysql-fetch-assoc.php

答案 2 :(得分:-1)

以下代码解决了我的问题。

$mysql_host='localhost';
$mysql_user='username';
$mysql_password='password';
$link = mysqli_connect($mysql_host,$mysql_user,$mysql_password);


if(mysqli_connect_errno())
{
    die("CanNot connect to database");  
}
else
{
    if(!(mysqli_select_db($link, "student")))
    {
        die('cannot connect to the database');
    }
    else
    {
        echo "connected"; 
    }
}
echo "<br />";
$query="SELECT * FROM `info`";
if($is_query_run=mysql_query($link,$query))
{
    echo"query executed";
    while($query_execute=mysql_fetch_assoc($is_query_run))
    {
        echo $query_execute["Name"];
    }
}
else
{
     echo "query not executed";
}

感谢代码@ v-sugumar