Jump to content


Photo

Problem with accessing data from database


  • Please log in to reply
3 replies to this topic

#1 witt

witt
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 31 May 2006 - 12:01 AM

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

<?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];
    
    }

?>

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 :
$query = "SELECT data_id FROM database WHERE user_id=$user_id";

Everything works fine.

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

The same error is displayed.

This is what the table looks like:

Table name = database

data_id     user_id       data               data_date
1           1            hello           2006-05-30 18:20:07


#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 31 May 2006 - 12:26 AM

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.
Life's too short for arguing.

#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 31 May 2006 - 12:30 AM

You need quotes around your data. Also, dont use the error supressor (@), learn to catch errors yourself.
<?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();
}
?>


#4 witt

witt
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 31 May 2006 - 12:35 AM

Ah of course...strings need quotes.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users