Page 1 of 1
Putting MySQL IDs into a PHP Array! PLZ HELP!
Posted: Thu Nov 21, 2002 2:06 pm
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?
Posted: Thu Nov 21, 2002 2:17 pm
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.
Posted: Thu Nov 21, 2002 2:42 pm
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]);
}
Posted: Thu Nov 21, 2002 2:52 pm
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] . ">";
}
source
Posted: Thu Nov 21, 2002 3:23 pm
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>
Errors in source
Posted: Thu Nov 21, 2002 3:24 pm
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!!!
Posted: Thu Nov 21, 2002 3:59 pm
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++)
{
?> <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>
Try something like the following:
Code: Select all
<select name="name">
<option selected>Select Wholesaler</option>
<?
for($x = 0; $x < count($wholarray); $x++) {
$wholid = mysql_query("SELECT wid FROM $wholtablename WHERE wname=$wholarrayї$x]");
$row = mysql_fetch_array($wholid);
echo "<OPTION VALUE=" . $rowї0] . "> $wholarrayї$x]";
}
?>
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"
Posted: Thu Nov 21, 2002 4:29 pm
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ї$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?
Posted: Fri Nov 22, 2002 2:09 am
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ї$x]";
$query = mysql_query($sql) or die(mysql_error().'<p>'.$sql'</p>');
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ї$x]'";
Mac
Sweet Thx
Posted: Fri Nov 22, 2002 7:26 am
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!!
Posted: Fri Nov 22, 2002 9:29 am
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ї$x])."'";
Mac