Jump to content

Error with php and MySQL


danalvess

Recommended Posts

Hello, i need help!!!

<?php 
    $query = mysql_query("SELECT 'id', 'name', 'url' FROM videos");
while($run = mysql_fetch_array($query)){
$video_id = $run['id'];
$video_name = $run['name'];
$video_url = $run['url'];
?>

Error:Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

Link to comment
https://forums.phpfreaks.com/topic/278675-error-with-php-and-mysql/
Share on other sites

Your query failed because you're using single quote. If a query fails, then $query will be FALSE, but mysql_fetch_array() expects a resource (hence the error). So:

 

1. Don't use single quotes, use backticks.

2. Check that the $query variable is not FALSE before using it.

<?php 

    $query = mysql_query("SELECT `id`, `name`, `url` FROM videos");
    if (!$query) {
        die('Query failed: ' . mysql_error());
    }

    while($run = mysql_fetch_array($query)){

         $video_id = $run['id'];

         $video_name = $run['name'];

         $video_url = $run['url'];
    }

?>

Also, you should look into using mysqli_* or PDO for MySQL access in PHP; the mysql_* extension has been deprecated.

 

 

  On 6/1/2013 at 11:33 PM, Jessica said:

No, it didn't. That's a perfectly valid query. It probably won't produce what OP wants, but it's not going to fail.

Well you learn something new every day. I use PDO and prepared statements, a framework-based ORM through PHP, or use the command line MySQL client and I've never seen or used single quotes in a query before and made a bad assumption. Thanks for straightening me out.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.