PHP error please find out mistake

Discussion in 'PHP' started by vic_msn, Jul 15, 2006.

  1. #1
    
    $sql=mysql_query("SELECT * FROM tags WHERE name LIMIT 0 , 30");
    $row=mysql_fetch_array($sql);
    Test <? echo $row ?>
    
    PHP:
    if i use this error i get the following error any reason or syntax error
     
    vic_msn, Jul 15, 2006 IP
  2. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #2
    It appears as if you haven't established a connection to the database. Also, since mysql_fetch_array() returns an array you can't echo it.

    
    <?
    $dbhost = "localhost";
    $dbuser = "username"; //replace with a valid username
    $dbpass = "password"; //replace with a valid password
    $dbname = "database" //replace with the name of a database you have already created
    
    mysql_connect($dbhost,$dbuser,$dbpass);
    mysql_select_db($dbname);
    
    $sql=mysql_query("SELECT * FROM tags WHERE name=Daniel LIMIT 0 , 30");
    
    while($row=mysql_fetch_array($sql)){
          
    echo $row[0]." ".$row[1]." ".$row[2]; //you replace this with what you want to do with the data
    }
    ?>
    
    Code (markup):
     
    danielbruzual, Jul 15, 2006 IP
  3. mariush

    mariush Peon

    Messages:
    562
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $sql=mysql_query("SELECT * FROM tags WHERE name LIMIT 0 , 30");

    must be where name=something

    Your SQL query is incorrect.
     
    mariush, Jul 15, 2006 IP
  4. vic_msn

    vic_msn Well-Known Member

    Messages:
    2,233
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    155
    #4
    previously i had closed the mysql connection before using fetch_array and query
    no i get this error
    $sql=mysql_query("SELECT * FROM tags WHERE name LIMIT 0 , 30");
    while($row=mysql_fetch_array($sql))
    {   
    echo $row[0]; //you replace this with what you want to do with the data
    }
    ?> 
    
    PHP:
     
    vic_msn, Jul 15, 2006 IP
  5. Nick_Mayhem

    Nick_Mayhem Notable Member

    Messages:
    3,486
    Likes Received:
    338
    Best Answers:
    0
    Trophy Points:
    290
    #5
    <?php
    function dbconnect()
    {
    $link = mysql_connect( $host, $user, $pass );
    if (! $link )
    	die("Couldn't connect to MYSQL");
    mysql_select_db( $database, $link)
    	or die("Couldn't open $database: ".mysql_error() );
    }
    ?>
    PHP:
    Just call this function whenever you need to connect. And yeah don't forget to edit the variables with your database settings.
     
    Nick_Mayhem, Jul 15, 2006 IP
  6. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #6
    I did not notice, when you use WHERE you must use an equal (=):

    SELECT * FROM tags WHERE name=debt LIMIT 0,30 // which would return all the rows in which the name is debt.

    SELECT * FROM tags WHERE 1=1 LIMIT 0,30 //would return every row in the table.
     
    danielbruzual, Jul 15, 2006 IP
  7. vic_msn

    vic_msn Well-Known Member

    Messages:
    2,233
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    155
    #7
    i still get this error is there a solution please
     
    vic_msn, Jul 15, 2006 IP
  8. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #8
    Try this

    $sql=mysql_query("SELECT * FROM tags WHERE name='vic_msn' LIMIT 0 , 30")
    
    Code (markup):
     
    YIAM, Jul 15, 2006 IP