Jump to content

Problems with a simple recordset


selliottsxm

Recommended Posts

Hi,

 

I'm very new to php and to mysql and I was hoping someone would be kind enough to give me a hand.  I'm trying to write a query that returns only one record set. 

I'll paste what I have below and the error.  I'm sure that I am doing something wrong that is right in front of my face but for the life of me I can't figure it out.

 

The relevant part of the code is:

 

require_once ('../mysqli_connect.php');
$movie_id = $_GET['movie_id'];
$q = "SELECT * FROM movies WHERE movie_id = $movie_id";
$r = mysqli_query($dbc, $q);


echo
'<table cols="2" width="1100px" align="center">
<tr>
<td align="left" width="240px"><img src="img/boxcovers/small/movie_' . $r['movie_id'] .'_small.jpg"></td>
<td></td>
</tr>

 

The error I'm getting is:

 

PHP Fatal error:  Cannot use object of type mysqli_result as array

 

Help! :)

Link to comment
Share on other sites

You have left yourself wide open for SQL injection attacks. 

 

You should never trust any user input. Before using any input in your query you should at least validate and sanitize it

require_once ('../mysqli_connect.php');

// make sure movie_id request param exist and that is a number 
if(isset($_GET['movie_id']) && ctype_digit($_GET['movie_id']))    // validate
{
    // cast the value of move_id to a integer
    $movie_id = intval($_GET['movie_id']);                        // sanitize

    $q = mysqli_query($dbc,"SELECT * FROM movies WHERE movie_id = $movie_id");
    $r = mysqli_fetch_array($q);
}

Or a better alternative would be to use prepared statements

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.