1

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>';
    }
Community
  • 1
  • 1
andrian
  • 423
  • 1
  • 8
  • 15

5 Answers5

3

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

Fabio
  • 23,183
  • 12
  • 55
  • 64
  • how about this? https://ideone.com/UK1npN but Noticed and Warning appears for the tcpdf file itself. – andrian Mar 06 '14 at 13:58
  • @adrian what should i see there? This seems to be another error not related to your actual question. If the answer was correct you should mark this answer as correct. If you have a new question after this problem was solved you should ask a new question with your new scenario. – Fabio Mar 06 '14 at 14:19
  • sorry I'm being crazy with these code. but your advice with `mysqli_*` was really helpful, thanks it works now. – andrian Mar 06 '14 at 14:25
2

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

RaggaMuffin-420
  • 1,762
  • 1
  • 10
  • 14
0

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);
i'm PosSible
  • 1,373
  • 2
  • 11
  • 30
0

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>';
}
Nishant Solanki
  • 2,119
  • 3
  • 19
  • 32
0

the first parameter should be the query it suppose t

$result = mysql_query($sql,$con);
moh kaw
  • 43
  • 5