3 Replies - 496 Views - Last Post: 27 May 2010 - 06:31 PM Rate Topic: -----

#1 Glorfindal  Icon User is offline

  • Java Enthusiast
  • member icon

Reputation: 23
  • View blog
  • Posts: 538
  • Joined: 31-December 08

Need help retrieving data with MySQL

Posted 27 May 2010 - 04:50 PM

I am working on making a page take it's code from the database, this way I could later add a admin panel that would let you directly edit the code letting you edit the page. But I am trying to run a query to pull the code out of the database and keep getting an error so can anyone help? This is the error I am getting.

Quote

Warning: mysql_fetch_field(): supplied argument is not a valid MySQL result resource in D:\Hosting\6040598\html\gallerytest.php on line 15
And the password or username isn't wrong I just removed all that information so that no one could see it.

<html><head>
<script type="text/javascript">
// Popup window code
function Popup(url) {
	popupWindow = window.open(
		url,'popUpWindow','width=900,height=550,left=0,top=0,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=no')
}
</script>
<title>Gallery</title><link rel="stylesheet" type="text/css" href="CSS/CSS.css"></head><body bgcolor="#D3D3D3">
<?php 
include("includes/gallery_header.php"); 

$result = mysql_query("SELECT Code FROM Pages WHERE Page='Gallery'",mysql_connect("localhost","ausername","apassword"));

$property = mysql_fetch_field($result);

echo $property;
 
mysql_close(mysql_connect("localhost","ausername","apassword"));
?></body></html>


Is This A Good Question/Topic? 0
  • +

Replies To: Need help retrieving data with MySQL

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,766
  • Joined: 27-December 08

Re: Need help retrieving data with MySQL

Posted 27 May 2010 - 05:03 PM

If your result set doesn't have any rows, then it isn't a valid result set according to PHP. You can test this using the mysql_num_rows($result) function, which will return the number of rows in the result set.

Also, not a good idea to store that much unformatted text in your database. Store the path to the File, and use PHP File I/O tools to edit the File. That is one reason I personally hate computer generated code, like NetBeans GUI Builder code, because it is sloppy, cumbersome and not easy to maintain.
Was This Post Helpful? 0
  • +
  • -

#3 Glorfindal  Icon User is offline

  • Java Enthusiast
  • member icon

Reputation: 23
  • View blog
  • Posts: 538
  • Joined: 31-December 08

Re: Need help retrieving data with MySQL

Posted 27 May 2010 - 05:49 PM

Okay but it is just supposed to just one row so what do I do? Would I use a different function?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Need help retrieving data with MySQL

Posted 27 May 2010 - 06:31 PM

This should be something more like:

$conn = mysql_connect("localhost","ausername","apassword");
if (!$connect)
{
    echo "Unable to connect to DB: " . mysql_error() . "\n";
    exit(1);
}

if (!mysql_select_db("my_db_name", $conn))
{
    echo ("Unable to select DB: " . mysql_error($conn) . "\n");
    exit(1);
}

$result = mysql_query("SELECT Code FROM Pages WHERE Page='Gallery'", $conn);
if (!$result)
{
    echo ("Query failed: " . mysql_error($conn) . "\n");
    exit(1);
}

$property = mysql_fetch_field($result);

mysql_free_result($result);
mysql_close($conn);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1