0

I'm trying to mastery php by reading book, and i'm stuck on this code :

$title_db_query = "SELECT zagolovok FROM Title WHERE id = 5"; 
$title_query = mysql_query ($title_db_query);  
while($row_title = mysql_fetch_array($title_query))
{  
    echo $row_title['zagolovok']; 
}   

This code supposed to print data from 'zagolovok', but instead it gives nothing. I tried to use var_dump($title_query) and it says bool(false).

I have connection to db, because other queries are work fine.

4
  • Enable error reporting. If $title_query returns false: you have an error. Read more here: nl3.php.net/manual/en/function.mysql-query.php You might also consider using mysqli_* instead of mysql. Commented Aug 16, 2012 at 8:18
  • See the big, pink box near the top of php.net/mysql_query ? You might want to pay attention to that. Commented Aug 16, 2012 at 8:19
  • @Quentin That's a good idea, but I don't think that will solve his problem. Commented Aug 16, 2012 at 8:20
  • 1
    Test you query string SELECT zagolovok FROM Title WHERE id = 5 directly in your database to ensure that data is returned. Commented Aug 16, 2012 at 8:21

2 Answers 2

1

Try adding or die(mysql_error()) to query and see what happens

$title_query = mysql_query($title_db_query) or die(mysql_error());
Sign up to request clarification or add additional context in comments.

Comments

0
$title_db_query = "SELECT zagolovok FROM Title WHERE id = 5"; 

This query selects the field zagolovok from the table Title, meaning that you have a table Title which has a column named zagolovok. It selects only one row since the id is probably an unique index. So it will select from the row with id 5 the data from the column zagolovok.

$title_query = mysql_query ($title_db_query); 

This will send the query to the database and the returned result is saved in the variable $title_query. If the query wasn't successful it will return false.

while($row_title = mysql_fetch_array($title_query))
{  
    echo $row_title['zagolovok']; 
}

This you should use like this : mysql_fetch_array($title_query, MYSQL_ASSOC). It will return an associative array (the key of the array will be the name of the column) or mysql_fetch_array($result, MYSQL_NUM) will return an array with numeric keys.

Also I recommend you to use mysqli instead of mysql or PDO if you're quite familiar with objects. From what I know PDO performs fastest. Good luck.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.