Jump to content

blank page being returned with this very simple code?


katie77

Recommended Posts

Hi,

 

I'm trying to learn php and the following code is returning a blank page.

 

Have I done something stupid?  I'm using WAMP to test and it's connecting to the database fine.

 

I've also tested the sql in myphpadmin and it's returning the correct variable

 

However on this page it's just returning the html 'hello2' and no variable from the database.

 

Can anyone point out where I'm being stupid?  :-[

 

<?php 
$username="root";
$password="";
$database="test";
$dataset="software";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query=
"SELECT *
FROM $database.$dataset
WHERE name = 'Maya';";
$result=mysql_query($query);

?>
<html>
Hello2
<?php echo $result;?>
</html>

Link to comment
Share on other sites

Ok So I've followed both your advice!

 

Now I get:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in G:\DEV WEB\Dev Server\test\Ratings.php on line 15

 

<html>
<?php 
$username="root";
$password="";
$database="test";
$dataset="software";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query=
"SELECT *
FROM $database.$dataset
WHERE name = 'Maya';";
$result=mysql_query($query);
$row = mysql_fetch_array($result);

echo "{$row['field']}";
?>

</html>

Link to comment
Share on other sites

k if you want to display al the informatin from the table where name = 'maya' you need a while loop that runs through the table until there are no more rows.

while ($row = mysql_fetch_assoc($result)){
			$id = $row['PicID'];
			$name = $row['Name'];
			}

 

pretty much create variable to hold each value that would be inside of the table, then  use

 

echo $id;

 

for example to echo that result.  you must do this for each value within that row.  normally going  echo "Id = ".$id."<BR>";  will produce a readable result.  by doing that for each variable(thus each value) you will have a listof everything in the table where name = 'maya'

 

$query= "SELECT * FROM $database.$dataset WHERE name = 'Maya'";

 

put that in for your query.  you had an extra ;  at the end which will make it fail.  hope that all helps

 

 

Link to comment
Share on other sites

Thanks - I think I have made the problem more complicated by putting SELECT *.  I will start again:

I have made the following table:

 

name  cost_pound

Maya3000

Max3200

 

What I would like is an html page which displays cost_pound WHERE name = 'Maya'.

That is, an html page displaying '3000'.  That's it.

 

Sorry I wasn't very clear.  Thanks again :)

Link to comment
Share on other sites

select * should work.  what that does is return all values within the row that contains the name Maya

 

<html>
<?php 
$username="root";
$password="";
$database="test";
$dataset="software";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query=
"SELECT * FROM $database.$dataset WHERE name = 'Maya'";
$result=mysql_query($query);


while ($row = mysql_fetch_assoc($result)){
			$name = $row['name];
			$cost = $row['cost_pound'];
echo "Name = ".$name."<BR>";
echo "Cost = ".$cost."<BR>";
			}

// Closing the Database
		$result = mysql_close($link);
		if(!$result)
		{
			echo "Couldn't close the Database Properly<BR>";
		}
?>

</html>

 

try this code and see if it works.  also i added in closing the database.  won't cause too many problems right now but may in the future.

Link to comment
Share on other sites

hi there, thanks for the help so far!  ;D

 

I think you might have missed an apostrophe in $name = $row['name]; so I added that after name, however it now says:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in G:\DEV WEB\Dev Server\test\Ratings.php on line 15

 

Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in G:\DEV WEB\Dev Server\test\Ratings.php on line 23

Couldnt close the Database Properly

 

What does this mean?

Link to comment
Share on other sites

haha ah yes i forgot you connected to the database differently than i do....for the sake that i've never done anyother way.  try

// Opening the Database
            $link = mysql_connect(localhost,$username,$password);
            if(!$link)
            {
                echo "The link was in error <BR>";
                exit();
            }
        
            
            
            // Select a Database
            $db_link = mysql_select_db($database, $link);
            if(!$db_link )
            {
                echo "Selecting a Database failed!<BR>";
                mysql_close($link);				// Close the Connection
                exit();
            }

 

 

that's for connecting and choosing the database to work with. 

 

try changing the query to

$query= "SELECT * FROM $dataset WHERE name = 'Maya'";

 

this "should" work  :)

Link to comment
Share on other sites

hmm blank page again - just to check here's my code in full:

 

<html>
<?php 
$username="root";
$password="";
$database="test";
$dataset="software";
// Opening the Database
            $link = mysql_connect(localhost,$username,$password);
            if(!$link)
            {
                echo "The link was in error <BR>";
                exit();
            }
        
            
            
            // Select a Database
            $db_link = mysql_select_db($database, $link);
            if(!$db_link )
            {
                echo "Selecting a Database failed!<BR>";
                mysql_close($link);				// Close the Connection
                exit();
            }

$query= "SELECT * FROM $dataset WHERE name = 'Maya'";

?>
</html>

Link to comment
Share on other sites

No i didn't  :-\- I added it and now I get error:

 

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in G:\DEV WEB\Dev Server\test\Ratings.php on line 31

 

I've pasted my complete code below...

 

<?php 
$username="root";
$password="";
$database="test";
$dataset="software";
// Opening the Database
            $link = mysql_connect(localhost,$username,$password);
            if(!$link)
            {
                echo "The link was in error <BR>";
                exit();
            }
        
            
            
            // Select a Database
            $db_link = mysql_select_db($database, $link);
            if(!$db_link )
            {
                echo "Selecting a Database failed!<BR>";
                mysql_close($link);				// Close the Connection
                exit();
            }

$query= "SELECT * FROM $dataset WHERE name = 'Maya'";

$result=mysql_query($query);


while ($row = mysql_fetch_assoc($result)){
			$name = $row['name'];
			$cost = $row['cost_pound'];
echo "Name = ".$name."<BR>";
echo "Cost = ".$cost."<BR>";
			}

?>


</html>

Link to comment
Share on other sites

oh gawd... almost too embarrassed to reply - your answer prompted me to check and I was getting muddled with another database, so I changed it and it works perfectly... so sorry for being stupid and thanks for helping me get to the bottom of it...! :-X

 

Can I ask what software you use for php?  I'm not sure if there's any shortcuts - for example do you write out the connect code each time or do you use software that let's you click a button to add the 'connect' script etc?  (as you can tell I need all the help I can get! ;) )

Link to comment
Share on other sites

no with each php page i write that uses a database you have to write out the code for connecting and selecting.  I use Adobe Dreamweaver for scripting and use a separate ftp software for uploading.  if you use an Apple computer... cyberduck is a very god program for uploading and if you use Windows, filezilla is pretty good too.  i'd avoid using dreamweavers built in ftp uploader as i've not had much luck with it in the past.  i'm glad you got it all working now. :)  have a good day!

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.