PHP变量声明中未运行mySQL语句

PHP变量声明中未运行mySQL语句,php,mysql,Php,Mysql,在下面的代码中,我试图连接到我的数据库,从我的表中提取最大ID,然后使用rand函数生成一个随机数。代码成功地将我连接到数据库,但当我尝试调用最大ID时,它不会返回值 当我尝试回显变量时,它从“file”返回selectmaxid <?php // Connect to the database $dbLink = new mysqli('localhost', 'username', 'password', 'database'); if(mysqli_

在下面的代码中,我试图连接到我的数据库,从我的表中提取最大ID,然后使用rand函数生成一个随机数。代码成功地将我连接到数据库,但当我尝试调用最大ID时,它不会返回值

当我尝试回显变量时,它从“file”返回selectmaxid

<?php

// Connect to the database
        $dbLink = new mysqli('localhost', 'username', 'password', 'database');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error()); }


    $amount = "SELECT MAX(id) FROM 'table'";
    $rannmr = rand(1, $amount);


// Close the mysql connection
    mysqli_close($dbLink);

?>
如能帮助解决此问题,我们将不胜感激

当我尝试回显变量时,它从“file”返回selectmaxid

<?php

// Connect to the database
        $dbLink = new mysqli('localhost', 'username', 'password', 'database');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error()); }


    $amount = "SELECT MAX(id) FROM 'table'";
    $rannmr = rand(1, $amount);


// Close the mysql connection
    mysqli_close($dbLink);

?>
首先,您使用了错误的for FROM“table”作为单引号

如果table确实是该表的名称,请用反勾号将其括起来,您的问题将显示该文件

无论哪种方式,都不能在表名周围使用引号。看起来您正在使用文件作为表名

所以,如果table只是一个示例,它被称为file,我们可以这么说,您可以:

$amount = "SELECT MAX(id) FROM `file`";

然后,您还需要查询,使用您没有使用的查询

$amount = mysqli_query($dbLink,"SELECT MAX(id) FROM `file`");
或面向对象的样式:

$amount = $dbLink->query("SELECT MAX(id) FROM `file`");

if($amount){
    echo "Success!";
}else{
    die('Error : ('. $dbLink->errno .') '. $dbLink->error);
}
参见中的示例1 使用或diemysqli_error$dbLink到mysqli_查询,该查询将发出错误信号

编辑:

尝试以下方法。您可能需要修改$row[0]和rand0,$count as 1,具体取决于列号

$result = $dbLink->query("SELECT MAX(id) FROM mytable")
while ($row=$result->fetch_row()) { $count = $row[0]; }
$random = rand(0,$count);
echo $random;
使用以下命令:


$amount=从表中选择MAXid

您忘记执行MySQL查询:

$amount = $dbLink->query("SELECT MAX(id) FROM table")->fetch_assoc();
$rannmr = rand(1, $amount[0]);

您从未执行过查询,您需要更多的逻辑

if ($result = mysqli_query($dbLink, "SELECT MAX(id) as amount FROM `table`")) {
    printf("Select returned %d rows.\n", mysqli_num_rows($result));
    if ($row = mysqli_fetch_assoc($result)) {
        $amount = $row['amount']; 
        $rannmr = rand(1, $amount);            
    }else{
        echo 'no row found';
    }
}
mysqli_close($dbLink);

我似乎没有看到实际执行查询的代码行:

试试这个:使用面向对象的mysqli方法

<?php

// Connect to the database
    $dbLink = new mysqli('localhost', 'username', 'password', 'database');
    if(mysqli_connect_errno()) {
        die("MySQL connection failed: ". mysqli_connect_error()); }


$amount = "SELECT MAX(id) as max_id FROM 'table'";

// Do the actual query : 
$run_query = $dbLink->mysql->query($amount);
// Retrieve the values:
$result = $run_query->fetch_array(); 
// Do the rand function together with the retrieved value
$rannmr = rand(1, $result['max_id']);

// Now you can echo the variable:
echo $rannmr;



// Close the mysql connection
mysqli_close($dbLink);

?>

谢谢

错误报告返回以下错误rand希望参数2很长,对象给定…@Sangeet为什么不使用MySQL的rand函数?例如,RANDI希望速度优先。@Sangeet重新加载我的答案,并查看脚本echo的0下方的底部。将$row[0]和rand0$count修改为1 echo的通知:未定义的偏移量:1。。和0。
<?php

// Connect to the database
    $dbLink = new mysqli('localhost', 'username', 'password', 'database');
    if(mysqli_connect_errno()) {
        die("MySQL connection failed: ". mysqli_connect_error()); }


$amount = "SELECT MAX(id) as max_id FROM 'table'";

// Do the actual query : 
$run_query = $dbLink->mysql->query($amount);
// Retrieve the values:
$result = $run_query->fetch_array(); 
// Do the rand function together with the retrieved value
$rannmr = rand(1, $result['max_id']);

// Now you can echo the variable:
echo $rannmr;



// Close the mysql connection
mysqli_close($dbLink);

?>