witt Posted May 31, 2006 Share Posted May 31, 2006 Let's call this page data.phpThere'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 11When 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 = databasedata_id user_id data data_date1 1 hello 2006-05-30 18:20:07[/code] Link to comment https://forums.phpfreaks.com/topic/10808-problem-with-accessing-data-from-database/ Share on other sites More sharing options...
hvle Posted May 31, 2006 Share Posted May 31, 2006 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 https://forums.phpfreaks.com/topic/10808-problem-with-accessing-data-from-database/#findComment-40402 Share on other sites More sharing options...
trq Posted May 31, 2006 Share Posted May 31, 2006 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 https://forums.phpfreaks.com/topic/10808-problem-with-accessing-data-from-database/#findComment-40403 Share on other sites More sharing options...
witt Posted May 31, 2006 Author Share Posted May 31, 2006 Ah of course...strings need quotes. Link to comment https://forums.phpfreaks.com/topic/10808-problem-with-accessing-data-from-database/#findComment-40406 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.