Jump to content

Archived

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

witt

Problem with accessing data from database

Recommended Posts

Let's call this page data.php
There's a link to this page that sends it the required variables. Like so:
site.com/data.php?user_id=1&data=hello

[code]
<?php

$user_id = $_GET['user_id'];
$data = $_GET['data'];

require_once ('includes/database_connect.php');

$query = "SELECT data_id FROM database WHERE data=$data AND user_id=$user_id";        
$result = @mysql_query ($query);

if (mysql_num_rows($result) == 1) {
        
        $row = mysql_fetch_array ($result, MYSQL_NUM);
        
                 echo $row[0];
    
    }

?>
[/code]

The expected result would be just the data_id for hello. The error received:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/data.php on line 11

When the query run is :
[code]
$query = "SELECT data_id FROM database WHERE user_id=$user_id";
[/code]

Everything works fine.

When the query run is :
[code]
$query = "SELECT data_id FROM database WHERE data=$data";
[/code]

The same error is displayed.

This is what the table looks like:

[code]
Table name = database

data_id     user_id       data               data_date
1           1            hello           2006-05-30 18:20:07
[/code]

Share this post


Link to post
Share on other sites
you need quote around values like this:

$query = "SELECT data_id FROM database WHERE data='$data' AND user_id='$user_id'";

when query is wrong, mysql_query returned a null, and this causing other function to spit out error.

Share this post


Link to post
Share on other sites
You need quotes around your data. Also, dont use the error supressor (@), learn to catch errors yourself.
[code]
<?php

$user_id = $_GET['user_id'];
$data = $_GET['data'];

require_once ('includes/database_connect.php');

$query = "SELECT data_id FROM database WHERE data='$data' AND user_id=$user_id";        
if ($result = mysql_query ($query)) {
  if (mysql_num_rows($result) == 1) {
    $row = mysql_fetch_array ($result, MYSQL_NUM);
    echo $row[0];
  }
} else {
  echo mysql_error();
}
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.