migroo Posted July 29, 2010 Share Posted July 29, 2010 I have bean playing around with data tables and I found this function, mysql_fetch_array(). I want to use it to put all of the data into an array so that I can get at it easier but it just isn't working for me.I am getting this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/gravel36/public_html/upload_file.php on line 30I think this means that the data I put in the function's parameters is the wrong type of data, not a mysql_result. Here is my code:$local_host = 'localhost';$user_name = 'my_username';$password = 'my_password';$db = 'db_name';$dt = 'dt_name';$display = '';$connect = mysql_connect($local_host, $user_name, $password); if(!$connect){ die('Could not connect: ' . mysql_error()); }mysql_select_db($db, $connect);$query = 'SELECT * FROM $dt ORDER BY $by ASC';$query_result = mysql_query($query, $connect);print_r(mysql_fetch_array($query_result));echo $display;?> I hope someone here knows what I am doing wrong. Link to comment Share on other sites More sharing options...
boen_robot Posted July 29, 2010 Share Posted July 29, 2010 mysql_fetch_array() fetches the next row of the result set as an array (with each field being an array member), and returns false when there are no more rows. That's not exactly what you want, but it's actually better.As for the acual problem... for some reason, the query failed. mysql_query() returned false, and you never checked for that. Why did it failed? Maybe (just maybe) because there's no value for the $by variable in the query. Link to comment Share on other sites More sharing options...
migroo Posted July 29, 2010 Author Share Posted July 29, 2010 Well you are mostly right...It is my sql statement that is causing the problem.The problem though is it won't treat the variables I put in the sql statement as variables.Here is my new code: $local_host = 'localhost';$user_name = 'my_username';$password = 'my_password';$db = 'db_name';$dt = 'dt_name';$display = '';$connect = mysql_connect($local_host, $user_name, $password); if(!$connect){ die('Could not connect: ' . mysql_error()); }mysql_select_db($db, $connect);$query = 'SELECT * FROM $dt ORDER BY ID ASC';$query_result = mysql_query($query, $connect) or die(mysql_error());print_r(mysql_fetch_array($query_result));echo $display; And here is my new error: Table 'my_db_name.$dt' doesn't existNow if I put the information that I have in the variables strait into the sql statement it works fine.Any Idea how to make it realize that those are variables not text?Thanks for the help I am lost on this one. Link to comment Share on other sites More sharing options...
boen_robot Posted July 29, 2010 Share Posted July 29, 2010 Oh, that... change your single quotes (around the query) to double quotes. Double quotes enable variable interpolation, which is essentially the very thing you want to do. Link to comment Share on other sites More sharing options...
migroo Posted July 29, 2010 Author Share Posted July 29, 2010 Wow, THANK YOU! It works great now. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.