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
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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.