Putting MySQL IDs into a PHP Array! PLZ HELP!

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
DevilDust
Forum Newbie
Posts: 7
Joined: Tue Nov 19, 2002 11:22 pm
Location: USA

Putting MySQL IDs into a PHP Array! PLZ HELP!

Post by DevilDust »

Okay here's the deal.. I have a table.. and the first field is an ID (auto-increment, NOT-NULL) I want to put all those IDs into an array. The only thing is I want to do a ("SELECT id FROM $table WHERE product=$somevar") What I mean is I want to select the id for a product from the product's row. I used a for loop to loop through and get all of the ids. The loop works, because I used it to loop through and get values of other fields (char fields). But, the problem is that it doesn't pull the right thing. When I displayed what was in the variable ($pid[$x]) It gave me something like Result #17 or something... the actual value SHOULD have been just the number 16... not a clue. Any help plz?
thna_brianb
Forum Newbie
Posts: 6
Joined: Thu Nov 21, 2002 12:24 pm
Contact:

Post by thna_brianb »

Im a psql user, not mysql, but it sounds like all you have to do is run some kind of fetch on the object/array inside of your loop ... i would guess mysql_fetch_array or something to the effect.
DevilDust
Forum Newbie
Posts: 7
Joined: Tue Nov 19, 2002 11:22 pm
Location: USA

Post by DevilDust »

Okay.. here's the code I used. What it does is loops through and puts all names of wholesalers into select boxes. But, I want to put the ID from that row that corrasponds to the wholesaler (wname) or $wholarray[$x];
into the value of that select box option.

