无法在PHP中连接到MySQLi? [英] Cannot connect to MySQLi in PHP?

查看:126
本文介绍了无法在PHP中连接到MySQLi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-固定-

我解决了我遇到的问题.我不是100%地确定它是如何修复的,但是我认为这是因为我将连接端口更改为3306,然后使用localhost而不是我的ip进行了连接.感谢您抽出宝贵时间为大家提供帮助,每个人都非常有帮助.

I fixed the issue that I was having. I'm not 100% sure how it was fixed, but I think it was because I changed the connection port to 3306 then connected using localhost rather than my ip. Thank you for everyone that took their time to help, you all were very helpful.

我遇到了一个确实令人沮丧的问题,过去四个小时来,我一直在努力解决这个问题.我无法通过我的PHP代码连接到MySQLi.我检查了用户名,密码,数据库和主机是否正确.这是我的代码:

I'm having a really frustrating issue that I've been trying to fix for (literally) the past 4 hours. I cannot connect to MySQLi through my php code. I checked and the username, password, database, and host are correct. Here's my code:

$mysqli = new mysqli("104.236.***.57", "Josh", "********", "******");

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

所有这些操作就是echo Connected.查询不返回任何内容(就像一个空字符串).当我尝试回显$ connection时,它会执行相同的操作.我认为这是连接的问题,因为它是空的,但是你们最清楚(我是MySQL的新手).我已经研究和尝试了4个多小时的东西,但没有任何效果.

All this does is echo Connected. The query doesn't return anything (it's like an empty string). When I try to echo $connection, it does the same. I think it's an issue with the connection because it was empty but you guys know best (I'm new to MySQL). I've researched and tried stuff for 4+ hours and nothing has worked.

非常感谢您,

乔什

我已经更新了代码.现在,它返回错误:连接被拒绝(mysql错误).

edit: I've updated the code. Now it returns the error: Connection refused (mysql error).

我想我已经解决了连接问题,但是我遇到了一个新问题,我认为这可能与另一个问题有关.我在这里有此代码:

I think I have fixed the connection issue, but I've ran into a new issue which I think may be related to the other issue. I have this code here:

$servername = "104.***.***.57";
$username = "Josh";
$password = "*******";
$dbname = "*****";

$conn = new mysqli($servername, $username, $password, $dbname, '/var/run/mysqld/mysqld.sock');
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"];
    }
} else {
    echo "0 results";
}
$conn->close();

它似乎可行,除了它在有1时打印'0结果'.

And it seems to work, except for that it prints '0 results' when there is 1.

推荐答案

<?php   
$servername = "localhost";  //replace your servername
$username = "root";   //replace your username
$password = "";        //replace your password
$dbname = "test";    //replace your database name

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    echo ' not connected';
}
else
    echo 'connected';
?>

执行此操作,然后告诉我输出.

execute this and tell me the output.

这篇关于无法在PHP中连接到MySQLi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