Jump to content

Problem with accessing data from database


witt

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]
Link to comment
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.
Link to comment
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]
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.