Search code examples
phpmysqlmysqlirowsmysql-num-rows

mysql_num_rows() Error


<?php
$id=$_GET['id'];
$username="xxx";
$password="xxx";
$database="xxx";
$host="xxx";
mysql_connect($host,$username,$password);
$con = mysql_connect("$host","$username","$password");

$id=$_POST['ID'];
$query="SELECT * FROM vbots WHERE ID=$id";
$result = mysql_query("SELECT * FROM vbots");
$num=mysql_query($result,$con) or die("Error: ". mysql_error(). " with query ". $query);
mysql_close();

I keep getting

Error: Query was empty with query SELECT * FROM vbots WHERE ID=1"

How can i fix that? I get it from mysql_num_rows() , wrote die("Error: ". mysql_error(). " with query ". $query); for more info .

Thank you !


Solution

  • You mixed something up with your query.

    $result = mysql_query("SELECT * FROM vbots");
    $num=mysql_query($result,$con); //! $result is already a result from a query. 
    //You can't 'query a result'…
    

    So this should just work:

    $id=$_GET['id'];
    $username="xxx";
    $password="xxx";
    $database="xxx";
    $host="xxx";
    $con = mysql_connect($host,$username,$password);
    if (!mysql_select_db($database))
        die("Can't select database");
    
    $id=$_POST['ID'];
    $query="SELECT * FROM vbots WHERE ID=$id";
    $result = mysql_query($query);
    echo mysql_num_rows($result);
    mysql_close();