gpt4 book ai didi

php - 我的 php 脚本没有使用给定的用户名/密码/主机,而是使用 root@localhost(密码 : NO)

转载 作者:可可西里 更新时间:2023-11-01 07:59:26 28 4
gpt4 key购买 nike

有问题!虽然我发现几乎相似的线程但没有帮助:(

我编写了一个 php 脚本来从我的 MySQL 数据库中获取注册用户的数量。该脚本在我的本地主机上运行良好;它使用给定的用户名、密码和主机名,分别是“root”、“root”和“localhost”,但脚本没有使用给定的用户名/密码/主机,而是使用 root@localhost(密码:NO)在实时服务器中。

在 Live 服务器中我创建了一个 MySQL 用户,设置了不同的密码和主机名当然不是 localhost。我用新创建的 mysql 用户数据更新了脚本。但是,每当我运行脚本时,我都会看到脚本仍在使用“root”、“root”和“localhost”!!

看一下脚本:

    //database connection
$conn = mysql_connect( "mysql.examplehost.com", "myusername", "mypass" );
$db = mysql_select_db ("regdb",$conn); //Oops, actually it was written this way in the script. I misstyped it previously. now edited as it is in the script.

//Query to fetch data
$query = mysql_query("SELECT * FROM regd ");
while ($row = mysql_fetch_array($query)):
$total_regd = $row['total_regd'];
endwhile;

echo $total_regd;

-- 有人说要更改默认用户名并传入位于 phpMyAdmin 目录中的 config.ini.php 文件。这会有帮助吗??我没有尝试这样做,因为我的托管服务提供商没有给我访问该目录的权限(因为我正在使用免费托管来测试脚本)或者我根本没有找到它:(

请帮忙....

最佳答案

前言:MySQL 扩展已被标记为弃用,最好使用 mysqli 或 PDO


虽然您将连接资源存储在 $conn 中,但您并没有在调用 mysql_query() 时使用它,也没有检查 mysql_connect() 的返回值,即如果连接由于某种原因 mysql_query() 失败“免费”以建立新的默认连接。

<?php
//database connection
$conn = mysql_connect( "mysql.examplehost.com", "myusername", "mypass" );
if ( !$conn ) {
die(mysql_error()); // or a more sophisticated error handling....
}

$db = mysql_select_db ("regdb", $conn);
if ( !$db ) {
die(mysql_error($conn)); // or a more sophisticated error handling....
}

//Query to fetch data
$query = mysql_query("SELECT * FROM regd ", $conn);
if (!$query) {
die(mysql_error($conn)); // or a more sophisticated error handling....
}

while ( false!=($row=mysql_fetch_array($query)) ):
$total_regd = $row['total_regd'];
endwhile;

echo $total_regd;


编辑:看起来你只处理了一行。
要么将 echo 行移动到 while 循环中,要么(如果你真的只想要一条记录)最好在 sql 语句中这样说并摆脱循环,例如

// Query to fetch data
// make it "easier" for the MySQL server by limiting the result set to one record
$query = mysql_query("SELECT * FROM regd LIMIT 1", $conn);
if (!$query) {
die(mysql_error($conn)); // or a more sophisticated error handling....
}

// fetch data and output
$row=mysql_fetch_array($query);
if ( !$row ) {
echo 'no record found';
}
else {
echo htmlspecialchars($row['total_regd']);
}

关于php - 我的 php 脚本没有使用给定的用户名/密码/主机,而是使用 root@localhost(密码 : NO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13816580/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com