for ($x = 0; $x < count($wholarray); $x++)
{
?> <BR>
<?
$wholid = mysql_query("SELECT wid FROM $wholtablename WHERE wname=$wholarray[$x]);
echo "<OPTION VALUE=" . $wholid . ">";

print ($wholarray[$x]);
}
thna_brianb
Forum Newbie
Posts: 6
Joined: Thu Nov 21, 2002 12:24 pm
Contact:

Post by thna_brianb »

After you run the

$wholid = mysql_query("SELECT wid FROM $wholtablename WHERE wname=$wholarray[$x]);

$wholid is only a resource handle. You have to then extract an array or object from this handle depending on what you use. do something like (again, I use PSQL, so bear in mind) :

$result = mysql_query("SELECT wid FROM $wholtablename WHERE wname=$wholarray[$x]);

while ($row = mysql_fetch_array($result)) {
echo "<OPTION VALUE=" . $row[0] . ">";
}
DevilDust
Forum Newbie
Posts: 7
Joined: Tue Nov 19, 2002 11:22 pm
Location: USA

source

Post by DevilDust »

<?php
$server = "*********";
$username = "******";
$password = "******";
$databasename = "wsc";
$tablename = "product";
$wholtablename = "wholesalers";

$connection = mysql_connect("$server","$username","$password");
if(!$connection)
{
echo "Couldn't make a connection!!!";
exit;
}
$db = mysql_select_db("$databasename",$connection);
if(!$db)
{
echo "The database disapeared!";
mysql_close($connection);
exit;
}

$query = mysql_query("SELECT DISTINCT category FROM $tablename");
$num_rows = mysql_num_rows($query);
$catarray = array();
for ( $i = 0; $i < $num_rows; $i++ )
{
$temp = mysql_fetch_array($query);
$catarray[$i] = $temp[0];
}

$query = mysql_query("SELECT wname FROM $wholtablename");
$num_rows = mysql_num_rows($query);

$wholarray = array();
for ( $x = 0; $x < $num_rows; $x++ )
{
$temp = mysql_fetch_array($query);
$wholarray[$x] = $temp[0];
}

?>

<html>
<head>
<title>Product Upload</title>
</head>

<body>
<center>
<FORM METHOD="POST" ACTION="pupload.php" ENCTYPE="multipart/form-data">
Product Name: <input type="text" name="pname" size="30"><br><br>
Product Description: <textarea name="pdes" rows="6" cols="40"></textarea><br><br>
Product Price: <input type="text" name="price" size="15"><br><br>
<input type="hidden" name="MAX_FILE_SIZE" value="700000">
Product Image: <input type="file" size="40" name="file"><br><br>
<INPUT TYPE="radio" NAME="catselected" VALUE="catexisting">
Product Category: <select name="category" size="1">
<option selected>Select Category</option>
<?
for ($x = 0; $x < count($catarray); $x++)
{
?>
<option>
<?
print ($catarray[$x]);
}
?>

</option>
</select><br><br>
<INPUT TYPE="radio" NAME="catselected" VALUE="catnew">New Category: <input type="text" name="newcategory" size="30"><br><br>
Product Wholesaler: <select name="wid" size="1">
<option selected>Select Wholesaler</option>
<?
for ($x = 0; $x < count($wholarray); $x++)
{
?> <BR>
<?
$wholid = mysql_query("SELECT wid FROM $wholtablename WHERE wname=$wholarray[$x]");
while ($row = mysql_fetch_array($wholid)) {
echo "<OPTION VALUE=" . $row[0] . ">";
}

print ($wholarray[$x]);
}
?>

</option>
</select>
<br><br>
<input type="submit" value="upload"><input type="reset">
</form>
</center>
</body>
</html>
DevilDust
Forum Newbie
Posts: 7
Joined: Tue Nov 19, 2002 11:22 pm
Location: USA

Errors in source

Post by DevilDust »

<body>
<center>
<FORM METHOD="POST" ACTION="pupload.php" ENCTYPE="multipart/form-data">
Product Name: <input type="text" name="pname" size="30"><br><br>
Product Description: <textarea name="pdes" rows="6" cols="40"></textarea><br><br>
Product Price: <input type="text" name="price" size="15"><br><br>
<input type="hidden" name="MAX_FILE_SIZE" value="700000">
Product Image: <input type="file" size="40" name="file"><br><br>
<INPUT TYPE="radio" NAME="catselected" VALUE="catexisting">
Product Category: <select name="category" size="1">
<option selected>Select Category</option>
<option>
jewlery<option>
music<option>
toys
</option>
</select><br><br>
<INPUT TYPE="radio" NAME="catselected" VALUE="catnew">New Category: <input type="text" name="newcategory" size="30"><br><br>
Product Wholesaler: <select name="wid" size="1">
<option selected>Select Wholesaler</option>
<BR>
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/c48/wsc/uploadform.php</b> on line <b>81</b><br />
Joe Blows Wholesale <BR>
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/c48/wsc/uploadform.php</b> on line <b>81</b><br />
Tony's Wholesale <BR>
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/c48/wsc/uploadform.php</b> on line <b>81</b><br />
TmMagnifiers <BR>
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/c48/wsc/uploadform.php</b> on line <b>81</b><br />
toycompany wholesalers <BR>
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/c48/wsc/uploadform.php</b> on line <b>81</b><br />
Social Deviant's Wholesaling <BR>
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/c48/wsc/uploadform.php</b> on line <b>81</b><br />
Test wholesaler #3 <BR>
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/c48/wsc/uploadform.php</b> on line <b>81</b><br />
Test wholesaler 5 <BR>
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/c48/wsc/uploadform.php</b> on line <b>81</b><br />
Test wholesaler 9 <BR>
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/c48/wsc/uploadform.php</b> on line <b>81</b><br />
blah wholesaler
</option>
</select>
<br><br>
<input type="submit" value="upload"><input type="reset">
</form>
</center>
</body>
</html>

THE ERRORS DID NOT SHOW UP IN THE HTML PAGE... ONLY IN THE SOURCE... NOT SURE WHAT'S GOING ON HERE!!!
thna_brianb
Forum Newbie
Posts: 6
Joined: Thu Nov 21, 2002 12:24 pm
Contact:

Post by thna_brianb »

Sorry for the lack of help from earlier, didn't know the whole scope of what you were doing until I saw the whole code .... BTW the errors don't show up on the html page because they are inside a select statement. Secondly you should try to print out the SQL statements to ensure they are correct.

Take a look at this:

you have the following:

Code: Select all

Product Wholesaler: <select name="wid" size="1">
<option selected>Select Wholesaler</option>
<?
for ($x = 0; $x < count($wholarray); $x++)
&#123;
?> <BR>
<?
$wholid = mysql_query("SELECT wid FROM $wholtablename WHERE wname=$wholarray&#1111;$x]");
while ($row = mysql_fetch_array($wholid)) &#123;
echo "<OPTION VALUE=" . $row&#1111;0] . ">";
&#125;

print ($wholarray&#1111;$x]);
&#125;
?>

</option>
</select>
Try something like the following:

Code: Select all

<select name="name">
<option selected>Select Wholesaler</option> 
<?
for($x = 0; $x < count($wholarray); $x++) &#123;
  $wholid = mysql_query("SELECT wid FROM $wholtablename WHERE wname=$wholarray&#1111;$x]");
  $row = mysql_fetch_array($wholid);
  echo "<OPTION VALUE=" . $row&#1111;0] . "> $wholarray&#1111;$x]"; 
&#125;
?>
Sorry about the extra while in there, I did not know that you grabbed it previously in your code.

Not sure if this will work, but it seems to be a logical "next try"
DevilDust
Forum Newbie
Posts: 7
Joined: Tue Nov 19, 2002 11:22 pm
Location: USA

Post by DevilDust »

Okay.. I'm still getting the Invalid MySQL Result Resource in Argument errors. I'm thinking it's in the

Code: Select all

$query = mysql_query("SELECT wid FROM $wholtablename WHERE wname=$wholarray&#1111;$x]");
because of the error message. I think there's something wrong here that I don't know about.... maybe in there wname=$wholarray[$x]... maybe quotes i'm missing or brackets or... who knows... any idea?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

To debug a MySQL query it is always a good idea to use mysql_error() and to put the SQL statement into its own variable so you can echo() it out and make sure it looks right once the variables have been added in. Try something like:

Code: Select all

$sql = "SELECT wid FROM $wholtablename WHERE wname=$wholarray&#1111;$x]";
$query = mysql_query($sql) or die(mysql_error().'&lt;p&gt;'.$sql'&lt;/p&gt;');
The problem may be occuring because you need some quotes in your WHERE clause, try:

Code: Select all

$sql = "SELECT wid FROM $wholtablename WHERE wname='$wholarray&#1111;$x]'";
Mac
DevilDust
Forum Newbie
Posts: 7
Joined: Tue Nov 19, 2002 11:22 pm
Location: USA

Sweet Thx

Post by DevilDust »

It's working now, it put the right IDs into the right option boxes, except for two. and the two that produce error's have apostraphe's in them ('). How do I work around th at? THX!!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You need to escape characters such as ' and " in your SQL statments, to do this you would use addslashes().

Code: Select all

$sql = "SELECT wid FROM $wholtablename WHERE wname='".addslashes($wholarray&#1111;$x])."'";
Mac
Post Reply