Issues with searching my database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ZEKI692
Forum Newbie
Posts: 12
Joined: Tue Aug 31, 2010 5:38 pm

Issues with searching my database

Post by ZEKI692 »

So, I'm not sure what I'm doing wrong. I'm trying to have a php script search the database and extract the information it finds, and if no entry is found, list some declared values. It currently returns
[text]Resource id #4$[/text] When it should actually return some values from the database
Here's what I've got so far.

Code: Select all

	
	
$id = NULL;
$wanted = NULL;
$price = NULL;
$weight = NULL;

function weight($wanted)
{
	
	$result = mysql_query('SELECT * from test WHERE id = "$wanted"');
    if (!result)
	{
		$weight = '13';
		echo $weight;
	}
	else
	{
		$row = mysql_fetch_assoc($result);
		$id = $row['id'];
		$weight = $row['weight'];
		echo $weight;
	}
}

function lookup($wanted) //Part number of the item this form is for.
{
	$result = mysql_query('SELECT * from test WHERE id = "$wanted"');
	echo $result;
	if (!result)
	{
		$price = 'Inquire';
		echo $price;
	}
	else
	{
		$row = mysql_fetch_assoc($result);
		$id = $row['id'];
		$price = '$ '.$row['price'];
		echo $price;
	}
}
        
?>
Last edited by ZEKI692 on Wed Sep 08, 2010 6:51 pm, edited 2 times in total.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Issues with searching my database

Post by mecha_godzilla »

There's a syntax error on your first mysql_query() because you're missing a single quote at the end - just look at the syntax highlighting to see what I mean. I can't offer any insight into the "Tester Resource id #4$" message though - maybe someone else can!

HTH,

Mecha Godzilla
ZEKI692
Forum Newbie
Posts: 12
Joined: Tue Aug 31, 2010 5:38 pm

Re: Issues with searching my database

Post by ZEKI692 »

Right, silly mistake, I'll edit my original post to fix it. Thanks for the help with that, Mecha.
ajith_rock
Forum Newbie
Posts: 11
Joined: Wed Sep 08, 2010 2:37 am

Re: Issues with searching my database

Post by ajith_rock »

Are you sure that your table has a column named "price"? And are you sure that the column "price" has non-empty values?

If you answer yes to both the questions, then try this :

Code: Select all

function lookup($wanted) //Part number of the item this form is for.
{
        $result = mysql_query('SELECT * from test WHERE id = "$wanted"');
        echo $result; //Lets see what $result is
        if (!result)
        {
                $price = 'Inquire';
                echo $price;
        }
        else
        {
                while($row = mysql_fetch_array($result))
				{
					$id = $row['id'];
					$price = '$ '.$row['price'];
					echo $price;
				}		
				
        }
}
ZEKI692
Forum Newbie
Posts: 12
Joined: Tue Aug 31, 2010 5:38 pm

Re: Issues with searching my database

Post by ZEKI692 »

Yeah I have the column and the values are NOT empty. I've done what you suggested and now I get this result - Resource id #4 .

Code as of now.

Code: Select all

$id = NULL;
$wanted = NULL;
$price = NULL;
$weight = NULL;

function weight($wanted)
{
	$result = mysql_query('SELECT * from test WHERE id = "$wanted"');
    if (!result)
	{
		$weight = '13';
		echo $weight;
	}
	else
        {
            while($row = mysql_fetch_array($result))
            {
				$id = $row['id'];
                $weight = $row['price'];
                echo $weight;
            }                                    
        }
}

function lookup($wanted) //Part number of the item this form is for.
{
        $result = mysql_query('SELECT * from test WHERE id = "$wanted"');
        echo $result; //Lets see what $result is
        if (!result)
        {
            $price = 'Inquire';
            echo $price;
        }
        else
        {
            while($row = mysql_fetch_array($result))
            {
				$id = $row['id'];
                $price = '$ '.$row['price'];
                echo $price;
            }                                    
        }
}
        
?>
Last edited by ZEKI692 on Wed Sep 08, 2010 6:52 pm, edited 1 time in total.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Issues with searching my database

Post by mecha_godzilla »

Ok, no problem! :)

I'm not familiar with what the issue might be, so in the meantime you could try searching for "Resource id #4" on the forum or search engines. It looks like it's to do with MySQL in any case. I don't see where in the script the DB is that you're trying to connect to, plus I noticed that you're not checking to see whether the MySQL query completed successfully, as this would tell you if there was a problem with the query. Where you've got this:

Code: Select all

$result = mysql_query('SELECT * from test WHERE id = "$wanted"');
change it to:

Code: Select all

$result = mysql_query('SELECT * from test WHERE id = "$wanted"') or die('Nope, that went wrong!');
HTH,

