Putting MySQL IDs into a PHP Array! PLZ HELP!
Moderator: General Moderators
Putting MySQL IDs into a PHP Array! PLZ HELP!
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:
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]);
}
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:
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] . ">";
}
$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
<?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>
$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
<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!!!
<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:
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:
Try something like the following:
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"
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>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]";
}
?>Not sure if this will work, but it seems to be a logical "next try"
Okay.. I'm still getting the Invalid MySQL Result Resource in Argument errors. I'm thinking it's in the 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?
Code: Select all
$query = mysql_query("SELECT wid FROM $wholtablename WHERE wname=$wholarrayї$x]");- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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:
The problem may be occuring because you need some quotes in your WHERE clause, try:
Mac
Code: Select all
$sql = "SELECT wid FROM $wholtablename WHERE wname=$wholarrayї$x]";
$query = mysql_query($sql) or die(mysql_error().'<p>'.$sql'</p>');Code: Select all
$sql = "SELECT wid FROM $wholtablename WHERE wname='$wholarrayї$x]'";- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You need to escape characters such as ' and " in your SQL statments, to do this you would use addslashes().
Mac
Code: Select all
$sql = "SELECT wid FROM $wholtablename WHERE wname='".addslashes($wholarrayї$x])."'";