Question

I'm trying to print data from mysql but get these error :

Warning: mysql_query() expects parameter 1 to be string, resource given in ..

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in ..

TCPDF ERROR: Some data has already been output, can't send PDF file

I have learn from these following links but still get warning:

This is the code:

    $con=mysql_connect('localhost','root','','bkd_rev');
    $sql = 'select * from tbl';                       
    $result = mysql_query($con,$sql);

    if($result === FALSE) {
        die(mysql_error());
    }

    while($row = mysql_fetch_array($result))
    {
                    $id       = $row['id'];
                    $nam      = $row['name'];                       
        $tbl    .= '<tr> 
                <td>'.$id.'</td><td>'.$nam.'</td><td>
                </tr>';
    }
Was it helpful?

Solution

The right syntax for mysql_query is the opposite of your. From documentation

mixed mysql_query ( string $query [, resource $link_identifier = NULL ] )

So you need to change to

$result = mysql_query($sql,$con);

Since connection link is not required if you only use a database connection you migh not use it

$result = mysql_query($sql);

You might need to select your database after connection

bool mysql_select_db ( string $database_name [, resource $link_identifier = NULL ] )

As side note i would advise you to switch to either PDO or mysqli since mysql_* api are deprecated and soon will be no longer mantained

OTHER TIPS

STOP USING mysql_ METHODS THEY ARE DEPRECATED AND VERY UNSAFE.

Instead use mysqli_, you should learn how to use this rather than trying to continue to learn mysql_ methods.

See Here: http://uk1.php.net/mysqli

the first parameter should be the query it suppose t

$result = mysql_query($sql,$con);

try this...

$con=mysql_connect('localhost','root','');
mysql_select_db('bkd_rev',$con);
$sql = 'select * from tbl';                       
$result = mysql_query($sql);

if(!$result) {
    die(mysql_error());
}

while($row = mysql_fetch_array($result))
{
                $id       = $row['id'];
                $nam      = $row['name'];                       
    $tbl    .= '<tr> 
            <td>'.$id.'</td><td>'.$nam.'</td><td>
            </tr>';
}

problem in parameter sequeance in mysql_query().

$con=mysqli_connect('localhost','root','','bkd_rev');
$sql = 'select * from tbl';                       
$result = mysql_query($sql);

if you want to pass connection object then,

$result = mysql_query($sql,$con);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top