M_G
ZEKI692
Forum Newbie
Posts: 12
Joined: Tue Aug 31, 2010 5:38 pm

Re: Issues with searching my database

Post by ZEKI692 »

I thought where I had the

Code: Select all

if (!result)
would do the same as the die in

Code: Select all

$result = mysql_query('SELECT * from test WHERE id = "$wanted"') or die('Nope, that went wrong!');
. Am I mistaken? I'll go ahead and change it anyways, if there is a function for it its better than an if statement!

And the same error persists.

I'm going to post the whole page of code. Maybe its not just an error in php?

Code: Select all

<p>Group 1</p>
<p> <img src="images/stories/Manuals/ManualName/group##.gif" border="0" width="580" height="750" />



<?php
	mysql_connect("admintestscs.db.2879572.hostedresource.com", "*****", "*****") or die(mysql_error());
	mysql_select_db("admintestscs") or die(mysql_error());
	
$id = NULL;
$wanted = NULL;
$price = NULL;
$weight = NULL;

function weight($wanted)
{
	$result = mysql_query('SELECT * from test WHERE id = "$wanted"') or die('Could not find the product');
	if (!result)
	{
		$weight = '13';
		echo $weight;
	}
	else
        {
            while($row = mysql_fetch_array($result))
            {
				$id = $row['id'];
                $weight = $row['price'];
                echo $weight;
            }                                    
        }
}

function lookup($wanted) //Part number of the item this form is for.
{
        $result = mysql_query('SELECT * from test WHERE id = "$wanted"') or die('Could not find the product');
        echo $result; //Lets see what $result is
        if (!result)
        {
            $price = 'Inquire';
            echo $price;
        }
        else
        {
            while($row = mysql_fetch_array($result))
            {
				$id = $row['id'];
                $price = '$ '.$row['price'];
                echo $price;
            }                                    
        }
}
        
?>


<html>
<head>
</head>
<body>

<script type="text/javascript"> 
function UpdateCost() {
var sum = 0;
var qty,
prc;
for (i=1; i<2; i++) { // Where i<## Where ## = [total number of products = 1]
qty = 'qty'+i;
prc = 'price'+i;
if
(document.getElementById(qty).checked == true)
{
sum += Number(document.getElementById(prc).value);
}
}
document.getElementById('totalcost').value = sum.toFixed(2);
}
</script> 


<form action="http://ww5.aitsafe.com/cf/addmulti.cfm" method="post"> 
<input name="userid" type="hidden" value="78290079">

<input type="hidden" name="product1" value="Tester">
<input type="checkbox" id="qty1" name="qty1" value="1" onClick="UpdateCost()">
<input type="hidden" id="price1" name="price1" value="<?php lookup("000005"); ?>">Tester  <?php lookup("000005"); ?>
<input name="units1" type="hidden" value="<?php weight("000005"); ?>">
<br>
<strong>Your Parts Cost $</strong>
<input type="text" id="totalcost" value="">&nbsp;
<input type="submit" value="Buy Your Parts">&nbsp;
<input type="reset" value="Reset"> 
</form>


</body>
</html>
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Issues with searching my database

Post by mecha_godzilla »

The point of using die() is that it will stop the script from executing. You can also replace it with your own custom error handler if necessary. I forgot to mention that you can also echo out mysql_error() to get the actual message.

You might want to have a look at the mysql_query page in the PHP manual:

http://uk.php.net/manual/en/function.mysql-query.php

specifically:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.
HTH,

M_G
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Issues with searching my database

Post by mikosiko »

ZEKI692 wrote:I thought where I had the

Code: Select all

if (!result)
would do the same as the die in

Code: Select all

$result = mysql_query('SELECT * from test WHERE id = "$wanted"') or die('Nope, that went wrong!');
. Am I mistaken? I'll go ahead and change it anyways, if there is a function for it its better than an if statement!
....
[/syntax]
I didn't read all your code but I see some errors in a few lines...

a) change your mysql_query sintax (all of them) from this:

Code: Select all

$result = mysql_query('SELECT * from test WHERE id = "$wanted"')
to this

Code: Select all

$result = mysql_query("SELECT * from test WHERE id = '$wanted' ") //switch ' to " and vice-versa
change this line

Code: Select all

if (!result)
for this

Code: Select all

if (!$result)
// "result" doesn't exist. ... your resulset name is $result
ZEKI692
Forum Newbie
Posts: 12
Joined: Tue Aug 31, 2010 5:38 pm

Re: Issues with searching my database

Post by ZEKI692 »

Sorry for the delayed reply, been busy.

Thanks for the help guys, all your help has solved the problem!
Post Reply