3

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

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

在 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 文件。这会有帮助吗??我没有尝试这个,因为我的托管服务提供商没有给我访问该目录的权限(因为我使用免费托管来测试脚本)或者我根本没有找到它:(

请帮忙....

4

2 回答 2

4

前言: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']);
}
于 2012-12-11T08:50:47.763 回答
1

首先:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

你的 mysql_error() 是什么?:)

于 2012-12-11T08:53:32.133 回答